C# 4.0 命令超时,返回响应的时间更长
我正在执行大型查询,所以我的应用程序抛出超时错误。一些线程建议添加命令超时,但在添加这些行之后,需要更长的时间才能获得响应,您知道我的代码中缺少了什么原因吗C# 4.0 命令超时,返回响应的时间更长,c#-4.0,C# 4.0,我正在执行大型查询,所以我的应用程序抛出超时错误。一些线程建议添加命令超时,但在添加这些行之后,需要更长的时间才能获得响应,您知道我的代码中缺少了什么原因吗 public int CreateRecord(string theCommand, DataSet theInputData) { int functionReturnValue = 0; int retVal = 0; SqlParameter objSqlParameter
public int CreateRecord(string theCommand, DataSet theInputData)
{
int functionReturnValue = 0;
int retVal = 0;
SqlParameter objSqlParameter = default(SqlParameter);
DataSet dsParameter = new DataSet();
int i = 0;
try
{
//Set the command text (stored procedure name or SQL statement).
mobj_SqlCommand.CommandTimeout = 120;
mobj_SqlCommand.CommandText = theCommand;
mobj_SqlCommand.CommandType = CommandType.StoredProcedure;
for (i = 0; i <= (theInputData.Tables.Count - 1); i++)
{
if (theInputData.Tables[i].Rows.Count > 0)
{
dsParameter.Tables.Add(theInputData.Tables[i].Copy());
}
}
objSqlParameter = new SqlParameter("@theXmlData", SqlDbType.Text);
objSqlParameter.Direction = ParameterDirection.Input;
objSqlParameter.Value = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" + dsParameter.GetXml();
//Attach to the parameter to mobj_SqlCommand.
mobj_SqlCommand.Parameters.Add(objSqlParameter);
//Finally, execute the command.
retVal = (int)mobj_SqlCommand.ExecuteScalar();
//Detach the parameters from mobj_SqlCommand, so it can be used again.
mobj_SqlCommand.Parameters.Clear();
functionReturnValue = retVal;
}
catch (Exception ex)
{
throw new System.Exception(ex.Message);
}
finally
{
//Clean up the objects created in this object.
if (mobj_SqlConnection.State == ConnectionState.Open)
{
mobj_SqlConnection.Close();
mobj_SqlConnection.Dispose();
mobj_SqlConnection = null;
}
if ((mobj_SqlCommand != null))
{
mobj_SqlCommand.Dispose();
mobj_SqlCommand = null;
}
if ((mobj_SqlDataAdapter != null))
{
mobj_SqlDataAdapter.Dispose();
mobj_SqlDataAdapter = null;
}
if ((dsParameter != null))
{
dsParameter.Dispose();
dsParameter = null;
}
objSqlParameter = null;
}
return functionReturnValue;
}
public int CreateRecord(字符串命令、数据集和输入数据)
{
int functionReturnValue=0;
int-retVal=0;
SqlParameter objsqlparmeter=默认值(SqlParameter);
数据集参数=新数据集();
int i=0;
尝试
{
//设置命令文本(存储过程名称或SQL语句)。
mobj_SqlCommand.CommandTimeout=120;
mobj_SqlCommand.CommandText=命令;
mobj_SqlCommand.CommandType=CommandType.StoredProcess;
对于(i=0;i=0)
{
添加(输入数据.Tables[i].Copy());
}
}
objSqlParameter=newsqlparameter(“@theXmlData”,SqlDbType.Text);
objSqlParameter.Direction=ParameterDirection.Input;
objSqlParameter.Value=”“+dsParameter.GetXml();
//将参数附加到mobj_SqlCommand。
mobj_SqlCommand.Parameters.Add(objSqlParameter);
//最后,执行命令。
retVal=(int)mobj_SqlCommand.ExecuteScalar();
//从mobj_SqlCommand中分离参数,以便再次使用。
mobj_SqlCommand.Parameters.Clear();
functionReturnValue=retVal;
}
捕获(例外情况除外)
{
抛出新系统异常(例如消息);
}
最后
{
//清理在此对象中创建的对象。
if(mobj_SqlConnection.State==ConnectionState.Open)
{
mobj_SqlConnection.Close();
mobj_SqlConnection.Dispose();
mobj_SqlConnection=null;
}
if((mobj_SqlCommand!=null))
{
mobj_SqlCommand.Dispose();
mobj_SqlCommand=null;
}
if((mobj_SqlDataAdapter!=null))
{
mobj_SqlDataAdapter.Dispose();
mobj_SqlDataAdapter=null;
}
如果((dsParameter!=null))
{
dsParameter.Dispose();
dsParameter=null;
}
objSqlParameter=null;
}
返回函数returnvalue;
}
这是正确的。当您在30秒内收到Tessponce时,您的会话将被中止并报告超时。现在它可以工作2分钟。也许你可以先添加一个虚拟的select/数据检索,这样调用者就会收到一些反馈。最理想的情况是,超时时间从启动SqlCommand调用到从该调用中接收数据的时间开始计算。或者,只需增加超时:)