通用数据访问功能 [英] Generic Data Access functions
问题描述
对以下通用数据访问函数(ADO.NET,C#或VB,SQLServer或OLEDB)进行编码的最佳方式是什么?
- 在连接上执行SQL
- 打开一个DataReader
- 打开一个DataSet(关于这个的任何想法?)
这样我可以从我的程序中的任何地方调用这些函数。我对数据访问模式或数据访问层不感兴趣,除非他们直接适用于这些功能。 (即自动关闭连接或读取器/数据集的模式)
使用示例
ExecuteSQL(UPDATE tblTest SET x = 5 WHERE [ID] = 4)
使用rdr As OleDb.OleDbDataReader = OpenReader(SELECT * FROM tblExecute)
虽然rdr.Read()
结束而
结束使用
示例函数
Public Function ExecuteSQL(ByVal strSQL As String)As Boolean b $ b使用cn作为新的OleDb .OleDbConnection(strConn)
cn.Open()
使用cmd作为新的OleDb.OleDbCommand(strSQL,cn)
返回cmd.ExecuteNonQuery()> 0
结束使用
结束使用
返回False
结束函数
公共函数OpenReader(ByVal strSQL As String)As OleDb.OleDbDataReader
Dim cn As New OleDb.OleDbConnection(strConn )
cn.Open()
如果cn.State = ConnectionState.Open Then
Dim cmd As New OleDb.OleDbCommand(strSQL,cn)
返回cmd.ExecuteReader(CommandBehavior.CloseConnection)
else
抛出新异常(Unable to connect )
End If
End Function
这是我的填充方法,它给出了一个通用列表和一个lambda,使用从IDataReader读取的对象填充列表:
public static void Fill< T>(this IDbCommand cmd,
IList< T>列表,Func< IDataReader,T>使用(var rdr = cmd.ExecuteReader())
{
while(rdr.Read())
{
list)
{
。加入(rowConverter(RDR));
$ b你可以像这样使用它: // var cmd = new SqlCommand(...);
// var things = new List< Thing>();
cmd.Fill(things,r => new Thing {ID = r.GetInt32(0),Name = r.GetString(1)});
真的很方便可以将ExecuteReader和Read循环包装在一行中。
What is the best way to code the following generic data access functions (ADO.NET, C# or VB, SQLServer or OLEDB)
- Execute SQL on a connection
- Open a DataReader
- Open a DataSet (any ideas on this one?)
Such that I can call these functions from anywhere in my program. I'm not interested in Data Access patterns or Data Access layers unless they directly apply to these functions. (i.e. a pattern to automatically close the connection or the reader/dataset)
Examples of use
ExecuteSQL("UPDATE tblTest SET x = 5 WHERE [ID] = 4")
Using rdr As OleDb.OleDbDataReader = OpenReader("SELECT * FROM tblExecute")
While rdr.Read()
End While
End Using
Example functions
Public Function ExecuteSQL(ByVal strSQL As String) As Boolean
Using cn As New OleDb.OleDbConnection(strConn)
cn.Open()
Using cmd As New OleDb.OleDbCommand(strSQL, cn)
Return cmd.ExecuteNonQuery() > 0
End Using
End Using
Return False
End Function
Public Function OpenReader(ByVal strSQL As String) As OleDb.OleDbDataReader
Dim cn As New OleDb.OleDbConnection(strConn)
cn.Open()
If cn.State = ConnectionState.Open Then
Dim cmd As New OleDb.OleDbCommand(strSQL, cn)
Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
Else
Throw New Exception("Unable to connect to database.")
End If
End Function
解决方案 Here's my Fill method which, given a generic list and a lambda, populates the list with objects read from an IDataReader:
public static void Fill<T>(this IDbCommand cmd,
IList<T> list, Func<IDataReader, T> rowConverter)
{
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
list.Add(rowConverter(rdr));
}
}
}
You use it like this:
// var cmd = new SqlCommand(...);
// var things = new List<Thing>();
cmd.Fill(things, r => new Thing { ID = r.GetInt32(0), Name = r.GetString(1) });
Really handy to be able to wrap up that ExecuteReader and Read loop in one line like that.
这篇关于通用数据访问功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!