sql server -在一个方法中使用ArrayList绑定两个sql语句.c#)
本文关键字:sql ArrayList 绑定 语句 两个 方法 server 一个 | 更新日期: 2023-09-27 17:54:42
我想显示基于StudentID
的SemesterID
列表,如下所示。
在comboBox1我已经列出了StudentIDs
,但给我一个错误:
错误#1
错误# 2Con未关闭。
数据读取器已在使用…
public void bindStudentID()
{
try
{
ArrayList a = new ArrayList();
con.Open();
SqlCommand cmd = new SqlCommand("SELECT studentId FROM tbStudent", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
a.Add(dr["studentId"]);
}
comboBox1.DataSource = a;
ArrayList aa = new ArrayList();
SqlCommand cmdd = new SqlCommand("SELECT SemesterID FROM tbSemester Where StudentID='" + comboBox1.Text + "'", con);
SqlDataReader drr = cmd.ExecuteReader();
while (drr.Read())
{
aa.Add(drr["SemesterID"]);
comboBox2.DataSource = aa;
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
您忘记像这样关闭阅读器:
// Call Close when done reading.
reader.Close();
尝试使用这种语法连接,它更安全:
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
ReadSingleRow((IDataRecord)reader);
}
// Call Close when done reading.
reader.Close();
}
我认为这段代码将工作良好。这段代码是不言自明的。您正在创建一个SqlDataReader对象而没有处理之前的对象。
public void bindStudentID()
{
try
{
ArrayList a = new ArrayList();
con.Open();
SqlCommand cmd = new SqlCommand("SELECT studentId FROM tbStudent", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
a.Add(dr["studentId"]);
}
comboBox1.DataSource = a;
ArrayList aa = new ArrayList();
SqlCommand cmdd = new SqlCommand("SELECT SemesterID FROM tbSemester Where StudentID='" + comboBox1.Text + "'", con);
dr = null;
dr = cmd.ExecuteReader();
while (dr.Read())
{
aa.Add(drr["SemesterID"]);
}
comboBox2.DataSource = aa;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
如果我帮助了,请标记为答案
检查这个,
SqlDataReader drr = cmd.ExecuteReader();
应该是
SqlDataReader drr = cmdd.ExecuteReader();
您正在使用cmd而不是cmdd命令
MSDN显示:
在使用完DataReader对象后,应该始终调用Close方法。如果您的命令包含输出参数或返回值,则在关闭DataReader之前它们将不可用。请注意,当一个数据阅读器打开时,连接仅由该数据阅读器使用。在原始数据读取器关闭之前,您不能为连接执行任何命令,包括创建另一个数据读取器。
所以基于MSDN,试试这个:
public void bindStudentID()
{
//instantiating a ArrayList object does not need to be included inside Try/Catch block
//try
//{
ArrayList a = new ArrayList();
try
{
//Before opening the connection make sure that connection's current state is not open, otherwise you will get exception
//con.Open();
if (con.State != ConnectionState.Closed)
{
con.Close();
}
con.Open();
SqlCommand cmd = new SqlCommand("SELECT studentId FROM tbStudent", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
a.Add(dr["studentId"]);
}
comboBox1.DataSource = a;
//You should close data reader before using it again or defining new one
dr.Close();
ArrayList aa = new ArrayList();
//No need to define new SqlCommand object and you can use the prev one
cmd = new SqlCommand("SELECT SemesterID FROM tbSemester Where StudentID='" + comboBox1.Text + "'", con);
//No need to define new SqlDataREader object and you can use the prev one
dr = cmd.ExecuteReader();
while (dr.Read())
{
aa.Add(dr["SemesterID"]);
//I think this should be stated out of the while block
//comboBox2.DataSource = aa;
}
comboBox2.DataSource = aa;
//You should close data reader before using it again or defining new one
dr.Close();
//Close the connection in finally block
//con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//You should also close the connection even if an exception raised
con.Close();
}
}