我在大约 200 台机器上有一个简单的程序,可以记录用户打开的表单。每次打开表单时,都会打开一个 Sql 连接,插入一行,我想连接已关闭?我读了连接池默认打开,所以我想它并没有真正关闭它?我不允许调用 Web 服务或潜在的更好方法,所以我的问题是为什么会出现此错误,是否存在以及如何解决它的想法?还是SQL端的东西?也许我可以尝试更改设置?
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand(
"INSERT INTO LoggerLoanForm VALUES(@Session, @Form, @DateStamp, @LoanNumber)", connection))
{
command.Parameters.Add(new SqlParameter("Session", llf.SesssionId));
command.Parameters.Add(new SqlParameter("Form", llf.Form));
command.Parameters.Add(new SqlParameter("DateStamp", llf.DateStamp));
command.Parameters.Add(new SqlParameter("LoanNumber", llf.LoanNumber));
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
AppInsightHelper.TrackException(ex);
}
}
建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及 SQL Server 是否配置为允许远程连接。(提供程序:TCP 提供程序,错误:0 - 请求的名称有效,但找不到请求类型的数据。请求的名称有效,但未找到所请求类型的数据
该错误似乎在高峰时段发生得更多,所以我想没有足够的开放连接或与SQL服务器的某些东西?
怎么样
const int max_try = 5;
int i = max_try;
while (i-- > 0)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand(
"INSERT INTO LoggerLoanForm VALUES(@Session, @Form, @DateStamp, @LoanNumber)", connection))
{
command.Parameters.Add(new SqlParameter("Session", llf.SesssionId));
command.Parameters.Add(new SqlParameter("Form", llf.Form));
command.Parameters.Add(new SqlParameter("DateStamp", llf.DateStamp));
command.Parameters.Add(new SqlParameter("LoanNumber", llf.LoanNumber));
command.ExecuteNonQuery();
i = 0;
}
}
catch (Exception ex)
{
if (i == 0)
AppInsightHelper.TrackException(ex);
System.Threading.Thread.Sleep(50);
}
}
}