protected void Button1_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "") || (TextBox2.Text == ""))
        {
            Label1.Text = "账号和密码不能为空!";
        }
        else
        {
            string conn = "Data Source=FY1\\SQLEXPRESS;Initial Catalog=web;User ID=fytest;Password=123456";
            SqlConnection myconnection=new SqlConnection (conn );
            myconnection .Open ();//打开数据库            string comm="select * from tab_manager where M_name='" +TextBox1.Text+ "' and M_password='" +TextBox2.Text+ "'";
            
            SqlCommand cmd=new SqlCommand (comm,myconnection );
            SqlDataReader myreader=cmd.ExecuteReader();            if (myreader .Read ())
            {
            }
            else
            {
                Label1 .Text ="账号和密码错误!";
            }
            myreader.Close (); 
            myconnection .Close ();        }
    }
当我输入正确的账号和密码时点击登陆出现错误
数据类型 text 和 varchar 在 equal to 运算符中不兼容。
我的表中M_name M_password的数据类型都为text
请大虾帮帮忙,谢谢!!!!

解决方案 »

  1.   

    估计你的M_name字段是Text类型的数据,需要做转换:
    替换以下的代码:string comm="select * from tab_manager where Convert(M_name,VarChar)='" +TextBox1.Text+ "' and Convert(M_password,VarChar)='" +TextBox2.Text+ "'"; 
      

  2.   

    不好意思,写反了,应该是如下这样:
    string comm="select * from tab_manager where Convert(VarChar,M_name)='" +TextBox1.Text+ "' and Convert(VarChar,M_password)='" +TextBox2.Text+ "'"; 
      

  3.   

    string comm="select * from tab_manager where M_name='" +TextBox1.Text.ToString().Trim()+ "' and M_password='" +TextBox2.Text.ToString().Trim()+ + "'";