我注意到sqlWrite.ExecuteNonQuery();
在几秒钟内执行 200 次插入查询后我的代码错误。我一直认为这using
将确保资源被正确地重用,并且不需要做任何事情。这是我第一次遇到这个错误,我已经处理 sql/c# 近 3 年了,做不同的事情。
using (SqlConnection varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
{
using (var sqlWrite = new SqlCommand(preparedCommand, varConnection))
{
sqlWrite.Parameters.AddWithValue("@var_agr_fname", var_agr_fname == "" ? (object) DBNull.Value : var_agr_fname);
sqlWrite.ExecuteNonQuery();
}
}
public static SqlConnection sqlConnectOneTime(string varSqlConnectionDetails)
{
var sqlConnection = new SqlConnection(varSqlConnectionDetails);
try
{
sqlConnection.Open();
}
catch
{
DialogResult result = MessageBox.Show(new Form {TopMost = true},
"Błąd połączenia z bazą danych. Czy chcesz spróbować nawiązac połączenie ponownie?",
"Błąd połączenia (000001)",
MessageBoxButtons.YesNo,
MessageBoxIcon.Stop);
if (result == DialogResult.No)
{
if (Application.MessageLoop)
{
Application.Exit(); // Use this since we are a WinForms app
}
else
{
Environment.Exit(1); // Use this since we are a console app
}
}
else
{
sqlConnection = sqlConnectOneTime(varSqlConnectionDetails);
}
}
return sqlConnection;
}
错误信息:A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)
考虑到此错误的建议,我应该使用SqlConnection.ClearAllPools();
它来确保正确重置或丢弃连接。所以我可以使用它,但问题是在哪里以及何时使用它?如何知道限制是否会被打破?极限在哪里?在 50 / 150 / 200 ?还是我应该在循环中每次都使用它?