打开 SqlDataReader 时有什么方法可以避免嵌套使用?

发布于 2025-01-08 07:21:52 字数 619 浏览 0 评论 0原文

没什么大不了的,但为了整洁起见,有什么方法可以“创建并打开”SqlConnection 吗?

我天真地编写了这段代码:

using (var strConnection = new SqlConnection(sourceConnection))
using (var strCommand = new SqlCommand(query, strConnection))
using (var reader = strCommand.ExecuteReader())
{
    ...
}

这当然会在第 3 行失败,因为连接未打开。
有没有一种巧妙的方法来避免打开连接引入的嵌套?

using (var strConnection = new SqlConnection(sourceConnection))
{
    strConnection.Open();
    using (var strCommand = new SqlCommand(query, strConnection))
    using (var reader = strCommand.ExecuteReader())
    {
        ...
    }
}

Not a big deal but for neatness sake is there any way to "create and open" a SqlConnection?

I naively wrote this code:

using (var strConnection = new SqlConnection(sourceConnection))
using (var strCommand = new SqlCommand(query, strConnection))
using (var reader = strCommand.ExecuteReader())
{
    ...
}

Which of course fails on line 3 because the connection isn't open.
Is there a neat way to avoid that nesting that opening the connection introduces?

using (var strConnection = new SqlConnection(sourceConnection))
{
    strConnection.Open();
    using (var strCommand = new SqlCommand(query, strConnection))
    using (var reader = strCommand.ExecuteReader())
    {
        ...
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2025-01-15 07:21:52

好问题,我的想法是 SqlConnection 的扩展方法。

检查一下:

public static class SqlExtensions {
    public static SqlConnection OpenAndReturn(this SqlConnection con) {
        try {
            con.Open();
            return con;
        } catch {
            if(con != null)
                con.Dispose();
            throw;
        }
    }
}

用法:

using(var strConnection = new SqlConnection("CONNECTION").OpenAndReturn()) 
using(var strCommand = new SqlCommand("QUERY", strConnection))
using(var reader = strCommand.ExecuteReader()) {
    //...       
}

Good question, my idea is an Extension-Method for SqlConnection.

Check this:

public static class SqlExtensions {
    public static SqlConnection OpenAndReturn(this SqlConnection con) {
        try {
            con.Open();
            return con;
        } catch {
            if(con != null)
                con.Dispose();
            throw;
        }
    }
}

Usage:

using(var strConnection = new SqlConnection("CONNECTION").OpenAndReturn()) 
using(var strCommand = new SqlCommand("QUERY", strConnection))
using(var reader = strCommand.ExecuteReader()) {
    //...       
}
国产ˉ祖宗 2025-01-15 07:21:52

类似的事情怎么样:

class SqlHelper : IDisposable
{
    public SqlHelper(string connectionString, string query) { ... }

    public SqlConnection Connection { get; set; }
    public SqlCommand Command { get; set; }

    // SQL querying logic here
    public void Execute() { ... }

    /** IDisposable implementation **/
}

在你的代码中

using (SqlHelper sql = new SqlHelper(sourceConnection, query)) 
{
    var reader = sql.Execute();
    ...
}

What about something like that:

class SqlHelper : IDisposable
{
    public SqlHelper(string connectionString, string query) { ... }

    public SqlConnection Connection { get; set; }
    public SqlCommand Command { get; set; }

    // SQL querying logic here
    public void Execute() { ... }

    /** IDisposable implementation **/
}

and in your code

using (SqlHelper sql = new SqlHelper(sourceConnection, query)) 
{
    var reader = sql.Execute();
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文