请问大虾c# asp.net这一小段登陆的代码为何执行不下去了?

来源:百度知道 编辑:UC知道 时间:2024/07/02 11:11:21
没有错误提示,但执行时提示异常,标志在“da.Fill(dt);”
代码如下:

string str = "server='(local)';DataBase='master';User ID='sa';Password='sa'";
SqlConnection con = new SqlConnection(str );
con.Open();
string sql = "select * from userl where username='"+TextBox1 .Text +"' and userpass='"+TextBox2 .Text +"'";

DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql,con );
da.Fill(dt );

if(dt .Rows .Count >0 )
{

Response.Write("你成功登陆了");
}
else
{

Response.Write("你错误了");

}
!!!发错代码了。。上面那样做就没错,但我把代码变成下面后就有异常了~~~~:
string str = "server='(local)';DataBase='1';User ID='sa';Password=''";<

是不是SQL语句写错了? 后面的是Select * from 1 你原来的是from userl 是不是这个的原因

我试了下 select * from 1这个语句有问题,SQL查询的时候表名不能用数字的,数据库名字也不能

可以改为 select * from [1]来试试

运行到选定地方。
查看sql 字符串的值。
在查询分析器中,运行一下判断出错在何

da.Fill(dt,"userl " );
这样就对了

string sql = "select * from 1 where username='" + TextBox1.Text + "' and userpwd='" + TextBox2.Text + "'";

是不是你拼接的sql字符串有问题。你看看输入的是什么。既然有异常,你应该把异常放上来看看。