大家帮我看看到底是哪里错了?数据库名字:购物系统 表:用户表 字段:序列编号、姓名、密码。protected void Login_Click(object sender, EventArgs e)
{
string username = tbUsername.Text;
string password = tbPassword.Text;
string ConnectionString = "Server=(local);User id=sa;Pwd=123;Database=购物系统";//连接数据库字符串
string Sql = "SELECT * FROM 用户表 WHERE 姓名='" + username+ "'";
SqlConnection Connection = new SqlConnection(ConnectionString); //创建SqlConnection对象
SqlCommand Command = new SqlCommand(Sql, Connection); Command.CommandText = "SELECT 密码 FROM 用户表 where 姓名='" + username + "'";
Command.CommandType = CommandType.Text; try
{
Command.Connection.Open();//打开数据库连接
object test = Command.ExecuteScalar();//获取查询结果
Response.Write (test);
SqlDataReader dr = Command.ExecuteReader();//执行SQL语句返回DataReader对象
while (dr.Read())
{
string strpassword = dr["密码"].ToString();//强制转换成STRING型数据
Response.Write(strpassword); if (password == strpassword)
{
Response.Redirect("default.aspx");
}
else
{
string message = ("对不起,您输入的内容有误!");
Response.Redirect("index.aspx?MSG=" + message);
} }
dr.Close(); //关闭 DataReader }
catch (SqlException ex)
{
//Response.Write ("数据库连接失败");
//Response.Write (ex);
}
finally
{
Connection.Close();//关闭数据库
Command.Connection.Close(); //关闭数据库连接 } }
{
string username = tbUsername.Text;
string password = tbPassword.Text;
string ConnectionString = "Server=(local);User id=sa;Pwd=123;Database=购物系统";//连接数据库字符串
string Sql = "SELECT * FROM 用户表 WHERE 姓名='" + username+ "'";
SqlConnection Connection = new SqlConnection(ConnectionString); //创建SqlConnection对象
SqlCommand Command = new SqlCommand(Sql, Connection); Command.CommandText = "SELECT 密码 FROM 用户表 where 姓名='" + username + "'";
Command.CommandType = CommandType.Text; try
{
Command.Connection.Open();//打开数据库连接
object test = Command.ExecuteScalar();//获取查询结果
Response.Write (test);
SqlDataReader dr = Command.ExecuteReader();//执行SQL语句返回DataReader对象
while (dr.Read())
{
string strpassword = dr["密码"].ToString();//强制转换成STRING型数据
Response.Write(strpassword); if (password == strpassword)
{
Response.Redirect("default.aspx");
}
else
{
string message = ("对不起,您输入的内容有误!");
Response.Redirect("index.aspx?MSG=" + message);
} }
dr.Close(); //关闭 DataReader }
catch (SqlException ex)
{
//Response.Write ("数据库连接失败");
//Response.Write (ex);
}
finally
{
Connection.Close();//关闭数据库
Command.Connection.Close(); //关闭数据库连接 } }
解决方案 »
- System.Array.IndexOf问题
- 帮看一下正则替换问题
- 请问几个C#中设计WINFORM时数据库操作的问题!
- e-mail编程的问题
- ComboBox 怎么让 value值等于指定值的项成为选中
- 求书籍《Professional C# Web Service:Building .NET Web Services with ASP.NET and .NET Remoting》
- 如何在網頁上開啟一個確認Dialog Box
- 怎样制作vs2005windows安装文件?
- 求助,运行时出错
- C#应用程序安装在win98下,窗口的大小改变问题 ,急! 在线等待
- 一个很诡异的问题WinForm
- 初学者求教:C#中Request.QueryString实例结果是什么?
你把这里的SqlException换成Exception呢,看看会不会提示错误
{
string username = tbUsername.Text;
string password = tbPassword.Text;
string ConnectionString = "Server=(local);User id=sa;Pwd=123;Database=购物系统";//连接数据库字符串
string Sql = "SELECT * FROM 用户表 WHERE 姓名='" + username+ "'"; SqlConnection Connection = new SqlConnection(ConnectionString); //创建SqlConnection对象
SqlCommand Command = new SqlCommand(Sql, Connection); Command.CommandText = "SELECT 密码 FROM 用户表 where 姓名='" + username + "'"; Command.CommandType = CommandType.Text; try
{
Command.Connection.Open();//打开数据库连接
object test = Command.ExecuteScalar();//获取查询结果
Response.Write (test);
SqlDataReader dr = Command.ExecuteReader();//执行SQL语句返回DataReader对象
while (dr.Read())
{
string strpassword = dr["密码"].ToString();//强制转换成STRING型数据
Response.Write(strpassword);
if (password == strpassword)
{
Response.Redirect("default.aspx");
}
else
{
string message = ("对不起,您输入的内容有误!");
Response.Redirect("index.aspx?MSG=" + message);
} } dr.Close(); //关闭 DataReader }
catch (SqlException ex)
{
//Response.Write ("数据库连接失败");
//Response.Write (ex);
}
finally
{
Connection.Close();//关闭数据库
Command.Connection.Close(); //关闭数据库连接 } }
{
//Response.Write ("数据库连接失败");
//Response.Write (ex);
}改成catch (SqlException ex)
{
Response.Write ("数据库连接失败");
Response.Write (ex);
}
好像用了同一个command对象来接收sql语句,好像无法识别