使用 Microsoft.ApplicationBlocks.Data SqlHelper 查询超时以及解决方案

时间:2023-12-22 12:56:26

 提示: 后面附有文件,不喜欢看吐槽的,直接到文章结尾下载

摘要:Data Access Application Block 是一个 .NET 组件,包含优化的数据访问代码,可以帮助用户调用存储过程以及向 SQL Server 数据库发出 SQL 文本命令。它返回 SqlDataReader、DataSet 和 XmlReader 对象。您可以在自己的 .NET 应用程序中将其作为构造块来使用,以减少需要创建、测试和维护的自定义代码的数量。您可以下载完整的 C# 和 Visual Basic .NET 源代码以及综合文档。【这段是抄的

故事:

最近在项目中使用到了这个古老的组件,一切都是那么的美好,只到昨天下午,当我用这个组件执行一个时间比较长的存储过程时,厄运就来了:

             try
{
SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, sql);
return ;
}
catch (Exception ex)
{
return -;
}

每当执行一段时间,自动就抛出Timeout的异常,好吧,作为一个有理智的程序员,马上就去找怎么给SqlHelper中的查询自定义Timeout,然而……悲催的我竟然没有找到,好吧,拿出反编译工具看看有没有Timeout字段,依然没有
使用 Microsoft.ApplicationBlocks.Data SqlHelper  查询超时以及解决方案

那么只能借助网络去找答案了,某度 呵呵,大量的转载,特意在搜索中加入了“Timeout”  “超时” ,然并卵,万能的过滤大发,把那些词都忽略了。

再想想 google 哎……  后来只能通过一些国内的“谷粉搜搜”之类的找一找了,虽然不多但还是找到了,都是E文的,然后借助自己很渣的E温水瓶,成功没找到答案。

好吧那只能看看有没有源代码了,某度照样渣,最后在一个外国网站上还真找到了源代码,然后简单修改

         /// <summary>
/// This method opens (if necessary) and assigns a connection, transaction, command type and parameters
/// to the provided command.
/// </summary>
/// <param name="command">the SqlCommand to be prepared</param>
/// <param name="connection">a valid SqlConnection, on which to execute this command</param>
/// <param name="transaction">a valid SqlTransaction, or 'null'</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParameters to be associated with the command or 'null' if no parameters are required</param>
private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
{
//if the provided connection is not open, we will open it
if (connection.State != ConnectionState.Open)
{
connection.Open();
} command.CommandTimeout = Timeout;//注意这里是我加的
//associate the connection with the command
command.Connection = connection; //set the command text (stored procedure name or SQL statement)
command.CommandText = commandText; //if we were provided a transaction, assign it.
if (transaction != null)
{
command.Transaction = transaction;
} //set the command type
command.CommandType = commandType; //attach the command parameters if they are provided
if (commandParameters != null)
{
AttachParameters(command, commandParameters);
} return;
}

编译,引用,成功执行,一切又是那么的美好。

使用 Microsoft.ApplicationBlocks.Data SqlHelper  查询超时以及解决方案

好吧,故事就到这了,特地把文件分享出来,毕竟助人为快乐之本,

里面有加入了Timeout字段并编译好的文件和 源码安装文件(未修改)

http://files.cnblogs.com/files/twzy/Microsoft.ApplicationBlocks.Data.zip

最后宣传一下自己的抓包软件

NetAnalyzer下载地址

NetAnalzyer交流群:39753670 (PS 只提供交流平台,群主基本不说话^_^)

[转载请保留作者信息  作者:冯天文  网址:http://www.cnblogs.com/twzy/p/4976867.html]