请大家帮忙解决下。谢谢!private void btn1_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("Data Source=(Local),Integrated Security=SSPI;DataBase=UserMessage");
SqlCommand cmd = new SqlCommand("select * from UserInfo where UserName='"+this.textBox1.Text+"'and PassWord='"+this.textBox2.Text+"'");
SqlDataReader dr = cmd.ExecuteNonQuery();//问题在这里,调试出现 无法将类型"int"隐式转化为"System.data.SqlClient.SqlDataReader"
if (dr.Read())
{ MessageBox.Show("成功登陆!","登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("用户名或密码有误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
{
SqlConnection connection = new SqlConnection("Data Source=(Local),Integrated Security=SSPI;DataBase=UserMessage");
SqlCommand cmd = new SqlCommand("select * from UserInfo where UserName='"+this.textBox1.Text+"'and PassWord='"+this.textBox2.Text+"'");
SqlDataReader dr = cmd.ExecuteNonQuery();//问题在这里,调试出现 无法将类型"int"隐式转化为"System.data.SqlClient.SqlDataReader"
if (dr.Read())
{ MessageBox.Show("成功登陆!","登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("用户名或密码有误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
解决方案 »
- 请教:Winform下DataGridView Column的index如何改变?
- C# 编写猜单词小程序遇到的问题
- 求助:"不支持给定路径的格式"
- 这两种连接字符串的写法问题?有什么本质区别?
- 正则表达式样替换时间格式文本的问题
- C#下需用用DDE接收数据,和以前VB6用TextBox一样。如何简单的解决?
- Rtf格式的图片添加名称
- 安装visual studio 2005 时候出现问题 ,请求援助
- socket.send的问题
- vs2017 使用RDLC报表 把reportviewer拖到窗体上没反应,没有界面显示,如何解决,求高手
- C#调用dll “尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
- C#dll调用
试试吧
试试吧
而dr是DataReader类型的变量,所以你把一个int值赋值给一个DataReader类型的变量肯定有问题
SqlConnection connection = new SqlConnection("Data Source=(Local),Integrated Security=SSPI;DataBase=UserMessage");
SqlCommand cmd = new SqlCommand("select * from UserInfo where UserName='"+this.textBox1.Text+"'and PassWord='"+this.textBox2.Text+"'");
if(cmd.ExecuteNonQuery()>0)
{
MessageBox.Show("成功登陆!","登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information); }
else
{
MessageBox.Show("用户名或密码有误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
dr 为DataReader 类型。cmd.ExecuteNonQuery() 返回值为int 肯定会报错。
把cmd.ExecuteNonQuery()改成cmd.ExecuteReader();