private void b1_Click(object sender, System.EventArgs e)
{
Form1 theOwner = (Form1)this.Owner ; //获得对主窗口的引用
string uname=t1.Text.ToString();
string upwd=t2.Text.ToString();
//连接数据库
string strConn="server=Localhost;uid=sa;pwd=6079197;database=test";
// 连接到eForum数据库
SqlConnection conn=new SqlConnection(strConn);
// 打开连接
conn.Open();
// 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
// 创建Command对象
SqlCommand cmd=new SqlCommand(str,conn);
// 执行ExecuteReader()方法
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Form2 frmMain = new Form2();
frmMain.Show ();
this.Hide ();
}
else
{
MessageBox.Show ("用户名和密码错误!","提示");
}
dr.Close();
conn.Close();
}运行时 SqlDataReader dr=cmd.ExecuteReader(); 有问题,提示如下:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
{
Form1 theOwner = (Form1)this.Owner ; //获得对主窗口的引用
string uname=t1.Text.ToString();
string upwd=t2.Text.ToString();
//连接数据库
string strConn="server=Localhost;uid=sa;pwd=6079197;database=test";
// 连接到eForum数据库
SqlConnection conn=new SqlConnection(strConn);
// 打开连接
conn.Open();
// 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
// 创建Command对象
SqlCommand cmd=new SqlCommand(str,conn);
// 执行ExecuteReader()方法
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Form2 frmMain = new Form2();
frmMain.Show ();
this.Hide ();
}
else
{
MessageBox.Show ("用户名和密码错误!","提示");
}
dr.Close();
conn.Close();
}运行时 SqlDataReader dr=cmd.ExecuteReader(); 有问题,提示如下:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
解决方案 »
- 100分 急求 CRC-16位 (虽然分很少,我也没有多余的分了)
- 字符串如何实现不断翻位并进位
- ???关于测试用例
- 在当前窗体隐藏,另一个窗体关闭是显示
- 有关DataGrid里边显示标题的问题
- 求Microsoft Visual Studio .NET 2003 七CD下载地址呀
- datagridview DataGridViewCheckBoxColumn
- Excel取值赋给DataTable 的问题
- 用C#编程显示器分辨率问题?在线等待
- Forms.DialogResult和Forms。show的区别和联系??以及它们分别该在什么时候用?
- IT人士,职业生涯规划
- 请较高手帮忙解决Socket的问题!!!
// 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
// 创建Command对象
SqlCommand cmd=new SqlCommand(str,conn);改成:
// 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
conn.Open();
// 创建Command对象
SqlCommand cmd=new SqlCommand(str,conn);
看看
这样的话还能读到什么信息
登陆呢
就是测试用户名或密码对与不对
select userpawdfrom user where username=?
cmd.Parameters.Add(New OleDbParameter("username", OleDbType.VarWChar, 50))
cmd.Parameters["username"].value=textbox.text
然后用参数把textbox的值传进去
这样防止注入这样取出来的密码与用户输入的密码进行对比
就OK了
出现异常用try catch捕获一下,错误信息就出来了
SqlCommand cmd=new SqlCommand(str,conn);
// 执行ExecuteReader()方法
try
{
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Form2 frmMain = new Form2();
rmMain.Show ();
this.Hide ();
}
}
catch(Exception ex)
{
message.show(ex.tostring())
}这样有时就能出现详细的错误信息 else
看上面的代码,能出错的就只有
//连接数据库
string strConn="server=Localhost;uid=sa;pwd=6079197;database=test";
// 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
conn.Open();
// 创建Command对象
SqlCommand cmd=new SqlCommand(str,conn);
int i = (int)cmd.ExecuteScalar();
if(i == 0)
{
用户名或密码不正确
}
else
{
跳转到正确的页面(用户和密码正确);}