首页 > 数据库 > mysql教程 > 使用MyDBase连接SQL Server

使用MyDBase连接SQL Server

WBOY
发布: 2016-06-07 14:53:24
原创
1378 人浏览过

使用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();

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板