【发布时间】:2020-07-08 01:20:42
【问题描述】:
我有一个项目,当我尝试运行它并且数据测试很大时,我总是遇到连接超时。 我添加了“sqlCmd.CommandTimeout = 3600;”但仍然无法正常工作。 我做错了什么?
这是我的代码:
public void createCode(String ce, int ord, String beh, int wkd)
{
String strSql = "";
SqlCommand sqlCmd;
SqlConnection conexion = new SqlConnection(getConexion());
try
{
if (conexion.State != ConnectionState.Open)
conexion.Open();
//The insert works fine in sql server
strSql = "Insert into x with values";
sqlCmd = new SqlCommand(strSql, conexion);
sqlCmd.CommandTimeout = 3600;
sqlCmd.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception("Error creating Code. " + ex.Message);
}
finally
{
if (conexion.State == ConnectionState.Open)
conexion.Close();
}
}
【问题讨论】:
-
请提供错误消息和连接字符串(混淆其秘密部分)。区分两种不同的超时类型很重要:ConnectionTimeout and CommandTimeout。哪个超时会导致您的代码出错?
标签: c# sql connection-timeout