WebMatrix Database.Query 与自定义 CommandTimeout [英] WebMatrix Database.Query with Custom CommandTimeout
问题描述
考虑以下带有 TestTable 和程序的 TestDb
Consider the following TestDb with TestTable and Procedure
USE TestDb
GO
--DROP TABLE dbo.TestTable
IF NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = 'TestTable')
BEGIN
CREATE TABLE dbo.TestTable
(
RecordId int NOT NULL IDENTITY(1,1) PRIMARY KEY
, StringValue varchar(50) NULL
, DateValue date NULL
, DateTimeValue datetime NULL
, MoneyValue money NULL
, DecimalValue decimal(19,4) NULL
, IntValue int NULL
, BitValue bit NOT NULL
)
INSERT INTO dbo.TestTable
SELECT 'Test', CAST(GETDATE() AS DATE), GETDATE(), 100.15, 100.0015, 100, 1
UNION SELECT NULL, NULL, NULL, NULL, NULL, NULL, 0
END
GO
IF EXISTS (SELECT 1 FROM sys.procedures WHERE name = 'Get_TestTable')
DROP PROCEDURE dbo.Get_TestTable
GO
CREATE PROCEDURE dbo.Get_TestTable (@RecordId int = NULL) AS WAITFOR DELAY '00:00:30'; SELECT * FROM dbo.TestTable WHERE RecordId = ISNULL(@RecordId,RecordId);
GO
EXEC dbo.Get_TestTable @RecordId = NULL
使用 WebMatrix 内置数据库查询助手时,您可以执行以下操作:
When using WebMatrix built-in database query helper, you can do the following:
@{
string errorMessage = String.Empty;
int? RecordId = null;
IEnumerable<dynamic> rowsTestTable = null;
try
{
using (Database db = Database.Open("TestDb"))
{
rowsTestTable = db.Query("EXEC dbo.Get_TestTable @RecordId=@0",RecordId);
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
@if(errorMessage == String.Empty)
{
<table border="1">
<thead>
<tr>
<th>RecordId</th>
<th>StringValue</th>
<th>DateValue</th>
<th>DateTimeValue</th>
<th>MoneyValue</th>
<th>DecimalValue</th>
<th>IntValue</th>
<th>BitValue</th>
</tr>
</thead>
<tbody>
@foreach(var row in rowsTestTable)
{
<tr>
<td>@row["RecordId"]</td>
<td>@row["StringValue"]</td>
<td>@if(@row["DateValue"] != null){@Html.Raw(String.Format("{0:MM/dd/yyyy}",@row["DateValue"]));}</td>
<td>@if(@row["DateTimeValue"] != null){@Html.Raw(String.Format("{0:MM/dd/yyyy hh:mm:ss.fff tt}",@row["DateTimeValue"]));}</td>
<td>@if(@row["MoneyValue"] != null){@Html.Raw(String.Format("{0:c}",@row["MoneyValue"]));}</td>
<td>@row["DecimalValue"]</td>
<td>@row["IntValue"]</td>
<td>@row["BitValue"]</td>
</tr>
}
</tbody>
</table>
}
<p>@errorMessage</p>
<h4>No Additional Problem - On handling of DateValue</h4>
@try
{
foreach(var row in rowsTestTable)
{
<p>@if(row.DateValue != null){@Html.Raw(DateTime.Parse(row.DateValue.ToString()))}</p>
}
}
catch (Exception ex)
{
<p>@ex.Message</p>
}
<h4>No Additional Problem - On handling of MoneyValue (and other number values)</h4>
@try
{
foreach(var row in rowsTestTable)
{
<p>@if(row.MoneyValue != null){@Html.Raw(Double.Parse(row.MoneyValue.ToString()))}</p>
}
}
catch (Exception ex)
{
<p>@ex.Message</p>
}
</body>
</html>
这会产生超时过期错误,因为 WebMatrix Database.Query 帮助程序已修复默认的 30 秒 CommandTimeout.有没有办法将单个查询的默认值覆盖为 5 分钟之类的时间?
This gives a Timeout expired error because the WebMatrix Database.Query helper has fixed default 30 second CommandTimeout. Is there any way to override the default for an individual query to something like 5 minutes?
没有找到解决方案,我根据大量搜索和尝试创建了自己的 SimpleQuery 帮助程序,直到我终于找到了 代码参考,我能够理解和适应.
Not having found a solution, I went down the road of creating my own SimpleQuery helper based on numerous searches and trying out things until I finally found a code reference that I was able to understand and adapt.
using System.Collections.Generic; // IEnumerable<dynamic>
using System.Data; // IDataRecord
using System.Data.SqlClient; // SqlConnection
using System.Dynamic; // DynamicObject
public class SimpleQuery
{
public static IEnumerable<dynamic> Execute(string connectionString, string commandString, int commandTimeout)
{
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(commandString, connection))
{
command.CommandTimeout = commandTimeout;
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
foreach (IDataRecord record in reader)
{
yield return new DataRecordDynamicWrapper(record);
}
}
connection.Close();
}
}
}
public class DataRecordDynamicWrapper : DynamicObject
{
private IDataRecord _dataRecord;
public DataRecordDynamicWrapper(IDataRecord dataRecord) { _dataRecord = dataRecord; }
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _dataRecord[binder.Name];
return result != null;
}
}
}
所以现在通过更改网络代码以使用新的 SimpleQuery 帮助程序,我可以获得几乎相同的结果,但存在一些问题
So now with the changes to the web-code to use the new SimpleQuery helper, I can get almost equivalent results, but with some problems
@{
string errorMessage = String.Empty;
int? RecordId = null;
IEnumerable<dynamic> rowsTestTable = null;
try
{
string commandString = String.Format("dbo.Get_TestTable @RecordId={0}", RecordId == null ? "null" : RecordId.ToString()); // Problem 1: Have to use String.Format to embed the Parameters
rowsTestTable = SimpleQuery.Execute(System.Configuration.ConfigurationManager.ConnectionStrings["TestDb"].ConnectionString,commandString,300);
foreach(var row in rowsTestTable) { break; } // Problem 2: Have to force query execution here, so the error (if any) gets trapped here
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
@if(errorMessage == String.Empty)
{
<table border="1">
<thead>
<tr>
<th>RecordId</th>
<th>StringValue</th>
<th>DateValue</th>
<th>DateTimeValue</th>
<th>MoneyValue</th>
<th>DecimalValue</th>
<th>IntValue</th>
<th>BitValue</th>
</tr>
</thead>
<tbody>
@foreach(var row in rowsTestTable)
{
<tr>
@*<td>@row["RecordId"]</td>*@ <!-- Problem 3: Can't reference as row["FieldName"], so if any field names have spaces or other special characters, can't reference -->
<td>@row.RecordId</td>
<td>@row.StringValue</td>
<td>@if(@row.DateValue != null){@Html.Raw(String.Format("{0:MM/dd/yyyy}",@row.DateValue));}</td>
<td>@if(@row.DateTimeValue != null){@Html.Raw(String.Format("{0:MM/dd/yyyy hh:mm:ss.fff tt}",@row.DateTimeValue));}</td>
<td>@if(@row.MoneyValue != null){@Html.Raw(String.Format("{0:c}",@row.MoneyValue));}</td>
<td>@row.DecimalValue</td>
<td>@row.IntValue</td>
<td>@row.BitValue</td>
</tr>
}
</tbody>
</table>
}
<p>@errorMessage</p>
<h4>Additional Problem - Unexpected handling of DateValue</h4>
@try
{
foreach(var row in rowsTestTable)
{
<p>@if(row.DateValue != null){@Html.Raw(DateTime.Parse(row.DateValue.ToString()))}</p>
}
}
catch (Exception ex)
{
<p>@ex.Message</p>
}
<h4>Additional Problem - Unexpected handling of MoneyValue (and other number values)</h4>
@try
{
foreach(var row in rowsTestTable)
{
<p>@if(row.MoneyValue != null){@Html.Raw(Double.Parse(row.MoneyValue.ToString()))}</p>
}
}
catch (Exception ex)
{
<p>@ex.Message</p>
}
</body>
</html>
问题 1-3 在使用 SimpleQuery 帮助程序的第二个网络代码中进行了注释.这些我可以解决,但我仍在努力解决的是为什么没有检测到 Number 和 Date 值的 NULL 检查.
Problem 1-3 are commented in the second web-code which is using the SimpleQuery helper. These I can work around, but what I am still struggling with is why the NULL check isn't detected for the Number and Date values.
我希望能够帮助您正确检测它们,以便在使用 Double.Parse 或 DateTime.Parse 时避免后续错误.对于 SimpleQuery 帮助程序或您看到的任何其他内容,我也将不胜感激.
I would appreciate help to properly detect those, so I can avoid the subsequent error when using Double.Parse or DateTime.Parse. I would also appreciate any general pointers/improvements for either the SimpleQuery helper, or anything else you see.
提前致谢.
推荐答案
您可以尝试改用 Dapper.它的语法与 WebMatrix.Data 非常相似,可以将结果返回为 IEnumerable
或强类型(如果您愿意),并允许您在每个查询的基础上覆盖命令超时.
You could try switching to using Dapper. It has a very similar syntax to WebMatrix.Data, can return results as IEnumerable<dynamic>
or strongly typed if you prefer, and allows you to override the command timeout on a per query basis.
https://github.com/StackExchange/dapper-dot-net
这篇关于WebMatrix Database.Query 与自定义 CommandTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!