Steps:
1.Add a class in your application. Name it ‘clsDataAccessLayer’.
2.Add following code within class.
public class clsDataAccessLayer
{
private SqlConnection sqlCon;
private SqlDataAdapter sqlDa;
private DataSet dsResult;
public SqlConnection Connection()
{
sqlCon = new SqlConnection();
sqlCon.ConnectionString = ConfigurationSettings.AppSettings["Main.ConnectionString"].ToString();
return sqlCon;
}
public DataSet GetResults(string strQuery)
{
try
{
sqlCon = Connection();
sqlCon.Open();
sqlDa = new SqlDataAdapter(strQuery, sqlCon);
dsResult = new DataSet();
sqlDa.Fill(dsResult);
return dsResult;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sqlCon.Close();
}
}
}
3.From your UI layer where you want to display the data.
Call the method ‘GetResults’, Which will return a dataset.Bind this dataset where you want to display this data. Example gridview.
1.Add a class in your application. Name it ‘clsDataAccessLayer’.
2.Add following code within class.
public class clsDataAccessLayer
{
private SqlConnection sqlCon;
private SqlDataAdapter sqlDa;
private DataSet dsResult;
public SqlConnection Connection()
{
sqlCon = new SqlConnection();
sqlCon.ConnectionString = ConfigurationSettings.AppSettings["Main.ConnectionString"].ToString();
return sqlCon;
}
public DataSet GetResults(string strQuery)
{
try
{
sqlCon = Connection();
sqlCon.Open();
sqlDa = new SqlDataAdapter(strQuery, sqlCon);
dsResult = new DataSet();
sqlDa.Fill(dsResult);
return dsResult;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sqlCon.Close();
}
}
}
3.From your UI layer where you want to display the data.
Call the method ‘GetResults’, Which will return a dataset.Bind this dataset where you want to display this data. Example gridview.
How to access data in three tier architecture?
Reviewed by kamal kumar das
on
October 25, 2011
Rating:
No comments: