【问题标题】:C# System.InvalidOperationException on sqlconnection closeC# System.InvalidOperationException on sqlconnection close
【发布时间】:2021-06-27 08:27:06
【问题描述】:

System.InvalidOperationException:超时已过期。
在从池中获取连接之前超时时间已过。 这可能是因为所有池连接都在使用中并且已达到最大池大小。

private void uploadDbButton_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor;

    countLabel.Text = autoid("Olvasasok", "olvasas_szama");
 
        string query, itemVlaue = "";
       
        SqlCommand cmd;
        
        if(kiadas.Checked)
        {
            radio = "K";
        }
        else if (bevetelezes.Checked)
        {
            radio = "B";
        }

   


        if (inventoryList.Items.Count > 0)
        {
            for (int i = 0; i < inventoryList.Items.Count; i++)
            {
                connect = new SqlConnection(conStr);
                DateTime time = DateTime.Now;
                String format = "yyyy-MM-dd";
                DateTime ido = DateTime.Now;
                String forma = "HH:mm:ss";
             
            
                itemVlaue = inventoryList.Items[i].Text;
                query = "INSERT INTO Olvasasok (olvasas_szama, rfid_tag, datum, ido, irany)VALUES ('" + countLabel.Text +"', '" + itemVlaue + "','" + DateTime.Now.ToString(format) +"' , '" + DateTime.Now.ToString(forma) + "' , '" + radio +"')";
                cmd = new SqlCommand(query, connect);

                

                if (connect.State == ConnectionState.Closed)
                {
                    connect.Open();
                }


                cmd.ExecuteNonQuery();
            }

            connect.Close();
            functionCallStatusLabel.Text = "Feltöltés sikerült...";

        }
        else
        {
            MessageBox.Show("Csatlakozz rá egy olvasóra...");

        }
        inventoryList.Items.Clear();
        m_TagTable.Clear();
        m_TagTotalCount = 0;
        totalTagValueLabel.Text = "0(0)";
        this.uploadDbButton.Enabled = false;


    }

【问题讨论】:

  • 您好,请问您的具体问题是什么?
  • 这能回答你的问题吗? C# Data Connections Best Practice? 始终使用using 处理 SQL 连接和读取器对象,并始终正确参数化查询
  • 我有一个阅读器程序,当我想上传200-300到数据库记录时它会打印出错误

标签: c# sql connection timeout invalidoperationexception


【解决方案1】:

潜在地,很多 SqlConnection 可以打开并且永远不会关闭:

if (inventoryList.Items.Count > 0)
{
    for (int i = 0; i < inventoryList.Items.Count; i++)
    {
        // For each loop, create new SqlConnection object
        connect = new SqlConnection(conStr);
        // Already true, because the connect is new SqlConnection object
        if (connect.State == ConnectionState.Closed)
            connect.Open();
        ...
    }
    //Close only the last SqlConnection
    connect.Close();
}

也许您可以尝试将SqlConnection 封装在using 中:

if (inventoryList.Items.Count > 0)
{
    // Create one SqlConnection
    using(var connect = new SqlConnection(conStr))
    {
        // Open one SqlConnection
        connect.Open();
        for (int i = 0; i < inventoryList.Items.Count; i++)
        {
            ...
        }
    }
    // End of using, the SqlConnection is automatically closed
}

【讨论】:

  • 感谢您的回复............线程不会更好?
  • 并行化?您可以使用 Thread,但更喜欢使用 TPL
  • 你能帮我如何在我的代码中使用 TPL 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多