Hello if you are using 3 tier Architecture,put the below code inside your DataAccessLayer(DAL).
Open the App.config put the below code
<pre lang="c#">="1.0"="utf-8"
<configuration>
<connectionStrings>
<add name="Constr" connectionString="Server=.\SQLEXPRESS; Integrated Security=true;Initial Catalog=IMS"/>
</connectionStrings>
</configuration></pre>
IDispose
<pre lang="c#">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DAL
{
public interface IDispose
{
void Dispose();
}
}
</pre>
Base Class
public abstract class BaseClass :IDispose
{
public SqlConnection sqlcon;
public SqlCommand sqlcm;
protected SqlDataReader sqlDr;
public void Dispose()
{
sqlcon.Close();
}
}
Method Class
public class Method :BaseClass
{
public Method()
{
string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
sqlcon=new SqlConnection(Connectionstring);
sqlcon.Open();
}
public Method(bool isMaster)
{
string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
sqlcon = new SqlConnection(Connectionstring);
sqlcon.Open();
}
public Method(string ConnectionString)
{
sqlcon = new SqlConnection(ConnectionString);
sqlcon.Open();
}
public SqlDataReader ExecuteReader()
{
sqlcm.Connection = sqlcon;
sqlDr = sqlcm.ExecuteReader();
return sqlDr;
}
public DataTable GetDataTable()
{
DataTable Dt;
DataSet Ds = FillData();
Dt = Ds.Tables[0];
return Dt;
}
public class SqlDataBase : Method
{
public SqlDataBase()
{
string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
sqlcon = new SqlConnection(Connectionstring);
sqlcon.Open();
}
public SqlDataBase(string ConnectionString)
{
ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
sqlcon = new SqlConnection(ConnectionString);
sqlcon.Open();
}
}
Accessing Connection string
public static bool Insert(PersonelData person)
{
const string insertdetails = "exec[INSERT_PERSONEL]@sid,@FName,@MName,@LName";
DAL.Method method = new DAL.SqlDataBase();
method.SelectQuery(insertdetails);
method.AddParameter("@sid", person.SID);
method.AddParameter("@FName", person.FName);
method.AddParameter("@MName",person.MName );
method.AddParameter("@LName",person.LName);
int ins = method.ExecuteQuery();
if (ins > 0)
{
return true;
}
return false;
}