winforms 读取器关闭时尝试调用元数据无效c# winform

dgiusagp  于 2022-11-25  发布在  C#
关注(0)|答案(3)|浏览(166)

这是我删除的编辑过的代码,请尝试捕捉,看看我在哪里得到了异常。我还标记了我编辑代码的地方。这段代码让我很紧张,我一直在谷歌上搜索这个问题,并尝试了我发现的一切。我检查了每个连接,然后这段代码似乎没有问题。每个连接都关闭了。

queryString = "SELECT * FROM product WHERE prd_code = @c OR prd_name=@pn ";
    SqlCommand command = new SqlCommand(queryString, con);
    command.Parameters.AddWithValue("@c", Convert.ToInt32(txtpCode.Text));
    command.Parameters.AddWithValue("@pn", txtpName.Text.ToString());
    con.Open();
    //edited here
    using (SqlDataReader dr = command.ExecuteReader())
    {
    // and here
    if (dr != null && fr.HasRows && dr.Read() == true)
    {
        if (txtpCode.Text == "")
        {
            txtpCode.Text = dr["prd_code"].ToString();
        }
        else if (txtpName.Text == "")
        {
            txtpName.Text = dr["prd_name"].ToString();
        }            
        txtpCompany.Text = dr["prd_company"].ToString();
        txtUnitPrice.Text = dr["prd_price"].ToString();
        txtDiscount.Text = dr["prd_dis"].ToString();
        txtFinalRate.Text = dr["prd_final"].ToString();
        } 
        else
        {
            MessageBox.Show("No such record exists");

            if (txtpName.Text == "")
            {
                txtpCode.Text = "";
            }
            else if (txtpCode.Text == "")
            {
                 txtpName.Text = "";
            }
            con.Close();
        }

    }
    }
u7up0aaq

u7up0aaq1#

using (var reader = command.ExecuteReader())
      {
           if (reader.HasRows)
              {
                 if(reader.Read())
                 {

                   if (txtpCode.Text == "")
                   {
                       txtpCode.Text = dr["prd_code"].ToString();
                   }
                   else if (txtpName.Text == "")
                   {
                       txtpName.Text = dr["prd_name"].ToString();
                   }
                   txtpCompany.Text = dr["prd_company"].ToString();
                   txtUnitPrice.Text = dr["prd_price"].ToString();
                   txtDiscount.Text = dr["prd_dis"].ToString();
                   txtFinalRate.Text = dr["prd_final"].ToString();
                }
            }
      }

查看What is the C# Using block and why should I use it?

eqoofvh9

eqoofvh92#

您应该使用以下内容更新finally块:

finally
{
    dr.close();
    con.Close();
}

此外,你应该看看其他SO和其他论坛线程,其中类似的问题已经讨论过。
invalid-attempt-to-call-metadata-when-reader-is-closed
InvalidplusattemptplustopluscallplusMetaDatapluswh

编辑:

在finally块中释放资源总是很好的。我建议在try catch外面做dr * 声明 *,然后在finally块中释放它。
也有可能您的查询未返回任何记录,因此请使用短路技术来避免此错误:

if (dr != null && dr.HasRows && dr.Read() == true)
{
    // TODO : do your stuff here
}

编辑:

查看连接和阅读器关闭的线路:

queryString = "SELECT * FROM product WHERE prd_code = @c OR prd_name=@pn ";
SqlCommand command = new SqlCommand(queryString, con);
command.Parameters.AddWithValue("@c", Convert.ToInt32(txtpCode.Text));
command.Parameters.AddWithValue("@pn", txtpName.Text.ToString());
con.Open();

using (SqlDataReader dr = command.ExecuteReader())
{
    if (dr != null && dr.HasRows && dr.Read() == true)
    {
        if (txtpCode.Text == "")
        {
            txtpCode.Text = dr["prd_code"].ToString();
        }
        else if (txtpName.Text == "")
        {
           txtpName.Text = dr["prd_name"].ToString();
        }

        txtpCompany.Text = dr["prd_company"].ToString();
        UnitPrice.Text = dr["prd_price"].ToString();
        txtDiscount.Text = dr["prd_dis"].ToString();
        txtFinalRate.Text = dr["prd_final"].ToString();
    } 
    else
    {
        MessageBox.Show("No such record exists");

        if (txtpName.Text == "")
        {
            txtpCode.Text = "";
        }
        else if (txtpCode.Text == "")
        {
             txtpName.Text = "";
        }
     }  

    dr.Close();   // <-------------------- Look here
    con.Close;    // <-------------------- and here
}
velaa5lx

velaa5lx3#

我可以通过将MultipleActiveResultSets=True添加到连接问题中来修复问题。有关详细信息,请参阅:https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/multiple-active-result-sets-mars?redirectedfrom=MSDN

相关问题