1. Create a Class Named as "DataManager".
2. Copy the Following Code and Paste it Into the Class.
3. Then in M_str variable paste your Connection String.
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlConnection desconn = new SqlConnection();
SqlCommand descmd = new SqlCommand();
string M_str = "Paste your Connection String Here";
// Method to get the Connection String and returns the connection object if the
// connection is successfully established
public SqlConnection GetConnection()
{
try
{
conn.Dispose();
conn.ConnectionString = M_str;
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
}
catch (Exception exp)
{
}
return conn;
}
// Method to get the Command Object and returns the object
public SqlCommand getCommand()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
return cmd;
}
// Method to Close the Connection if the connection is Opened
public void CloseConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
else
{
conn.Close();
}
}
public System.Data.ConnectionState GetConnectionState()
{
GetConnection();
return conn.State;
}