使用ADO.NET自定义类MyDBase连接SQL Server数据库使用系统;使用系统数据;使用 System.Data.SqlClient;公共类 MyDBase { bool ECode=false;字符串ES; SqlConnection cn=new System.Data.SqlClient.SqlConnection();数据集卢比;发布
使用ADO.NET自定义类MyDBase连接SQL Server数据库
使用系统;
使用 System.Data;
使用 System.Data.SqlClient;
公共类MyDBase
bool ECode=false;
字符串 ES;
SqlConnection cn=new System.Data.SqlClient.SqlConnection();
数据集 Rs;
公共MyDBase(string MyDBServerName,string MyDataBaseName)
ECode = false;
cn.ConnectionString =“workstation id =”MyDBServerName“;数据包大小= 4096;集成安全= SSPI;数据源=“MyDBServerName”;persist security info = False;初始目录=“MyDataBaseName;
尝试
赶上(例外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; 尝试
赶上(例外e)
ES = e.Message; ECode = true; public DataSet GetRecordset(string Sqls)
SqlCommand sqlCmd= new SqlCommand(); sqlCmd.Connection = cn; sqlCmd.CommandText = Sqls; 尝试
SqlDataAdapter adp = new SqlDataAdapter(sqlCmd); Rs = new DataSet(); adp.Fill(Rs); 赶上(例外e)
ES = e.Message; ECode = true; 返回空; 返回(卢比); public int ExecuteSQLScalar(string Sqls)
SqlCommand sqlCmd= new SqlCommand(); sqlCmd.Connection = cn; sqlCmd.CommandText = Sqls; sqlCmd.CommandType = CommandType.Text; 尝试
s = sqlCmd.ExecuteScalar().ToString(); 赶上(例外e)
ES = e.Message; ECode = true; 返回-1; 返回(int.Parse(s)); 公共字符串ExecuteSQLScalarTOstring(string Sqls)
SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = cn; sqlCmd.CommandText = Sqls; sqlCmd.CommandType = CommandType.Text; 尝试
s = sqlCmd.ExecuteScalar().ToString(); 赶上(例外e)
ES = e.Message; ECode = true; 返回“-1”; 返回; public string ExecuteSQLWithTrans(string Sqls)
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; 尝试
s = sqlCmd.ExecuteScalar().ToString(); 赶上(例外e)
ES = e.Message; ECode = true; myTrans.Commit(); 返回“”; myTrans.Commit(); 返回; public void ExecuteSQL(string Sqls)
SqlCommand sqlCmd= new SqlCommand(); sqlCmd.Connection = cn; sqlCmd.CommandText = Sqls; sqlCmd.CommandType = CommandType.Text; 尝试
sqlCmd.ExecuteNonQuery(); 赶上(例外e)
ES = e.Message; ECode = true; 公共 SqlDataReader DBDataReader(string Sqls)
SqlCommand sqlCmd= new SqlCommand(); sqlCmd.Connection = cn; sqlCmd.CommandText = Sqls; sqlCmd.CommandType = CommandType.Text; 尝试
返回sqlCmd.ExecuteReader(CommandBehavior.CloseConnection); 赶上(例外e)
ES = e.Message; ECode = true; 返回空; public void DBClose()
尝试
赶上(例外e)
ES = e.Message; ECode = true; public bool ErrorCode()
返回ECode; 公共字符串ErrMessage()
返回ES; ~MyDBase()
//if (cn.State==ConnectionState.Open ) cn.Close();