public class DBConnector
{
private string connectionString = null;
public DBConnector()
{
XmlTextReader reader = new XmlTextReader("LandscapeConfiguration.xml");
string nodeName = "";
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
nodeName = reader.Name;break;
case XmlNodeType.Text:
switch (nodeName)
{case "connectionstring":
connectionString = reader.Value;
break;}
break;
case XmlNodeType.EndElement:
break;
}}}
public SqlConnection Connect
{
get { return new SqlConnection(connectionString); }
}}
now in the gateway class constructor function initiate the connection -
DBConnector dbConnector = new DBConnector();
SqlConnection connection = dbConnector.Connect;
connection.Open();
now in the gateway class your db access method will be like that-
public SqlDataReader GetAllDepartmentInformation()
{
string selectQueryString = @"SELECT * FROM t_Department";
SqlCommand selectAllDepartmentCommand = new SqlCommand(selectQueryString,connection);
SqlDataReader reader = selectAllDepartmentCommand.ExecuteReader();
return reader;
}
and in your form/aspx page's code behind page access db data by-
DepartmentGateway department=new DepartmentGateway();
List<Department> departments = department.GetAllDepartmentInformation();