阅读背景:

DataBase☞Oracle&&sqlServer

来源:互联网 
using System; using Genersoft.Platform.AppFramework.Service; using System.Xml; using System.IO; using System.Data.SqlClient; using System.Data.OracleClient; using System.Data; namespace xxx { public class DataBase { private ConfigInfo CInfo = new ConfigInfo(); private string ConnStr = string.Empty; private string _DBTYPE = string.Empty; public string DBTYPE { get { return _DBTYPE; } set { _DBTYPE = value; } } private SqlConnection conn; private OracleConnection oraconn; private SqlTransaction sqltrans; private SqlCommand sqlcom; private OracleTransaction oratrans; private OracleCommand oracom; private SqlDataAdapter sqlAdp; private OracleDataAdapter oraAdp; public DataBase() { CInfo = GetInfoFromXML(); //<DATABASE Code="01" DbType="Oracle" UserId="数据库名" Password="密码" Source="服务名" Catalog="" Version="1.0" /> DBTYPE = CInfo.dbType; switch (DBTYPE) { case "Oracle": ConnStr = "User ID=" + CInfo.userID + ";password=" + CInfo.password + ";Data Source=" + CInfo.source; WriteLog(ConnStr); break; case "SQLServer": ConnStr = "server=" + CInfo.source + ";uid=" + CInfo.userID + ";pwd=" + CInfo.password + ";database=" + CInfo.catalog; break; } } protected void Connection() { switch (DBTYPE) { case "Oracle": try { oraconn = new OracleConnection(ConnStr); oraconn.Open(); WriteLog("33333333"); } catch (Exception ex) { throw new Exception(ex.ToString()); } break; case "SQLServer": try { conn = new SqlConnection(ConnStr); conn.Open(); } catch (Exception ex) { throw new Exception(ex.ToString()); } break; default: break; } } public void ExecuteSQL(string sql) { Connection(); switch (DBTYPE) { case "Oracle": try { oratrans = oraconn.BeginTransaction(); oracom = new OracleCommand(sql, oraconn); oracom.Transaction = oratrans; oracom.ExecuteNonQuery(); } catch(Exception ex) { oratrans.Rollback(); throw new Exception(ex.ToString()); } finally { oratrans.Commit(); oraconn.Close(); } break; case "SQLServer": try { sqltrans = conn.BeginTransaction(); sqlcom = new SqlCommand(sql, conn); sqlcom.Transaction = sqltrans; sqlcom.ExecuteNonQuery(); } catch (Exception ex) { sqltrans.Rollback(); throw new Exception(ex.ToString()); } finally { sqltrans.Commit(); conn.Close(); } break; default: break; } } public DataSet ExecuteDataSet(string sql) { Connection(); DataSet ds = new DataSet(); switch (DBTYPE) { case "Oracle": try { oraAdp = new OracleDataAdapter(sql, oraconn); oraAdp.Fill(ds); WriteLog(ds.Tables.Count.ToString()); } catch (Exception ex) { throw new Exception(ex.ToString()); } finally { oraconn.Close(); } break; case "SQLServer": try { sqlAdp = new SqlDataAdapter(sql, conn); sqlAdp.Fill(ds); } catch (Exception ex) { throw new Exception(ex.ToString()); } finally { conn.Close(); } break; default: break; } return ds; } public ConfigInfo GetInfoFromXML() { //读取config文件的数据库配置信息 ConfigInfo info = new ConfigInfo(); string serverInstallpath = string.Empty; string xmlPath = string.Empty; serverInstallpath = GSPContext.Current.ServerInstallPath.ToString(); xmlPath = Path.Combine(serverInstallpath, "bscw_local\Config\xxx.Config"); XmlDocument xmls = new XmlDocument(); try { xmls.Load(xmlPath); WriteLog(xmlPath); XmlNodeList xmlList = xmls.SelectSingleNode("//DbConfig").ChildNodes; foreach (XmlNode node in xmlList) { XmlElement element = (XmlElement)node; WriteLog(element.Name.ToString()); if (element.Name == "DATABASE") { try { WriteLog(element.GetAttribute("CODE").Trim()); info.code = element.GetAttribute("CODE").Trim(); info.dbType = element.GetAttribute("DBTYPE").Trim(); info.userID = element.GetAttribute("USERID").Trim(); info.password = element.GetAttribute("PASSWORD").Trim(); info.source = element.GetAttribute("SOURCE").Trim(); info.catalog = element.GetAttribute("CATALOG").Trim(); info.version = element.GetAttribute("VERSION").Trim(); WriteLog(info.code + info.dbType + info.userID + info.password + info.source + info.catalog + info.version); } catch(Exception ex) { WriteLog(ex.ToString()); } } } } catch (Exception ex) { WriteLog(ex.ToString()); throw new Exception(ex.ToString()); } return info; } public class ConfigInfo { public ConfigInfo() { } private string _code = string.Empty; private string _dbType = string.Empty; private string _userID = string.Empty; private string _password = string.Empty; private string _source = string.Empty; private string _catalog = string.Empty; private string _version = string.Empty; public string code { get { return _code; } set { _code = value; } } public string dbType { get { return _dbType; } set { _dbType = value; } } public string userID { get { return _userID; } set { _userID = value; } } public string password { get { return _password; } set { _password = value; } } public string source { get { return _source; } set { _source = value; } } public string catalog { get { return _catalog; } set { _catalog = value; } } public string version { get { return _version; } set { _version = value; } } } #region 日志打印 public static void WriteLog(string strLog) { string sFilePath = "C:\" + DateTime.Now.ToString("yyyyMM"); string sFileName = "TMATWebServiceLog" + DateTime.Now.ToString("dd") + ".log"; sFileName = sFilePath + "\" + sFileName; //文件的绝对路径 if (!Directory.Exists(sFilePath))//验证路径是否存在 { Directory.CreateDirectory(sFilePath); //不存在则创建 } FileStream fs; StreamWriter sw; if (File.Exists(sFileName)) //验证文件是否存在,有则追加,无则创建 { fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write); } else { fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write); } sw = new StreamWriter(fs); sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " --- " + strLog); sw.Close(); fs.Close(); } #endregion } } using System; using Genersoft.Platform.AppFramewo



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: