I have created a database file userData.mdf. And have a created a class named dbHelper. Here i have made three functions:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Insert_Delete_UpdateOperation
{
public class dbhelper
{
public static SqlConnection sqlConnection;
public static SqlDataReader sqlDataReader;
public static void OpenConnection()
{
String connectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename="c:\users\suraj\documents\visual studio 2012\Assignment\Insert,Delete,UpdateOperation\Insert,Delete,UpdateOperation\userData.mdf"; Integrated Security=True";);
sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
}
public static void ExecuteQry(string qry,Boolean state)
{
SqlCommand sqlcommand=new SqlCommand(qry,sqlConnection);
if(state)
{
sqlcommand.ExecuteNonQuery();
}
else
{
sqlDataReader=sqlcommand.ExecuteReader();
}
}
public static void CloseConnection()
{
if(sqlConnection.State==System.Data.ConnectionState.Open)
{
sqlConnection.Close();
}
}
}
}