C#如何在方法或方法链上创建方法
我创建了一个简化的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这最终对我有用:
我将不得不对其进行一些扩展以添加一些错误检查,也许返回的不仅仅是动态值 - 例如返回带有该值的对象和SQL数据类型。但是保罗和巴格斯的评论使我走上了正确的道路。
This ended up working for me:
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.