连接的当前状态为打开错误
本文关键字:错误 状态 连接 | 更新日期: 2023-09-27 18:19:23
我遇到了以下错误:
连接未关闭。的连接的当前状态为打开。试图调用元数据无效当阅读器关闭时。
我试过了:
while(dr.Read()){//and set value}
但它不起作用。在其他一些有效的形式中也是这样的。
当我在应用程序中按Enter键时,此代码将运行。当我第一次按Enter键时,显示错误,但当我第二次按Enter键时,使用相同的数据,它工作了。
try
{
SqlCommand cmd = new SqlCommand("selectknittingdaily", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@m_id", SqlDbType.Int).Value = Convert.ToInt32(txt_id.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
dateTimePicker1.Text = dr[0].ToString();
textBox1.Text = dr[1].ToString();
machineno_combo.Text = dr[2].ToString();
guage_combo.Text = dr[3].ToString();
gsm_combo.Text = dr[4].ToString();
count_combo.Text = dr[5].ToString();
color_combo.Text = dr[6].ToString();
supplier_combo.Text = dr[7].ToString();
txt_rollsfrom.Text = dr[8].ToString();
txt_rollsto.Text = dr[9].ToString();
txt_rolls.Text = dr[10].ToString();
txt_totalweight.Text = dr[11].ToString();
quality_combo.Text = dr[12].ToString();
txt_cotton.Text = dr[13].ToString();
txt_polyster.Text = dr[14].ToString();
txt_elasthan.Text = dr[15].ToString();
txt_modal.Text = dr[16].ToString();
txt_tensil.Text = dr[17].ToString();
txt_organic.Text = dr[18].ToString();
border_combo.Text = dr[19].ToString();
}
dr.Close();
//con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
这是我的Sql存储过程
create proc selectknittingdaily
@m_id int
as
begin
select master_knittingdaily.date2,master_order.program_no,machine.machine_no,machine.guage,
machine.gsm,yarn.yarn_count,(select color.color_no from yarn LEFT JOIN color on yarn.color_id=color.color_id
where yarn.yarn_id=(select yarn_id from master_knittingdaily where m_id=@m_id))
,(select supplier_knitting.supplier_name from yarn LEFT JOIN supplier_knitting on
yarn.supplier_id=supplier_knitting.supplier_id
where yarn.yarn_id=(select yarn_id from master_knittingdaily where m_id=@m_id))
,master_knittingdaily.rolls_from,
master_knittingdaily.rolls_to,master_knittingdaily.total_rolls,master_knittingdaily.total_weight,
quality_knitting.quality_code,quality_knitting.cotton,quality_knitting.polyster,quality_knitting.elasthan,
quality_knitting.modal,quality_knitting.tensil,quality_knitting.organic,master_knittingdaily.border
from master_knittingdaily
LEFT JOIN master_order on master_knittingdaily.order_id=master_order.order_id
LEFT JOIN machine on master_knittingdaily.machine_id=machine.machine_id
LEFT JOIN yarn on master_knittingdaily.yarn_id=yarn.yarn_id
LEFT JOIN quality_knitting on master_knittingdaily.quality_id=quality_knitting.quality_id
where master_knittingdaily.m_id=@m_id
end
在打开数据库连接之前插入一个代码
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
同时添加下面的代码
dr.close();
dr.dispose();
希望这对你有帮助
尝试使用Using语句连接数据库,这有助于管理资源。
using语句与实现IDisposable的类型结合使用。
using (var con = new SqlConnection(//connection string)
{
using (var cmd = new SqlCommand(storedProcname, con))
{
try{
con.open();
//data reader code
}
catch
{
}
}
}
我的问题现在解决了,因为我可以代替
SqlDataReader
到DataTable
和connection
的错误也修复了谢谢给你的想法,但是仍然是问题,SqlDataReader
不工作