WebMatrix Database.Query 与自定义 CommandTimeout

2024-02-02

考虑以下带有 TestTable 和过程的 TestDb

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内置数据库查询助手时,您可以执行以下操作:

@{
    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 分钟之类的吗?

由于没有找到解决方案,我根据大量搜索和尝试创建了自己的 SimpleQuery 助手,直到最终找到了一个代码参考 http://blogs.msdn.com/b/davidebb/archive/2009/10/29/using-c-dynamic-to-simplify-ado-net-use.aspx我能够理解和适应。

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 帮助器,我可以获得几乎相同的结果,但存在一些问题

@{
    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 帮助程序的第二个 Web 代码中进行了注释。我可以解决这些问题,但我仍然在努力解决为什么未检测到数字和日期值的 NULL 检查。

我希望能够帮助您正确检测这些内容,这样我就可以避免在使用 Double.Parse 或 DateTime.Parse 时出现后续错误。我也很感激 SimpleQuery 帮助器或您看到的其他任何内容的一般指示/改进。

提前致谢。


您可以尝试改用 Dapper。它的语法与 WebMatrix.Data 非常相似,可以返回结果为IEnumerable<dynamic>或强类型(如果您愿意),并允许您在每个查询的基础上覆盖命令超时。

https://github.com/StackExchange/dapper-dot-net https://github.com/StackExchange/dapper-dot-net

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WebMatrix Database.Query 与自定义 CommandTimeout 的相关文章

  • 为什么使用数组索引循环数组比指针访问慢?

    我正在读Kochan的书 Programming in C 在第 14 页的 指针和数组 部分中 264 他说 一般来说 索引数组的过程比执行索引过程花费更多的时间 访问指针内容的过程 其实这也是主要原因之一 为什么使用指针来访问数组的元素
  • 命名管道客户端无法连接到作为网络服务运行的服务器

    我有一个服务在网络服务帐户下运行 该服务只是设置一个命名管道并侦听连接 NamedPipeServerStream listeningPipe new NamedPipeServerStream ourservicepipe PipeDir
  • (SQL) 识别字段中字符串格式多次出现的位置

    我需要将叙述字段 自由文本 拆分为多行 目前的格式如下 Case Reference Narrative XXXX XX 123456 Endless Text up to 50k characters 在作为文本的叙述字段中 各个条目 当
  • 值类型如何实现引用类型

    我遇到了一个值类型正在实现 ref 的场景 类型 只是想知道这怎么可能 幕后发生了什么 结构体是值类型 接口是引用 类型但结构可以实现接口而不会出现任何错误 有什么想法吗 提前致谢 实际上 它同时以两种不同的方式进行 首先 任何值类型都可以
  • 带有嵌入 Flash 视频的 PDF 示例?

    有谁知道我在哪里可以查看嵌入 Flash 视频的 PDF 示例 我知道问这个问题很愚蠢 因为你会认为任何面向技术的用户都应该能够使用谷歌找到一个 但我真的找不到 我的另一个问题是 使用 C 中的 API 将 Flash 视频嵌入 PDF 文
  • ASP.NET 网站上的 XSS 攻击 [已关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我遇到了很大的麻烦 请帮忙 我的网站
  • C++ 并行任务的开销

    我有以下简单的功能 include
  • UI 线程正在阻塞调用 COM 对象的后台线程

    我正在开发一个通过第三方 COM 库与外部设备通信的应用程序 我试图让与设备的所有通信都通过后台线程 以防止通信问题搞砸我的应用程序 并消除在 UI 线程中进行通信所引入的一些其他复杂性 问题是 每当发生导致主 UI 线程阻塞的情况 即调用
  • 将视频上传/保存到数据库或文件系统

    我以前从未尝试过保存视频 所以我对此了解不多 我知道如果视频很小 我可以转换为字节数组并保存到数据库 但是为了提高效率 我想了解如何将任何上传的视频保存到我的服务器文件中 然后只保存该文件的文件路径我的数据库表中的视频 我完全不知道如何开始
  • 如何生成可变参数包?

    给定不相关的输入是否可以生成非类型参数包 我的意思是 我想改变这一点 template
  • 在 Linq 查询中使用动态列名称

    foreach Dimension dimensions in Enum GetValues typeof Dimension var r new ReferenceTable dimensions referenceItems List
  • 解析连接字符串

    是否有标准库或代码片段可以使用这样的连接字符串获取值 string connstr DataServiceUrl http localhost foo RemoteServerConnection server http localhost
  • 为什么最小的 int -2147483648 的类型为“long”? [复制]

    这个问题在这里已经有答案了 对于一个学校项目 我必须编写 C 函数 printf 的代码 一切进展顺利 但有一个问题我找不到好的答案 所以我来了 printf PRINTF d t d n 2147483648 告诉我 gcc Werror
  • “DeploymentItem”属性是什么意思?

    假设我们有一个简短的程序 namespace ConsoleTryIt static class Program static void Main string args var sum Add 1 2 private static int
  • 如何将 Boost Spirit 自动规则与 AST 结合使用?

    编辑 当我想在另一个规则上使用它时 我扩展了 sehe 的示例以显示问题 http liveworkspace org code 22lxL7 http liveworkspace org code 22lxL7 17 我正在尝试提高 Bo
  • 从 AuthorizeAttribute 继承的属性不起作用

    我目前正在尝试根据用户角色在新的 ASP MVC 5 应用程序中实现安全性 目标是防止用户在没有特定角色 或更高角色 的情况下访问某些控制器或控制器方法 根据到目前为止我所读到的问题 我创建了一个继承 AuthorizeAttribute
  • C中使用JNI从对象获取对象

    public class Student private People people private Result result private int amount 这是 Java 中类的示例 在C中 我试图获取 学生 中的 人 但失败了
  • 使用 DataGridViewCheckboxCell 真正禁用 DataGridView 中的复选框

    有谁知道如何使用 DataGridViewCheckboxCell 禁用 DataGridView 中的复选框 我可以将其设置为只读 并设置背景颜色 但我无法让复选框本身显示为禁用状态 有什么想法吗 Guess 你必须自己画 http so
  • sql server GO 相当于 oracle

    我正在为 Oracle 编写迁移脚本 我需要更改表结构 然后用数据填充它 我想先进行结构更改 然后再进行数据更改 在 SQL Server 中我会使用GO分离批次 是否有 SQL ServerGOOracle 中的等效命令 It s and
  • 从其对象获取结构体字段的名称和类型

    例如 我有一个类似这样的结构 struct Test int i float f char ch 10 我有一个该结构的对象 例如 Test obj 现在 我想以编程方式获取字段名称和类型obj 是否可以 顺便说一句 这是 C 你正在要求C

随机推荐