C#如何在方法或方法链上创建方法

发布于 2025-01-27 10:45:51 字数 1339 浏览 1 评论 0原文

我创建了一个简化的SQL数据类,以及用于返回准备使用结果集的类方法:(

public SQL_Data(string database) {

    string ConnectionString = GetConnectionString(database);

    cn = new SqlConnection(ConnectionString);

    try {
        cn.Open();
    } catch (Exception e) {
        Log.Write(e);
        throw;
    }

}

public SqlDataReader DBReader(string query) {

    try {

        using (SqlCommand cmd = new SqlCommand(query, this.cn)) {
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

    } catch {

        Log.Write("SQL Error with either Connection String:\n" + cn + " \nor Query:\n" + query);
        throw;
    }

}

我捕获任何错误,记录它们,然后在链条上捕获更高的错误。此外,我不包括ConnectionsTring() 请求的连接字符串。

对于Brevity,它只是返回所

SqlDataReader rs = new SQL_Data("MyDatabase").DBReader(@"SELECT * FROM Employees");

while (rs.Read()) {
    // code
}

rs.Close();

我想扩展它,并添加一个我想链接到.dbreader().columnReader()方法,例如:

string empID = new SQL_Data("MyDatabase").DBReader(@"SELECT * FROM Employees).ColumnReader("EmpID");

我尝试通过添加来尝试使用此操作。 ColumnReader()方法,但最终是直接的sql_data()类的方法,而不是.dbreader()的成员或扩展。我还尝试在.dbreader()中添加.columnReader()(例如“闭合”),但这也不起作用。

可以做到吗?

I have created a simplified SQL Data class, and a class method for returning a ready to use resultset:

public SQL_Data(string database) {

    string ConnectionString = GetConnectionString(database);

    cn = new SqlConnection(ConnectionString);

    try {
        cn.Open();
    } catch (Exception e) {
        Log.Write(e);
        throw;
    }

}

public SqlDataReader DBReader(string query) {

    try {

        using (SqlCommand cmd = new SqlCommand(query, this.cn)) {
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

    } catch {

        Log.Write("SQL Error with either Connection String:\n" + cn + " \nor Query:\n" + query);
        throw;
    }

}

(I catch any errors, log them, and then catch the error higher up the chain. Also, I did not include the ConnectionString() code for brevity. It just returns the requested connection string. That's all.)

This all works just fine, and with a single line of code, I'm ready to .Read() rows.

SqlDataReader rs = new SQL_Data("MyDatabase").DBReader(@"SELECT * FROM Employees");

while (rs.Read()) {
    // code
}

rs.Close();

I want to expand this and add a .ColumnReader() method that I want to chain to .DBReader() like this:

string empID = new SQL_Data("MyDatabase").DBReader(@"SELECT * FROM Employees).ColumnReader("EmpID");

I attempted this by adding a .ColumnReader() method, but it ends up being a method of SQL_Data() class directly, not a member or extension of .DBReader(). I also tried adding the .ColumnReader() inside the .DBReader() (like a "closure"), but that didn't work either.

Can this be done?

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

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

发布评论

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

评论(1

猫腻 2025-02-03 10:45:51

这最终对我有用:

public static class SQLExtentions {

    public static dynamic ColumnReader(this SqlDataReader rs, string colName) {
        return rs[colName];
    }

}

我将不得不对其进行一些扩展以添加一些错误检查,也许返回的不仅仅是动态值 - 例如返回带有该值的对象和SQL数据类型。但是保罗和巴格斯的评论使我走上了正确的道路。

This ended up working for me:

public static class SQLExtentions {

    public static dynamic ColumnReader(this SqlDataReader rs, string colName) {
        return rs[colName];
    }

}

I will have to expand on it a bit to add some error checking, and perhaps return more than just the dynamic value - like return an object with the value and it's SQL data type. But Paul and Bagus' comments got me on the right track.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文