PureDevelopers
IS-IT--Management
We currently have a class that does all database actions, but as it is now, each method is creating its own connection. I am trying to add a constructor to the class to allow all methods to use a single connection. When I try to call the Open method of Conn in
the DataTable GetDataTable method, I get the following error:
"The name 'Conn' does not exist in the current context"
How do I make Conn accessible?
I have tried using static, private and public for the constructor, but nothing works.
public class Database
{
public Database()
{
Database GetEnvmtName = new Database();
string strConnString = "Data Source=" + GetEnvmtName.CheckServer() + "; Initial Catalog=" + ConfigurationManager.AppSettings["DefaultDatabase"].ToString() + ";";
SqlConnection Conn = new SqlConnection(strConnString);
}
public DataTable GetDataTable(string SQL, string strDBaseName)
{
try
{
Conn.Open();
DataTable dtGetDataTable = new DataTable();
SqlDataAdapter daGetData = new SqlDataAdapter(SQL, Conn);
daGetData.Fill(dtGetDataTable);
Conn.Close();
daGetData.Dispose();
dtGetDataTable.Dispose();
if (dtGetDataTable != null)
return dtGetDataTable;
else
return null;
}
catch
{
//connGlobal.Dispose();
return null;
}
}
}
the DataTable GetDataTable method, I get the following error:
"The name 'Conn' does not exist in the current context"
How do I make Conn accessible?
I have tried using static, private and public for the constructor, but nothing works.
public class Database
{
public Database()
{
Database GetEnvmtName = new Database();
string strConnString = "Data Source=" + GetEnvmtName.CheckServer() + "; Initial Catalog=" + ConfigurationManager.AppSettings["DefaultDatabase"].ToString() + ";";
SqlConnection Conn = new SqlConnection(strConnString);
}
public DataTable GetDataTable(string SQL, string strDBaseName)
{
try
{
Conn.Open();
DataTable dtGetDataTable = new DataTable();
SqlDataAdapter daGetData = new SqlDataAdapter(SQL, Conn);
daGetData.Fill(dtGetDataTable);
Conn.Close();
daGetData.Dispose();
dtGetDataTable.Dispose();
if (dtGetDataTable != null)
return dtGetDataTable;
else
return null;
}
catch
{
//connGlobal.Dispose();
return null;
}
}
}