帮我看看登陆错误原因

Fy_Hello 2009-04-21 09:26:25
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
请大虾帮帮忙,谢谢!!!!
...全文
46 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝海D鱼 2009-04-21
  • 打赏
  • 举报
回复
string comm="select * from tab_manager where M_name='" +TextBox1.Text.ToString().Trim()+ "' and M_password='" +TextBox2.Text.ToString().Trim()+ + "'";
Fy_Hello 2009-04-21
  • 打赏
  • 举报
回复
谢谢,没错了.
zsuswy 2009-04-21
  • 打赏
  • 举报
回复
不好意思,写反了,应该是如下这样:
string comm="select * from tab_manager where Convert(VarChar,M_name)='" +TextBox1.Text+ "' and Convert(VarChar,M_password)='" +TextBox2.Text+ "'";
zsuswy 2009-04-21
  • 打赏
  • 举报
回复
估计你的M_name字段是Text类型的数据,需要做转换:
替换以下的代码:

string comm="select * from tab_manager where Convert(M_name,VarChar)='" +TextBox1.Text+ "' and Convert(M_password,VarChar)='" +TextBox2.Text+ "'";
Fy_Hello 2009-04-21
  • 打赏
  • 举报
回复
是不是数据类型不对,如果是,要怎么改
最好不改数据表的字段数据类型
有什么方法?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧