Connection的当前状态是打开错误

时间:2013-06-21 05:08:25

标签: c#

我遇到以下错误:

  

连接未关闭。该   Connection的当前状态为open无效尝试调用MetaData   读者关闭时

我试过了:

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

3 个答案:

答案 0 :(得分:0)

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

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

 con.Open();

还添加以下代码

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

希望这有助于你

答案 1 :(得分:0)

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

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

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

       }
     }
  }

答案 2 :(得分:0)

  

我的问题现在解决了,因为我可以将SqlDataReader替换为   DataTableconnection错误也可以解决问题   仍然是问题y SqlDataReader无效