0

我遇到了以下错误:

连接未关闭。连接的当前状态是打开当阅读器关闭时尝试调用元数据无效。

我试过这个:

while(dr.Read()){//and  set value}

但它不起作用。这就是它以其他一些有效的形式完成的方式。

当我在我的应用程序中按 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
4

3 回答 3

0

只需在打开数据库连接之前插入一个代码

 if (con.State == ConnectionState.Open)
        con.Close();

 con.Open();

还添加以下代码

dr.close();
  dr.dispose();

希望这可以帮助你

于 2013-06-21T05:18:55.137 回答
0

我的问题现在解决了,因为我可以替换SqlDataReaderDataTableconnection错误也修复了 thx 给你的想法,但仍然是问题 ySqlDataReader不起作用

于 2013-06-21T07:04:51.093 回答
0

尝试使用Using 语句进行数据库连接,这有助于管理资源。

using 语句与实现IDisposable的类型相结合。

using (var con = new SqlConnection(//connection string)
  {
    using (var cmd = new SqlCommand(storedProcname, con))
     {
       try{
           con.open();
           //data reader code
       }
       catch
       {

       }
     }
  }
于 2013-06-21T05:47:12.993 回答