Hey Folks,

Here is a complete documentation of Database Helper for C#.NET which is not only the easiest way to perform database operations but will also boost your speed. It would be great if you can add more functionalities to it as its an open-source project and is available on GIT.

Utilizing Database Helper

  1. Utilizing the helper is pretty straight forward. You just have to add a reference to your existing or a new project after downloading it. Download Database Helper.
  2. Once you downloaded it. Open your C# .NET Project in Visual Studio and right-click references and click add reference as shown below:
    utilizing-database-helper-1
  3. New Dialog (Reference Manager) will be open after clicking add reference. Now click browse button and select the downloaded DLL file:
    utilizing-database-helper-2

Configuring Connection String in Database Helper

Now Database Helper is a part of your project and you can start using it but before that there is one last thing which I would like to share here is Defining a Connection string. So that is pretty straight forward.

In case of all ASP.NET projects you have to define the connection string inside your web.config file and in case of WPF you can define the connection string inside App.Config. For that just paste the following code inside tags.

<add name="DefaultConnectionString" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=Name of your Database;Integrated Security=True" providerName="System.Data.SqlClient"

Make sure  your connection string can be of any format but the only thing matters is its name which should be DefaultConnectionString in order to utilize the database helper.

That's all. Now helper is ready to boost your project by making coding more easier and quicker.

Testing the Connection

Lets assume we are developing a ASP.NET Web Forms application. Then in my file.aspx.cs I can test rather the connection is established successfully or not.

Few Steps to check connectivity:

  1. Suppose we want to insert data to our database. So we need to add a namespace first and following is the namespace for our helper.
    using Database_Helpers;
  2. Now opening and closing the connection can help us in finding out rather  the helper works or not:
    void anyFunction()
    {
    helper db = new helper();
    db.Connection.Open(); // If the connection opens without any error it means you have configured connection string perfectly and don't forget to close the connection.
    db.Connection.Close();
    }
    

Insert Data Example

So with this database helper you can insert data without writing long code and worrying about opening and closing connections again and again. All these tasks will be done by this db helper all we need is to tell it what data is to be inserted in which column and in which table. For this purpose we have already implemented Dictionary in helper to send data to database as shown below:

void insertData()
{
helper db = new helper();
db.Values.add("database column name here","value you want to insert");
if (db.Insert("name of the table",db.Values)
{
//Data Inserted Successfully.
}
else
{
//Failed to Insert Data
}
}

It is important to  clear the values collection afterwards so that old values can be replaced with new as shown below:

void insertData()
{
helper db = new helper();
db.Values.add("name","bilal");
db.Values.add("age","23");
if (db.Insert("person",db.Values)
{
//Data Inserted Successfully.
db.Values.Clear();
}
else
{
//Failed to Insert Data
}
}

So this is how simple it is to insert data to database with database helper.

Update Data Example

void UpdateData()
{
helper db = new helper();
db.Values.add("age","23");
if (db.Update("person",db.Values,"where name='bilal'")
{
//Data Updated Successfully.
db.Values.Clear();
}
else
{
//Failed to Update Data
}
}

Delete Data Example

void DeleteData()
{
helper db = new helper();
if (db.Delete("person","where name='bilal'")
{
//Data Deleted Successfully.
}
else
{
//Failed to Delete Data
}
}