C#winform建立一个共用的数据库连接

 using System; using System.Xml; using System.Data.OleDb; using System.Data; namespace ACS { /// <summary> /// 對數據庫進行更新 /// </summary> /// AIDU數據庫連接 /// Retrieval 返回查詢Dataset /// Translation與數據庫進行交易[Access = true][fail = false] /// public class AIDU { #region 連接失敗返回失敗消息--[Link Ok return null][Link fail return ErrorMessage] public string ErrorMsg() { return sErrorMsg; } #endregion #region 設置公共連接對像字串--[ sAcsCon ] public string sAcsConString() { string sConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source ="+@"KSZH.mdb"; // string sConString = "Provider=OraOLEDB.Oracle.1;Password=mes_ks1;Persist Security Info=True;User ID=mes_ks1;Data Source=linux;Extended Properties="; return sConString; } public string sAcsCon { get{return sAcsConString();} } #endregion #region 設置公共連接對像--[ Link_cn ] public OleDbConnection Link_cn { get{return Acs_cn();} } public OleDbConnection Acs_cn() { OleDbConnection cn = new OleDbConnection(sAcsCon); if(cn.State == ConnectionState.Closed) { cn.Open(); } return cn; } #endregion //連接失敗後的返回消息 private string sErrorMsg ="" ; //連接標志變量 [bFlag = true Connection Database Success] //[bFlag = false Connection Database unSuccess] private bool bFlag = false; #region 數據庫連接--[ LINK-TEST ] public bool AIDU_Link() { OleDbConnection TestCn = new OleDbConnection(sAcsCon); //測試數據庫連接 try { if(TestCn.State != ConnectionState.Open) { TestCn.Open(); bFlag = true; } } catch(Exception e) { sErrorMsg = e.Message; } finally { if(TestCn.State == ConnectionState.Open) { TestCn.Close(); } } return bFlag; } #endregion #region 資料查詢--[ SELECT ] public DataSet Select(string sCmdString) { //建立CONNECTION對像 OleDbConnection cn1=new OleDbConnection(sAcsCon); //建立DATASET對像,做資料返回值 DataSet oDs = new DataSet(); try {//建立Adapter對像 OleDbDataAdapter myAdapter = new OleDbDataAdapter(sCmdString,cn1); myAdapter.Fill(oDs); } catch { cn1.Close(); } return oDs; } #endregion #region 數據資料更新--[ Update ] //執行--[ UPDATE ][ INSERT ][ DELETE ] //[ The command Execute sccess return 1] //[ The command Execute unSuccess return -1] public int UpdateCmd(string CommandString) { int nResult = -1; //進行一個本地交易 // OleDbTransaction myTran = Link_cn.BeginTransaction(IsolationLevel.ReadCommitted); // myTran.Begin(); //oledbcommand 命令執行交易 OleDbCommand myCmd = new OleDbCommand(CommandString,Link_cn); //和數據庫進這行數據交易 try { //提交數據 myCmd.ExecuteNonQuery(); // myTran.Commit(); nResult = 1; } catch(OleDbException oe) { //出錯數據回滾原來的狀態,保持數據庫一致性 // myTran.Rollback(); nResult = -1; throw oe; } finally { //執行交易後關閉聯接 Link_cn.Close(); } return nResult; } #endregion } #region 處理字符,以符合SQL語句用--[ class sTringAdd ] public class sTringAdd { public sTringAdd(){} public string SQLQ(string sText) { sText = "'"+sText+"'"; return sText; } public string SQLQ(int n) { return n.ToString(); } public string SQLQ(double n) { return n.ToString(); } public string SQLQC(string sText) { sText = "'"+sText+"',"; return sText; } public string SQLQC(int nText) { string sText = nText.ToString()+","; return sText; } public string SQLQC(double nText) { string sText = nText.ToString()+","; return sText; } } #endregion }

你可能感兴趣的:(数据库,String,command,database,dataset,translation)