超时时间已到。在操作完成之前超时时间已过或服务器未响应。 (.Net SqlClient Data Provider)
使用VS调试几次项目后,使用SQL Server Management Studio管理数据库时,使用SA登录就会出现这个错误,当然,如果项目中的数据库连接字符串中使用的sa验证,那么项目也会连不到数据库
在做一个小东西的时候出现了这个问题,就是使用VS调试几次项目后,使用SQL Server Management Studio管理数据库时,使用SA登录就会出现这个错误,当然,如果项目中的数据库连接字符串中使用的sa验证,那么项目也会连不到数据库的.可是如果是在 Server Management Studio和项目中使用Windows身份验证,就没有任何问题.
提示错误消息如下
这里有一个帖子(点我看帖子)反映的情况好像和我的类似,可是他没有说具体的解决方案,仅仅提到优化了一下存储过程
CSDN这里有一个类似问题(点我看原文)大意就是说要把TimeOut时间设的长一点,可是我的不是这个原因
初步分析原因为对MSSQL操作时连接超时,知道这事,以前没留意,大概是在配置文件中设置连接时限,在网上找了下解决方法,大多说在数据库连接字符串里解决
到底是怎么回事呢,现在也没有搞定,所以我只能是认为我的那个DBAcess类的问题了,所以把它贴出来大家分析,看看原因,希望有人帮我解答一下,代码比较长,不想在网页上看的我提供了下载 (点我下载)
提示错误消息如下
Code
超时时间已到。在操作完成之前超时时间已过或服务器未响应。 (.Net SqlClient Data Provider)
------------------------------
有关帮助信息,请单击: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=-2&LinkId=20476
------------------------------
服务器名称: ZY-CQU
错误号: -2
严重性: 11
状态: 0
------------------------------
程序位置:
#region 程序信息
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
在 System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
在 System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
在 System.Data.SqlClient.TdsParserStateObject.ReadPacket(Int32 bytesExpected)
在 System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
在 System.Data.SqlClient.TdsParserStateObject.ReadByte()
在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
在 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
在 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
在 System.Data.SqlClient.SqlConnection.Open()
在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ObjectExplorer.ValidateConnection(UIConnectionInfo ci, IServerType server)
在 Microsoft.SqlServer.Management.UI.ConnectionDlg.Connector.ConnectionThreadUser()
#endregion 程序信息
超时时间已到。在操作完成之前超时时间已过或服务器未响应。 (.Net SqlClient Data Provider)
------------------------------
有关帮助信息,请单击: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=-2&LinkId=20476
------------------------------
服务器名称: ZY-CQU
错误号: -2
严重性: 11
状态: 0
------------------------------
程序位置:
#region 程序信息
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
在 System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
在 System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
在 System.Data.SqlClient.TdsParserStateObject.ReadPacket(Int32 bytesExpected)
在 System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
在 System.Data.SqlClient.TdsParserStateObject.ReadByte()
在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
在 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
在 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
在 System.Data.SqlClient.SqlConnection.Open()
在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ObjectExplorer.ValidateConnection(UIConnectionInfo ci, IServerType server)
在 Microsoft.SqlServer.Management.UI.ConnectionDlg.Connector.ConnectionThreadUser()
#endregion 程序信息
初步推断可能是由于我的那个DBAcess类没有正确的关闭数据库连接吧.在网上搜了一通,也没有什么实质性的进展
这里有一个帖子(点我看帖子)反映的情况好像和我的类似,可是他没有说具体的解决方案,仅仅提到优化了一下存储过程
CSDN这里有一个类似问题(点我看原文)大意就是说要把TimeOut时间设的长一点,可是我的不是这个原因
初步分析原因为对MSSQL操作时连接超时,知道这事,以前没留意,大概是在配置文件中设置连接时限,在网上找了下解决方法,大多说在数据库连接字符串里解决
SqlConnection con = new SqlConnection("server=.;database=myDB;uid=sa;pwd=password;")
改为:
SqlConnection con = new SqlConnection("server=.;database=myDB;uid=sa;pwd=password;Connect Timeout=500")
似乎没效果。依然运行30秒即报超时!
突然感觉似乎应该可以在连接数据库代码中指明,式了下con的属性,有个ConnectionTimeout,
SqlConnection con = new SqlConnection("server=.;database=myDB;uid=sa;pwd=;");
con.ConnectionTimeout = 180;//报错,属性ConnectionTimeout 为只读!
con.ConnectionTimeout = 180;//报错,属性ConnectionTimeout 为只读!
尝试失败,再接着看command对象属性,发现其也有类似属性!CommandTimeout设置一下:
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = 180;
cmd.CommandTimeout = 180;
再运行,即解决,这里设置的时间的180秒,即三分钟!可根据需要设置,如果过长,也可以设置为0,当此属性设置为0时表示不限制时间。此属性值应该慎用。还需要在Web.config配置文件中设置http请求运行时限间
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="720" />
</system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="720" />
</system.web>
这里设置的为720秒,前面的属性maxRequestLength一般用于用户上传文件限制大小!默认一般为4096 KB (4 MB)。
到底是怎么回事呢,现在也没有搞定,所以我只能是认为我的那个DBAcess类的问题了,所以把它贴出来大家分析,看看原因,希望有人帮我解答一下,代码比较长,不想在网页上看的我提供了下载 (点我下载)
Code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/**//// <summary>
/// 数据访问层类,提供数据库的连接以及sql语句的执行
/// </summary>
public class DBAccess
{
private IDbCommand cmd = new SqlCommand();
private string strConnectionString = "";
private bool handleErrors = false;
private string strLastError = "";
public DBAccess()
{
ConnectionStringSettings objConnectionStringSettings = ConfigurationManager.ConnectionStrings["conn"];
strConnectionString = objConnectionStringSettings.ConnectionString;
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = strConnectionString;
cmd.Connection = cnn;
//cmd.CommandTimeout=
}
public void SetCmdType(CommandType type)
{
cmd.CommandType = type;
}
public string ConnectionString
{
get
{
return strConnectionString;
}
set
{
strConnectionString = value;
}
}
public bool HandleExceptions
{
get
{
return handleErrors;
}
set
{
handleErrors = value;
}
}
public string LastError
{
get
{
return strLastError;
}
}
/**//// <summary>
/// 执行一条SQL语句或者存储过程,返回一个IDataReader
/// 执行参数ExecuteReader(CommandBehavior.CloseConnection)
/// 如果关闭DataReader,则相关联的Connection也关闭
/// </summary>
/// <returns>返回一个IDataReader</returns>
public IDataReader ExecuteReader()
{
IDataReader reader = null;
try
{
this.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}
/**//// <summary>
/// 执行一条SQL语句或者存储过程,返回一个IDataReader
/// 如果关闭DataReader,则相关联的Connection也关闭
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回一个IDataReader</returns>
public IDataReader ExecuteReader(string commandtext)
{
IDataReader reader = null;
try
{
cmd.CommandText = commandtext;
reader = this.ExecuteReader();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}
/**//// <summary>
/// 执行一条SQL语句
/// </summary>
/// <returns>返回所影响的行数</returns>
public int ExecuteNonQuery()
{
int i = -1;
try
{
this.Open();
i = cmd.ExecuteNonQuery();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return i;
}
/**//// <summary>
/// 执行一条SQL语句或者存储过程,该语句由参数给出
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回所影响的行数</returns>
public int ExecuteNonQuery(string commandtext)
{
int i = -1;
try
{
cmd.CommandText = commandtext;
i = this.ExecuteNonQuery();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return i;
}
/**//// <summary>
/// 执行CMD对象,返回DataSet
/// </summary>
/// <returns>返回DataSet</returns>
public DataSet ExecuteDataSet()
{
SqlDataAdapter da = null;
DataSet ds = null;
try
{
da = new SqlDataAdapter();
da.SelectCommand = (SqlCommand)cmd;
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return ds;
}
/**//// <summary>
/// 执行SQL或者存储过程,返回DataSet
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回DataSet</returns>
public DataSet ExecuteDataSet(string commandtext)
{
DataSet ds = null;
try
{
cmd.CommandText = commandtext;
ds = this.ExecuteDataSet();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return ds;
}
/**//// <summary>
/// 执行CMD对象,返回结果的第一行,一般用来执行聚合函数
/// </summary>
/// <returns>返回查询结果的第一行</returns>
public object ExecuteScalar()
{
object obj = null;
try
{
this.Open();
obj = cmd.ExecuteScalar();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return obj;
}
/**//// <summary>
/// 执行SQL或者存储过程,返回结果的第一行,一般用来执行聚合函数
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回查询结果的第一行</returns>
public object ExecuteScalar(string commandtext)
{
object obj = null;
try
{
cmd.CommandText = commandtext;
obj = this.ExecuteScalar();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return obj;
}
/**//// <summary>
/// 对CMD对象添加Parameter
/// </summary>
/// <param name="paramname">Parameter名字,形如@Name</param>
/// <param name="paramvalue">Parameter值,如zhangyue</param>
public void AddParameter(string paramname, object paramvalue)
{
SqlParameter param = new SqlParameter(paramname, paramvalue);
cmd.Parameters.Add(param);
}
/**//// <summary>
/// 将一个Parameter对象添加到CMD对象中
/// </summary>
/// <param name="param">Parameter对象</param>
public void AddParameter(IDataParameter param)
{
cmd.Parameters.Add(param);
}
private void Open()
{
try
{
cmd.Connection.Open();
}
catch (Exception e)
{
if (handleErrors)
strLastError = "连接数据库失败";
else
throw;
}
}
private void Close()
{
//if (cmd.Connection.State != "Closed")
cmd.Connection.Close();
}
public void Dispose()
{
cmd.Dispose();
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/**//// <summary>
/// 数据访问层类,提供数据库的连接以及sql语句的执行
/// </summary>
public class DBAccess
{
private IDbCommand cmd = new SqlCommand();
private string strConnectionString = "";
private bool handleErrors = false;
private string strLastError = "";
public DBAccess()
{
ConnectionStringSettings objConnectionStringSettings = ConfigurationManager.ConnectionStrings["conn"];
strConnectionString = objConnectionStringSettings.ConnectionString;
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = strConnectionString;
cmd.Connection = cnn;
//cmd.CommandTimeout=
}
public void SetCmdType(CommandType type)
{
cmd.CommandType = type;
}
public string ConnectionString
{
get
{
return strConnectionString;
}
set
{
strConnectionString = value;
}
}
public bool HandleExceptions
{
get
{
return handleErrors;
}
set
{
handleErrors = value;
}
}
public string LastError
{
get
{
return strLastError;
}
}
/**//// <summary>
/// 执行一条SQL语句或者存储过程,返回一个IDataReader
/// 执行参数ExecuteReader(CommandBehavior.CloseConnection)
/// 如果关闭DataReader,则相关联的Connection也关闭
/// </summary>
/// <returns>返回一个IDataReader</returns>
public IDataReader ExecuteReader()
{
IDataReader reader = null;
try
{
this.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}
/**//// <summary>
/// 执行一条SQL语句或者存储过程,返回一个IDataReader
/// 如果关闭DataReader,则相关联的Connection也关闭
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回一个IDataReader</returns>
public IDataReader ExecuteReader(string commandtext)
{
IDataReader reader = null;
try
{
cmd.CommandText = commandtext;
reader = this.ExecuteReader();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}
/**//// <summary>
/// 执行一条SQL语句
/// </summary>
/// <returns>返回所影响的行数</returns>
public int ExecuteNonQuery()
{
int i = -1;
try
{
this.Open();
i = cmd.ExecuteNonQuery();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return i;
}
/**//// <summary>
/// 执行一条SQL语句或者存储过程,该语句由参数给出
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回所影响的行数</returns>
public int ExecuteNonQuery(string commandtext)
{
int i = -1;
try
{
cmd.CommandText = commandtext;
i = this.ExecuteNonQuery();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return i;
}
/**//// <summary>
/// 执行CMD对象,返回DataSet
/// </summary>
/// <returns>返回DataSet</returns>
public DataSet ExecuteDataSet()
{
SqlDataAdapter da = null;
DataSet ds = null;
try
{
da = new SqlDataAdapter();
da.SelectCommand = (SqlCommand)cmd;
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return ds;
}
/**//// <summary>
/// 执行SQL或者存储过程,返回DataSet
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回DataSet</returns>
public DataSet ExecuteDataSet(string commandtext)
{
DataSet ds = null;
try
{
cmd.CommandText = commandtext;
ds = this.ExecuteDataSet();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return ds;
}
/**//// <summary>
/// 执行CMD对象,返回结果的第一行,一般用来执行聚合函数
/// </summary>
/// <returns>返回查询结果的第一行</returns>
public object ExecuteScalar()
{
object obj = null;
try
{
this.Open();
obj = cmd.ExecuteScalar();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return obj;
}
/**//// <summary>
/// 执行SQL或者存储过程,返回结果的第一行,一般用来执行聚合函数
/// </summary>
/// <param name="commandtext">存储过程名或者SQL语句</param>
/// <returns>返回查询结果的第一行</returns>
public object ExecuteScalar(string commandtext)
{
object obj = null;
try
{
cmd.CommandText = commandtext;
obj = this.ExecuteScalar();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return obj;
}
/**//// <summary>
/// 对CMD对象添加Parameter
/// </summary>
/// <param name="paramname">Parameter名字,形如@Name</param>
/// <param name="paramvalue">Parameter值,如zhangyue</param>
public void AddParameter(string paramname, object paramvalue)
{
SqlParameter param = new SqlParameter(paramname, paramvalue);
cmd.Parameters.Add(param);
}
/**//// <summary>
/// 将一个Parameter对象添加到CMD对象中
/// </summary>
/// <param name="param">Parameter对象</param>
public void AddParameter(IDataParameter param)
{
cmd.Parameters.Add(param);
}
private void Open()
{
try
{
cmd.Connection.Open();
}
catch (Exception e)
{
if (handleErrors)
strLastError = "连接数据库失败";
else
throw;
}
}
private void Close()
{
//if (cmd.Connection.State != "Closed")
cmd.Connection.Close();
}
public void Dispose()
{
cmd.Dispose();
}
}