using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace 图书管理系统
{
public partial class XGPassword : Form
{
public XGPassword()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("data source=WWW-7F333BFCAB9\\SQLEXPRESS;initial catalog=图书管理系统;integrated security=true");
cn.Open();
SqlCommand cmd = new SqlCommand("select Xsmm from xs where Xszh='" + textBox1.Text + "'", cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
String s = textBox2.Text;
String t = dr["Xsmm"].ToString().Trim();
if (t.Equals(s))
{
string mm1 = textBox3.Text;
string mm2 = textBox4.Text;
if (mm1 == mm2)
{
using (SqlCommand cmd1 = new SqlCommand("update users set Xsmm=" + textBox3.Text + " where id=" + textBox1.Text + " ", cn))
{
int count = cmd1.ExecuteNonQuery();
if (count == 1)
{
MessageBox.Show("恭喜!密码成功", "信息框", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
MessageBox.Show("对不起!密码修改失败!", "信息框", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
else
{
MessageBox.Show("两次输入密码不一致");
}
}
else
{
MessageBox.Show("密码出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox2.Focus();
}
}
else
{
MessageBox.Show("账号出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
}
cn.Close();
} }
} 运行 修改密码出错在 黄色部分 错误提示为:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace 图书管理系统
{
public partial class XGPassword : Form
{
public XGPassword()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("data source=WWW-7F333BFCAB9\\SQLEXPRESS;initial catalog=图书管理系统;integrated security=true");
cn.Open();
SqlCommand cmd = new SqlCommand("select Xsmm from xs where Xszh='" + textBox1.Text + "'", cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
String s = textBox2.Text;
String t = dr["Xsmm"].ToString().Trim();
if (t.Equals(s))
{
string mm1 = textBox3.Text;
string mm2 = textBox4.Text;
if (mm1 == mm2)
{
using (SqlCommand cmd1 = new SqlCommand("update users set Xsmm=" + textBox3.Text + " where id=" + textBox1.Text + " ", cn))
{
int count = cmd1.ExecuteNonQuery();
if (count == 1)
{
MessageBox.Show("恭喜!密码成功", "信息框", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
MessageBox.Show("对不起!密码修改失败!", "信息框", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
else
{
MessageBox.Show("两次输入密码不一致");
}
}
else
{
MessageBox.Show("密码出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox2.Focus();
}
}
else
{
MessageBox.Show("账号出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
}
cn.Close();
} }
} 运行 修改密码出错在 黄色部分 错误提示为:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
if (t.Equals(s))
{
上面加一句
dr.Close();在
MessageBox.Show("账号出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
上面也加一句
dr.Close();另附,这代码结构不合理
select Xsmm from xs where Xszh=
后面更新时候使用users
update users set Xsmm
是不是表名搞错啦
比如
using (sqlconnection conn = new sqlconnection(""))
{
conn.open();
sqlcommand cmd = new sqlcommand(conn);
using (sqldatareader dr = cmd.exectudatareader())
{
}
conn.close();
}
手写的, 单词错误不负责任
{
conn.open();
sqlcommand cmd = new sqlcommand(conn);
using (sqldatareader dr = cmd.exectudatareader())
{
......
dr.close();
}
conn.close();
}