|
using System;
using System.Data;
using System.Data.SqlClient;
namespace DataAccessLayer
{
/// <summary>
/// Summary description for DataAccessLayer.
/// This is a generic code for loading data from any database..
/// One can also use OleDb for more generalized coding....
/// </summary>
public class DataAccess
{
//Private member
private string strConn;
private string strSelectCat;
public DataAccess()
{
//
// TODO: Add constructor logic here
//Initialization
strConn=null;
strSelectCat=null;
}
public DataAccess(string _strConn,string _strSelectCat)
{
//parameterized constructor;
strConn=_strConn;
strSelectCat=_strSelectCat;
}
//set connectionstring property
//this has been done just to show how property can be defined
public string ConnectionString
{
get
{
return strConn;
}
set
{
strConn=value;
}
}
public string SelectStmOnCat
{
get
{
return strSelectCat;
}
set
{
strSelectCat=value;
}
}
//PreCondition:Take Connection Object, Sql Query
//PostCondition:Return DataTable containing Category list
//Process:Fill Datatable with category from Northwind _db
public DataTable LoadCategoryInDropDownList()
{
SqlConnection objConn=new SqlConnection(strConn);
DataTable tblCategory =new DataTable();
try
{
objConn.Open();
//Using helps to dispose object as soon as
//required process gets complete
using(SqlDataAdapter objAdpOnCat=new SqlDataAdapter(strSelectCat,objConn))
{
objAdpOnCat.Fill(tblCategory);
}
}
catch(SqlException e)
{
throw new Exception("Invalid Connection Error Occured "+e.Message);
}
finally
{
if(objConn.State.ToString()=="Open")
{
objConn.Dispose();
objConn.Close();
}
}
return tblCategory;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.