C#写的一段小代码,可调试的时候总是出现问题,郁闷的是我几乎是从书上抄下来的代码,请大家帮忙看看,是拿了错了,谢谢了
string name = Login1.UserName;
string password = Login1.Password;
string connection = @"provider=microsoft.ace.oledb.12.0; Data Source=g:\临时\数据库事例\用户信息.accdb";
OleDbConnection conn = new OleDbConnection(connection);
OleDbCommand command = new OleDbCommand(string.Format ("Select count(*) From user Where name='{0}' and password='{1}'", name, password));
conn.Open();
int count = (int)command.ExecuteScalar();
if (count > 0)
{
e.Authenticated = true;
Response.Redirect("http://www.t.sina.com.cn/");
}
else
{
e.Authenticated = false;
ClientScript .RegisterStartupScript (this.GetType (),"无此用户","<script language='javascript'>alert('你输入的信息有误')</script>";
}
conn.Close();
Select count(*) From user
改成 Select count(*) From [user] 试试
你应该把错误信息也贴出来
稍微那么一看,感觉你的OleDbCommand那句有问题
OleDbCommand command = new OleDbCommand(string.Format ("Select count(*) From user Where name='{0}' and password='{1}'", name, password));
没见你把command跟connection关联起来。。。。下面这样就对了
OleDbCommand command = new OleDbCommand(string.Format ("Select count(*) From user Where name='{0}' and password='{1}'", name, password), connection);
command类型写明。另外那个user可能是系统对象,所以跟系统对象同名的加上[]也就是楼上说的【user】