📄 frameconn.cs
字号:
namespace FrameCountry
{
using System;
using System.Data;
using System.Data.OleDb;
using System.Xml;
internal class FrameConn
{
private string ClassAlias = "CN";
private OleDbCommand Com;
private string configInfo;
private OleDbConnection Conn;
private int connTimeout = 0;
private string currentSQLL = "";
private string dBType;
private FrameCity frameCity = new FrameCity();
private FrameNode frameNode = new FrameNode();
private FramePolice framePolice = new FramePolice();
private OleDbTransaction Transaction;
private string transactionContent = "";
public bool CloseConn(ref string ErrorInfo)
{
string functionAlias = "04";
try
{
if (this.Conn.State == ConnectionState.Open)
{
this.Conn.Dispose();
}
}
catch (Exception exception)
{
ErrorInfo = "断开数据库连接错误!\r\n错误信息如下:\n\r" + exception.Message;
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, ErrorInfo);
return false;
}
return true;
}
public bool ExeNonQuery(string ExeString, ref string ErrorInfo)
{
string functionAlias = "07";
try
{
string errorInfo = "";
if ((this.Conn.State != ConnectionState.Open) && !this.StartConn(ref errorInfo))
{
throw new Exception("◇" + errorInfo);
}
if (this.Transaction == null)
{
throw new Exception("事务未启动...");
}
this.Com.CommandText = ExeString;
this.transactionContent = this.transactionContent + "//transactionContent\n\r" + ExeString;
this.CurrentSQLL = ExeString;
this.Com.ExecuteNonQuery();
}
catch (Exception exception)
{
string str3 = "";
this.TransactionRollback(ref str3);
ErrorInfo = "执行非查询SQL语句:\n\r" + ExeString.Trim() + " 错误!\n\r错误信息如下:\n\r" + exception.Message;
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, ErrorInfo);
return false;
}
return true;
}
public bool ExeQuery(string ExeString, ref DataSet ds, ref string ErrorInfo)
{
string functionAlias = "08";
try
{
string errorInfo = "";
this.transactionContent = "";
if ((this.Conn.State != ConnectionState.Open) && !this.StartConn(ref errorInfo))
{
throw new Exception("◇" + errorInfo);
}
this.Com.CommandText = ExeString;
this.CurrentSQLL = ExeString;
this.Com.ExecuteNonQuery();
DataSet dataSet = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = this.Com;
adapter.Fill(dataSet);
ds = dataSet;
}
catch (Exception exception)
{
string str3 = "";
this.TransactionRollback(ref str3);
ErrorInfo = "执行查询SQL语句:\n\r" + ExeString.Trim() + " 错误!\n\r错误信息如下:\n\r" + exception.Message;
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, ErrorInfo);
return false;
}
return true;
}
public bool ExeQueryExtend(string ExeString, ref DataSet ds, string TableName, ref string ErrorInfo)
{
string functionAlias = "09";
try
{
string errorInfo = "";
if ((this.Conn.State != ConnectionState.Open) && !this.StartConn(ref errorInfo))
{
throw new Exception("◇" + errorInfo);
}
this.Com.CommandText = ExeString;
this.CurrentSQLL = ExeString;
this.Com.ExecuteNonQuery();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = this.Com;
adapter.Fill(ds.Tables[TableName]);
}
catch (Exception exception)
{
string str3 = "";
this.TransactionRollback(ref str3);
ErrorInfo = "执行扩展查询SQL语句:\n\r" + ExeString.Trim() + " 错误!\n\r错误信息如下:\n\r" + exception.Message + "\n\r\n\r也可能违反DataSet数据集约定!";
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, ErrorInfo);
return false;
}
return true;
}
private bool GetConnString(ref string ConnString, ref string ErrorInfo)
{
string functionAlias = "05";
string errorInfo = "";
string innerText = "";
string str4 = "";
try
{
str4 = "分解 Datebase 节点错误:\n\r";
XmlNode node = this.frameNode.GetNode(this.configInfo, "Datebase", ref errorInfo);
if (node == null)
{
throw new Exception("◇" + str4 + errorInfo);
}
str4 = "分解 DBString 节点错误:\n\r";
try
{
innerText = node.SelectNodes("./DBString")[0].InnerText;
}
catch (Exception exception)
{
throw new Exception(str4 + exception.Message);
}
str4 = "分解 DBTimeout 节点错误:\n\r";
try
{
this.connTimeout = int.Parse(node.SelectNodes("./DBTimeout")[0].InnerText);
}
catch (Exception exception2)
{
throw new Exception(str4 + exception2.Message);
}
if (!this.framePolice.CheckRegister(this.configInfo, ref errorInfo))
{
throw new Exception("◇" + errorInfo);
}
}
catch (Exception exception3)
{
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, exception3.Message);
return false;
}
ConnString = innerText;
return true;
}
public bool StartConn(ref string ErrorInfo)
{
string functionAlias = "06";
string connString = "";
string errorInfo = "";
try
{
if (!this.GetConnString(ref connString, ref errorInfo))
{
throw new Exception("◇" + errorInfo);
}
this.Conn = new OleDbConnection(connString);
this.Conn.Open();
this.Com = this.Conn.CreateCommand();
switch (this.Conn.Provider)
{
case "OraOLEDB.Oracle":
if (!this.framePolice.allowOracle)
{
throw new Exception("不允许访问 Oracle 数据库!");
}
this.dBType = "Oracle";
goto Label_0204;
case "MSDAORA.1":
if (!this.framePolice.allowOracle)
{
throw new Exception("不允许访问 Oracle 数据库!");
}
this.dBType = "Oracle";
goto Label_0204;
case "SQLOLEDB.1":
if (!this.framePolice.allowSQLServer)
{
throw new Exception("不允许访问 SQLServer 数据库!");
}
this.dBType = "SQLServer";
goto Label_0204;
case "IBMDADB2.1":
if (!this.framePolice.allowDB2)
{
throw new Exception("不允许访问 DB2 数据库!");
}
this.dBType = "DB2";
goto Label_0204;
case "Microsoft.Jet.OLEDB.4.0":
if (!this.framePolice.allowAccess2000)
{
throw new Exception("不允许访问 Access2000 数据库!");
}
this.dBType = "Access2000";
goto Label_0204;
case "MSDataShape":
if (!this.framePolice.allowAccess2000)
{
throw new Exception("不允许访问 Access2000 数据库!");
}
this.dBType = "Access2000";
goto Label_0204;
}
throw new Exception("数据源 " + this.Conn.Provider + " 无法操作!");
}
catch (Exception exception)
{
ErrorInfo = exception.Message;
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, ErrorInfo);
return false;
}
Label_0204:
return true;
}
public bool TransactionCommit(ref string ErrorInfo)
{
string functionAlias = "11";
try
{
if (this.Transaction == null)
{
throw new Exception("事务未启动!");
}
this.Transaction.Commit();
this.Transaction = null;
this.transactionContent = "";
}
catch (Exception exception)
{
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, exception.Message);
return false;
}
return true;
}
public bool TransactionRollback(ref string ErrorInfo)
{
string functionAlias = "12";
try
{
if (this.Transaction == null)
{
throw new Exception("事务未启动!");
}
this.Transaction.Rollback();
this.Transaction = null;
}
catch (Exception exception)
{
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, exception.Message);
return false;
}
return true;
}
public bool TransactionStart(ref string ErrorInfo)
{
string functionAlias = "10";
try
{
if (this.Transaction != null)
{
throw new Exception("事务已启动!");
}
this.Transaction = this.Conn.BeginTransaction();
this.Com.Transaction = this.Transaction;
this.transactionContent = "";
}
catch (Exception exception)
{
ErrorInfo = this.frameCity.DealErrorInfo(this.ClassAlias, functionAlias, exception.Message);
return false;
}
return true;
}
public string ConfigInfo
{
get
{
return this.configInfo;
}
set
{
this.configInfo = value;
}
}
public int ConnTimeout
{
get
{
return this.connTimeout;
}
set
{
this.connTimeout = value;
}
}
public string CurrentSQLL
{
get
{
return this.currentSQLL;
}
set
{
this.currentSQLL = value;
}
}
public string DBType
{
get
{
return this.dBType;
}
set
{
this.dBType = value;
}
}
public string TransactionContent
{
get
{
return this.transactionContent;
}
set
{
this.transactionContent = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -