.net Database Utility Library
Overview
Accessing your database can be a very repetitive task. This Database Utility class can help reduce a lot of the repetitious code.This library will work in any .net or asp.net project; using C#, VB.net or any other .net language.
Features
- Full MSDN Style HTML documentation with example code.
Html Documentation can be located at http://joel.net/docs/DatabaseUtility. - Works in ANY .net project
C#, VB.NET, ASP.NET or any other supported .Net language, this library will work! - Retreive a DataTable with only one command!
You no longer have create a SqlDataAdapter and call the Fill() method to fill your DataTable. From retreiving a DataTable, SqlDataReader, Scalar or simply executing a NonQuery, you can do it all with only one command. - No need to Open and Close your connection object.
The SqlConnection object will automatically be Opened (if closed) and Closed at the end (if closed when calling method). If SqlConnection object is Open when calling the method, the SqlConnection object will be left open. The SqlConnection object will automatically be closed (if closed when calling method) with a SqlDataReader object when calling SqlDataReader's Close() method. - All input variables for methods are identical.
Changing your code to return a Datatable instead of a SqlDataReader is now super easy. - 15 overrides for each of the 4 methods.
The control has 4 methodsExecuteDataTable,ExecuteNonQuery,ExecuteReader,ExecuteScalareach with 15 overrides -- that's a total of 64 methods! - Global Connection's can be used in your project
so you can simplify your code like this...
// Retrieve DataTable from database (SqlConnection is automatically Opened and Closed!) DataTable table = DatabaseUtility.ExecuteDataTable("SELECT * FROM Customers"); -
Write less code.
You can convert this...
// Create SqlConnection, SqlDataAdapter, Datatable SqlConnection connection = new SqlConnection("Data Source=localhost;User ID=sa;Password=password"); SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection); DataTable table = new DataTable(); // Open Connection, fill DataTable, Close Connection. connection.Open(); adapter.Fill(table); connection.Close();
Into this...
// Retrieve DataTable from database (SqlConnection is automatically Opened and Closed!) DataTable table = DatabaseUtility.ExecuteDataTable( new SqlConnection("Data Source=localhost;User ID=sa;Password=password"), "SELECT * FROM Customers" );
Installation
If you using VisualStudio.net... Open your Project. In the Solution Explorer right click on References and select Add Reference. Click Browse and browse to the Joel.Net.DatabaseUtility.dll library.
If you are not using VisualStudio.net, simply copy the Joel.Net.DatabaseUtility.dll library into your ~/bin directory.
At the top of your class add using Joel.Net; // C#"Imports Joel.Net; ' VB.NET"Joel.Net.DatabaseUtility.ExecuteDataTable(...)).