Use the custom ADO. NET class MyDBase to connect to the SQL Server database
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 + "; packet size = 4096; integrated security = SSPI; data source =" + MyDBServerName + "; persist 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 + "; packet size = 4096; user id =" + sUerName + "; pwd =" + sPasswd + "; data source = "+ MyDBServerName +"; persist 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 ();
}
}