> 데이터 베이스 > MySQL 튜토리얼 > 使用MyDBase连接SQL Server

使用MyDBase连接SQL Server

WBOY
풀어 주다: 2016-06-07 14:53:24
원래의
1380명이 탐색했습니다.

使用ADO.NET自定义类MyDBase连接SQL Server数据库 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; publ

  使用ADO.NET自定义类MyDBase连接SQL Server数据库

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿