When I started at my current employer I inherited a project from a previous developer and in that project was a data access utility class that tries to simplify a lot of the code involved in making calls and retrieving data back from the database. Over time it has been modified to add more overloaded forms of functions, and now I am looking at possible suggestions from the StackOverflow community.
What do you think should be added? Removed? Modified?
Note:
It would be nice if this class could remain compatible to VB.NET in the .NET 2.0 framework. We are also developing in 3.5, but I would like to have something that is generally going to work across most frameworks (so no LINQ, etc.) Also, please refrain from unnecessary answers that consist of nothing but "Use nHibernate" or other tools.
My class:
Public Class DataAccess
Public Shared Function ReturnScalar(ByVal CmdStr As String) As String
Dim Result As String
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As SqlCommand = New SqlCommand(CmdStr, con)
cmd.CommandType = CommandType.StoredProcedure
Result = cmd.ExecuteScalar
con.Close()
Return Result
End Function
Public Shared Function ReturnScalar(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As Integer) As String
Dim Result As String
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As SqlCommand = New SqlCommand(CmdStr, con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
Result = cmd.ExecuteScalar
con.Close()
Return Result
End Function
Public Shared Function ReturnScalar(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As String) As String
Dim Result As String
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As SqlCommand = New SqlCommand(CmdStr, con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
Result = cmd.ExecuteScalar
con.Close()
Return Result
End Function
Public Shared Function ReturnScalar(ByVal CmdStr As String, ByVal Params As Hashtable) As String
Dim Result As String
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
cmd.CommandType = CommandType.StoredProcedure
For Each entry As DictionaryEntry In Params
cmd.Parameters.Add(New SqlParameter(entry.Key.ToString, entry.Value))
Next
Result = cmd.ExecuteScalar
con.Close()
Return Result
End Function
Public Shared Function ReturnDataSet(ByVal CmdStr As String) As DataSet
Dim DS As New DataSet
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
adp.Fill(DS)
con.Close()
Return DS
End Function
Public Shared Function ReturnDataSet(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As Integer) As DataSet
Dim DS As New DataSet
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
adp.Fill(DS)
con.Close()
Return DS
End Function
Public Shared Function ReturnDataSet(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As String) As DataSet
Dim DS As New DataSet
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
adp.Fill(DS)
con.Close()
Return DS
End Function
Public Shared Function ReturnDataSet(ByVal CmdStr As String, ByVal Params As Hashtable) As DataSet
Dim DS As New DataSet
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
For Each entry As DictionaryEntry In Params
cmd.Parameters.Add(New SqlParameter(entry.Key.ToString, entry.Value))
Next
adp.Fill(DS)
con.Close()
Return DS
End Function
Public Shared Function ReturnDataTable(ByVal CmdStr As String) As DataTable
Dim DT As New DataTable
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
adp.Fill(DT)
con.Close()
Return DT
End Function
Public Shared Function ReturnDataTable(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As Integer) As DataTable
Dim DT As New DataTable
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
adp.Fill(DT)
con.Close()
Return DT
End Function
Public Shared Function ReturnDataTable(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As Date) As DataTable
Dim DT As New DataTable
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
adp.Fill(DT)
con.Close()
Return DT
End Function
Public Shared Function ReturnDataTable(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As String) As DataTable
Dim DT As New DataTable
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
adp.Fill(DT)
con.Close()
Return DT
End Function
Public Shared Function ReturnDataTable(ByVal CmdStr As String, ByVal Params As Hashtable) As DataTable
Dim DT As New DataTable
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
Dim adp As New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
For Each entry As DictionaryEntry In Params
cmd.Parameters.Add(New SqlParameter(entry.Key.ToString, entry.Value))
Next
adp.Fill(DT)
con.Close()
Return DT
End Function
Public Shared Function DBExecute(ByVal CmdStr As String) As Integer
Dim RowsAffected As Integer
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
cmd.CommandType = CommandType.StoredProcedure
RowsAffected = cmd.ExecuteNonQuery
con.Close()
Return RowsAffected
End Function
Public Shared Function DBExecute(ByVal CmdStr As String, ByVal ParamName As String, ByVal Param As Integer) As Integer
Dim RowsAffected As Integer
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter(ParamName, Param))
RowsAffected = cmd.ExecuteNonQuery
con.Close()
Return RowsAffected
End Function
Public Shared Function DBExecute(ByVal CmdStr As String, ByVal Params As Hashtable) As Integer
Dim RowsAffected As Integer
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("RoutingConnectionString").ToString)
con.Open()
Dim cmd As New SqlCommand(CmdStr, con)
cmd.CommandType = CommandType.StoredProcedure
For Each entry As DictionaryEntry In Params
cmd.Parameters.Add(New SqlParameter(entry.Key.ToString, entry.Value))
Next
RowsAffected = cmd.ExecuteNonQuery
con.Close()
Return RowsAffected
End Function
End Class
Note #2:
"RoutingConnectionString" just happens to be the name of the ConnectionString in this particular version of the class and is not something which would remain the same throughout all of our projects.
Note #3:
All of our projects utilize SQLServer2005, but I do not see anything wrong with modifications which could help make it more flexible with other database systems such as MySQL or older/newer versions of SQL Server.