ExecuteReader 要求已打开且可用的 Connection。连接的当前状态为已关闭。请问大佬知道这个是什么原因吗?
源代码如下:
string name = textBox1.Text.Trim();
string password1 = textBox2.Text.Trim();
string password2 = textBox3.Text.Trim();
string Name = textBox4.Text.Trim();
string number = textBox5.Text.Trim();
if (name == “”)
{
MessageBox.Show(“用户名不能为空!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (password1 != password2)
{
MessageBox.Show(“两次输入的密码不一致!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con = new SqlConnection();
con.ConnectionString = “Data source=(local);Initial Catalog=Management;Integrated Security=True”; SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select username from manager";
cmd.Connection = con;
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
string username = reader["username"].ToString();
if (name == username)
{
MessageBox.Show("用户名已存在!", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sql = "insert into manager(username,pwd,name,ID)values(" + name + "," + password1 + "," + Name + "," + number + ")";
try
{
SqlCommand comm = new SqlCommand(sql, con);
int count = comm.ExecuteNonQuery();
if (count >= 0)
{
MessageBox.Show("注册成功,请前往登录", "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
MessageBox.Show("注册失败", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
con.Close();
}
Form1 a = new Form1();
a.Show();
this.Visible=false; }
} }
}
---------------------
作者:Numb6
来源:CSDN
原文:https://blog.csdn.net/weixin_45013749/article/details/90085080
版权声明:本文为博主原创文章,转载请附上博文链接!
源代码如下:
string name = textBox1.Text.Trim();
string password1 = textBox2.Text.Trim();
string password2 = textBox3.Text.Trim();
string Name = textBox4.Text.Trim();
string number = textBox5.Text.Trim();
if (name == “”)
{
MessageBox.Show(“用户名不能为空!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (password1 != password2)
{
MessageBox.Show(“两次输入的密码不一致!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con = new SqlConnection();
con.ConnectionString = “Data source=(local);Initial Catalog=Management;Integrated Security=True”; SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select username from manager";
cmd.Connection = con;
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
string username = reader["username"].ToString();
if (name == username)
{
MessageBox.Show("用户名已存在!", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sql = "insert into manager(username,pwd,name,ID)values(" + name + "," + password1 + "," + Name + "," + number + ")";
try
{
SqlCommand comm = new SqlCommand(sql, con);
int count = comm.ExecuteNonQuery();
if (count >= 0)
{
MessageBox.Show("注册成功,请前往登录", "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
MessageBox.Show("注册失败", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
con.Close();
}
Form1 a = new Form1();
a.Show();
this.Visible=false; }
} }
}
---------------------
作者:Numb6
来源:CSDN
原文:https://blog.csdn.net/weixin_45013749/article/details/90085080
版权声明:本文为博主原创文章,转载请附上博文链接!
解决方案 »
- datagridview合并单元格并保存
- C# 怎么获取刚刚打开的那个窗体的句柄啊
- 用DotNetBar的TabControl制作用户控件usercontrol时遇到的问题!!!!求高手
- C# 文件按时间分组
- 想请教一下Web服务器 这个东西??
- XP中iis5怎样升级到IIS6.0
- 在cs文件中,用javascript写数据库操作,如何实现??
- 一直在等你!请问各位高人,怎么在ListView中添加背景图。急切的盼望你的出现!
- WPF,这个对齐方式,帮我弄迷糊了
- 怎样把DataGrid中数据导出成EXCEL。一定要C#的语言来写哈!!!
- 请教如何制作旋钮控件,一种分段式的,一种设置数值的
- 如何用A软件检测B软件的弹出窗口呢?
在这行代码之前,加上con.open,只有打卡连接之后,才能read
if(con.connectingstate==connetingstate.close)
{
con.open();
}
string name = textBox1.Text.Trim();
string password1 = textBox2.Text.Trim();
string password2 = textBox3.Text.Trim();
string Name = textBox4.Text.Trim();
string number = textBox5.Text.Trim();
if (name == “”)
{
MessageBox.Show(“用户名不能为空!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (password1 != password2)
{
MessageBox.Show(“两次输入的密码不一致!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con = new SqlConnection();
con.ConnectionString = “Data source=(local);Initial Catalog=Management;Integrated Security=True”; SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select username from manager";
cmd.Connection = con;
con.open();
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
string username = reader["username"].ToString();
if (name == username)
{
MessageBox.Show("用户名已存在!", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sql = "insert into manager(username,pwd,name,ID)values(" + name + "," + password1 + "," + Name + "," + number + ")";
try
{
SqlCommand comm = new SqlCommand(sql, con);
int count = comm.ExecuteNonQuery();
if (count >= 0)
{
MessageBox.Show("注册成功,请前往登录", "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
MessageBox.Show("注册失败", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
con.Close();
}
Form1 a = new Form1();
a.Show();
this.Visible=false; }
} }
}
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.CommandTimeout = Times;
MySqlDataReader rows = cmd.ExecuteReader();
return rows;
}
catch (MySql.Data.MySqlClient.MySqlException e)
{
connection.Close();
throw e;
}
}
}
}把取数据和逻辑分开
con.open
{
con.Close();
}删掉;然后在循环外边再Close;