To connect to a SQL Server database using the Ado.net custom class MyDBase
Using System;
Using System.Data;
Using System.Data.SqlClient;
public class MyDBase
{
BOOL Ecode=false;
String ES;
SqlConnection cn=new System.Data.SqlClient.SqlConnection ();
DataSet Rs;
Public MyDBase (String mydbservername,string mydatabasename)
{
Ecode = false;
cn. connectionstring= "Workstation id=" +mydbservername+ ";p acket size=4096;integrated security=sspi;data source=" + mydbservername+ ";p ersist security info=false;initial catalog=" +mydatabasename;
Try
{
cn. Open ();
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
}
}
Public MyDBase (String mydbservername, String mydatabasename, String suername, String spasswd)
{
Ecode = false;
String sconn = "Workstation id=" + mydbservername + ";p acket size=4096;user id=" + suername + ";p wd=" + spasswd + ";d ata s Ource= "+ mydbservername +";p ersist security info=false;initial catalog= "+ mydatabasename;
cn. ConnectionString = sconn;
Try
{
cn. Open ();
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
}
}
Public DataSet GetRecordSet (string sqls)
{
SqlCommand sqlcmd= new SqlCommand ();
Sqlcmd.connection = CN;
Sqlcmd.commandtext = Sqls;
Try
{
SqlDataAdapter ADP = new SqlDataAdapter (SQLCMD);
Rs = new DataSet ();
Adp. Fill (Rs);
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
return null;
}
return (Rs);
}
public int Executesqlscalar (string sqls)
{
string S;
SqlCommand sqlcmd= new SqlCommand ();
Sqlcmd.connection = CN;
Sqlcmd.commandtext = Sqls;
Sqlcmd.commandtype = CommandType.Text;
Try
{
s = Sqlcmd.executescalar (). ToString ();
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
return-1;
}
return (int. Parse (s));
}
public string executesqlscalartostring (string sqls)
{
string S;
SqlCommand SQLCMD = new SqlCommand ();
Sqlcmd.connection = CN;
Sqlcmd.commandtext = Sqls;
Sqlcmd.commandtype = CommandType.Text;
Try
{
s = Sqlcmd.executescalar (). ToString ();
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
Return "-1";
}
return s;
}
public string Executesqlwithtrans (string sqls)
{
string S;
SqlTransaction Mytrans;
MYTRANS=CN. BeginTransaction ();
SqlCommand sqlcmd= new SqlCommand ();
Sqlcmd.connection = CN;
Sqlcmd.commandtext = Sqls;
Sqlcmd.commandtype = CommandType.Text;
Sqlcmd.transaction =mytrans;
Sqlcmd.executenonquery ();
sqls= "SELECT @ @IDENTITY as ID";
Sqlcmd.commandtext =sqls;
Try
{
s = Sqlcmd.executescalar (). ToString ();
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
Mytrans.commit ();
Return "";
}
Mytrans.commit ();
return (s);
}
public void ExecuteSQL (string sqls)
{
SqlCommand sqlcmd= new SqlCommand ();
Sqlcmd.connection = CN;
Sqlcmd.commandtext = Sqls;
Sqlcmd.commandtype = CommandType.Text;
Try
{
Sqlcmd.executenonquery ();
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
}
}
Public SqlDataReader DbDataReader (string sqls)
{
SqlCommand sqlcmd= new SqlCommand ();
Sqlcmd.connection = CN;
Sqlcmd.commandtext = Sqls;
Sqlcmd.commandtype = CommandType.Text;
Try
{
Return Sqlcmd.executereader (commandbehavior.closeconnection);
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
return null;
}
}
public void Dbclose ()
{
Try
{
cn. Close ();
}
catch (Exception e)
{
ES = E.message;
Ecode = true;
}
}
public bool ErrorCode ()
{
return ecode;
}
public string Errmessage ()
{
return ES;
}
~mydbase ()
{
if (CN. State==connectionstate.open) cn. Close ();
}
}