2023年6月21日发(作者:)
C#各种帮助类⼤全前⾔此篇专门记录⼀些常见DB帮助类及其他帮助类,以便使⽤时不⽤重复造轮⼦。DBHelper帮助类①⾸当其冲的就是Sql Server帮助类,创建名为DbHelperSQL 的类 ,全部代码如下:/// /// 数据访问抽象基础类 /// Copyright (C) Maticsoft
/// public abstract class DbHelperSQL { //数据库连接字符串(来配置),多数据库可使⽤DbHelperSQLP来实现. public static string connectionString = tionString;
public DbHelperSQL() {
} #region 公⽤⽅法 /// /// 判断是否存在某表的某个字段 /// /// 表名称 /// 列名称 /// 是否存在 public static bool ColumnExists(string tableName, string columnName) { string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'"; object res = GetSingle(sql); if (res == null) { return false; } return 32(res) > 0; } public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); //也可能=0 } if (cmdresult == 0) { return false; } else { return true; } } /// /// 表是否存在 /// /// /// public static bool TabExists(string TableName) { string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1"; //string strsql = "SELECT count(*) FROM s WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')"; object obj = GetSingle(strsql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } public static bool Exists(string strSql, params SqlParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } #endregion #region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); int rows = eNonQuery(); return rows; } catch (eption e) { (); throw e; } } } } public static int ExecuteSqlByTime(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); dTimeout = Times; int rows = eNonQuery(); return rows; } catch (eption e) { (); throw e; } } } }
/// /// 执⾏Sql和Oracle滴混合事务 /// /// SQL命令⾏列表 /// Oracle命令⾏列表 /// 执⾏结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执⾏成功 public static int ExecuteSqlTran(List list, List oracleCmdSqlList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); SqlCommand cmd = new SqlCommand(); tion = conn; SqlTransaction tx = ransaction(); ction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = dText; SqlParameter[] cmdParms = (SqlParameter[])ters; PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (NextType == tationEvent) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("违背要求"+dText+"必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (isHave) { //引发事件 citationEvent(); } } if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("SQL:违背要求" + dText + "必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须⼤于0"); //return 0; } if (NextType == HaveContine && isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须等于0"); //return 0; } continue; } int val = eNonQuery(); if (NextType == EffectRows && val == 0) { ck(); throw new Exception("SQL:违背要求" + dText + "必须有影响⾏"); //return 0; } (); } string oraConnectionString = nectionString("ConnectionStringPPC"); bool res = eSqlTran(oraConnectionString, oracleCmdSqlList); if (!res) { ck(); throw new Exception("Oracle执⾏失败"); // return -1; } (); return 1; } catch (eption e) { ck(); throw e; } catch (Exception e) { ck(); throw e; } } }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static int ExecuteSqlTran(List SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); SqlCommand cmd = new SqlCommand(); tion = conn; SqlTransaction tx = ransaction(); ction = tx; try { int count = 0; for (int n = 0; n < ; n++) { string strsql = SQLStringList[n]; if (().Length > 1) { dText = strsql; count += eNonQuery(); } } (); return count; } catch { ck(); return 0; } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString, string content) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, connection); ameter myParameter = new ameter("@content", ); = content; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (eption e) { throw e; } finally { e(); (); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static object ExecuteSqlGet(string SQLString, string content) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, connection); ameter myParameter = new ameter("@content", ); = content; (myParameter); try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { throw e; } finally { e(); (); } } } /// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(strSQL, connection); ameter myParameter = new ameter("@fs", ); = fs; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (eption e) { throw e; } finally { e(); (); } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { (); throw e; } } } } public static object GetSingle(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); dTimeout = Times; object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { (); throw e; } } } } /// /// 执⾏查询语句,返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// SqlDataReader public static SqlDataReader ExecuteReader(string strSQL) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(strSQL, connection); try { (); SqlDataReader myReader = eReader(onnection); return myReader; } catch (eption e) { throw e; }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); (ds, "ds"); } catch (eption ex) { throw new Exception(e); } return ds; } } public static DataSet Query(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); dTimeout = Times; (ds, "ds"); } catch (eption ex) { throw new Exception(e); } return ds; } } #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = eNonQuery(); (); return rows; } catch (eption e) { throw e; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); SqlParameter[] cmdParms = (SqlParameter[]); PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static int ExecuteSqlTran( cmdList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { int count = 0; //循环 foreach (CommandInfo myDE in cmdList) { string cmdText = dText; SqlParameter[] cmdParms = (SqlParameter[])ters; PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); return 0; } if (NextType == HaveContine && isHave) { ck(); return 0; } continue; } int val = eNonQuery(); count += val; if (NextType == EffectRows && val == 0) { ck(); return 0; } (); } (); return count; } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static void ExecuteSqlTranWithIndentity( SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { int indentity = 0; //循环 foreach (CommandInfo myDE in SQLStringList) { string cmdText = dText; SqlParameter[] cmdParms = (SqlParameter[])ters; foreach (SqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (SqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { int indentity = 0; //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); SqlParameter[] cmdParms = (SqlParameter[]); foreach (SqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (SqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = eScalar(); (); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { throw e; } } } } /// /// 执⾏查询语句,返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// SqlDataReader public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); SqlDataReader myReader = eReader(onnection); (); return myReader; } catch (eption e) { throw e; } // finally // { // e(); // (); // }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { (ds, "ds"); (); } catch (eption ex) { throw new Exception(e); } return ds; } } } private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms) { if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { if ((ion == utput || ion == ) && ( == null)) { = ; } (parameter); } } } #endregion #region 存储过程操作 /// /// 执⾏存储过程,返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 存储过程名 /// 存储过程参数 /// SqlDataReader public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters) { SqlConnection connection = new SqlConnection(connectionString); SqlDataReader returnReader; (); SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); dType = Procedure; returnReader = eReader(onnection); return returnReader;
} /// /// 执⾏存储过程 /// /// 存储过程名 /// 存储过程参数 /// DataSet结果中的表名 /// DataSet public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); (); SqlDataAdapter sqlDA = new SqlDataAdapter(); Command = BuildQueryCommand(connection, storedProcName, parameters); (dataSet, tableName); (); return dataSet; } } public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); (); SqlDataAdapter sqlDA = new SqlDataAdapter(); Command = BuildQueryCommand(connection, storedProcName, parameters); dTimeout = Times; (dataSet, tableName); (); return dataSet; } } /// /// 构建 SqlCommand 对象(⽤来返回⼀个结果集,⽽不是⼀个整数值) /// /// 数据库连接 /// 存储过程名 /// 存储过程参数 /// SqlCommand private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(storedProcName, connection); dType = Procedure; foreach (SqlParameter parameter in parameters) { if (parameter != null) { // 检查未分配值的输出参数,将其分配以. if ((ion == utput || ion == ) && ( == null)) { = ; } (parameter); } } return command; } /// /// 执⾏存储过程,返回影响的⾏数
/// /// 存储过程名 /// 存储过程参数 /// 影响的⾏数 /// public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected) { using (SqlConnection connection = new SqlConnection(connectionString)) { int result; (); SqlCommand command = BuildIntCommand(connection, storedProcName, parameters); rowsAffected = eNonQuery(); result = (int)ters["ReturnValue"].Value; //(); return result; } } /// /// 创建 SqlCommand 对象实例(⽤来返回⼀个整数值)
/// /// 存储过程名 /// 存储过程参数 /// SqlCommand 对象实例 private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); (new SqlParameter("ReturnValue", , 4, Value, false, 0, 0, , t, null)); return command; } #endregion }View Code注意:根据提⽰引⽤相关命名空间即可。上述代码中⽤到了⼀个PubConstant类 ⽤来获取连接字符串,全部代码如下: public class PubConstant {
/// /// 获取连接字符串 /// public static string ConnectionString {
get
{ string _connectionString = tings["ConnectionString"];
string ConStringEncrypt = tings["ConStringEncrypt"]; if (ConStringEncrypt == "true") { _connectionString = t(_connectionString); } return _connectionString;
} } /// /// 得到⾥配置项的数据库连接字符串。 /// /// /// public static string GetConnectionString(string configName) { string connectionString = tings[configName]; string ConStringEncrypt = tings["ConStringEncrypt"]; if (ConStringEncrypt == "true") { connectionString = t(connectionString); } return connectionString; } }View CodePubConstant类中对连接字符串进⾏了DES加密解密操作,DES加密/解密帮助类全部代码如下:/// /// DES加密/解密类。 /// public class DESEncrypt { public DESEncrypt() {
} #region ========加密========
/// /// 加密 /// /// /// public static string Encrypt(string Text)
{ return Encrypt(Text, "MATICSOFT"); } ///
/// 加密数据
///
///
///
///
public static string Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=es(Text);
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
Stream ms=new Stream();
CryptoStream cs=new CryptoStream(ms,Encryptor(),);
(inputByteArray,0,);
inalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in y())
{
Format("{0:X2}",b);
}
return ng();
}
#endregion
#region ========解密========
/// /// 解密 /// /// /// public static string Decrypt(string Text)
{ return Decrypt(Text, "MATICSOFT"); } ///
/// 解密数据
///
///
///
///
public static string Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len=/2;
byte[] inputByteArray = new byte[len];
int x,i;
for(x=0;x {
i = 32(ing(x * 2, 2), 16);
inputByteArray[x]=(byte)i;
}
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
Stream ms=new Stream();
CryptoStream cs=new CryptoStream(ms,Decryptor(),);
(inputByteArray,0,); inalBlock();
return ing(y());
}
#endregion
}View Code
强烈建议使⽤ESC加密全部代码如下: public class DESCrypto { private static string key = "KABCMS2017"; /// /// 加密(UTF-8) /// /// 待加密字符串 /// 密钥(长度不能少于8位字符) /// public static string Encode(string str, string secKey = "") { if (OrEmpty(secKey)) secKey = key; else if ( < 8) return ""; DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); = es(ing(0, 8)); = es(ing(0, 8)); byte[] bytes = oding("UTF-8").GetBytes(str); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, Encryptor(), ); (bytes, 0, ); inalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in y()) { Format("{0:X2}", num); } (); return ng(); } /// /// 解密(UTF-8) /// /// 待解密字符串 /// 密钥(长度不能少于8位字符) /// public static string Decode(string str, string secKey = "") { if (OrEmpty(secKey)) secKey = key; else if ( < 8) return ""; DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); = es(ing(0, 8)); = es(ing(0, 8)); byte[] buffer = new byte[ / 2]; for (int i = 0; i < ( / 2); i++) { int num2 = 32(ing(i * 2, 2), 0x10); buffer[i] = (byte)num2; } MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, Decryptor(), ); (buffer, 0, ); inalBlock(); (); return oding("UTF-8").GetString(y()); } }View Code
然后再配置⽂件中新建⼏个字符串链接,分别连接不同的库,代码如下: -- MySql -- SqlServer -- SqlLole使⽤ /// /// 根据账号获取信息 /// /// /// public ServiceProvider GetNameByQR(string name) { try { string sql = "select 字段1, 字段2, 字段3 from viw_promotion where name='" + name + "'"; DataSet ds = (sql);//此处调⽤MysqlDBHelper帮助类 DataTable dt = [0]; ServiceProvider ProviderQR = new ServiceProvider(); foreach (DataRow item in ) { if (!OrEmpty(item["字段1"].ToString())) { tName = item["字段1"].ToString(); } if (!OrEmpty(item["字段2"].ToString())) { tyURL = item["字段2"].ToString(); } if (!OrEmpty(item["字段3"].ToString())) { tyQRCode = item["字段3"].ToString(); } } return ProviderQR; //返回实体对象 } catch (Exception) { return null; } }② DbHelperMySQL帮助类,全部代码如下:
/// /// 数据访问抽象基础类 /// Copyright (C) Maticsoft /// public abstract class DbHelperMySQL { //数据库连接字符串(来配置),可以动态更改connectionString⽀持多数据库.
// public static string connectionString = tionString;Mysql_ConnectionString,ConnectionString1 public static string connectionString = tings["ConnectionString1"]; public DbHelperMySQL() {
} #region 公⽤⽅法 /// /// 得到最⼤值 /// /// /// /// public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } /// /// 是否存在 /// /// /// public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } }
/// /// 是否存在(基于MySqlParameter) /// /// /// /// public static bool Exists(string strSql, params MySqlParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } #endregion #region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); int rows = eNonQuery(); return rows; } catch (xception e) { (); throw e; } } } } public static int ExecuteSqlByTime(string SQLString, int Times) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); dTimeout = Times; int rows = eNonQuery(); return rows; } catch (xception e) { (); throw e; } } } }
/// /// 执⾏MySql和Oracle滴混合事务 /// /// SQL命令⾏列表 /// Oracle命令⾏列表 /// 执⾏结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执⾏成功 public static int ExecuteSqlTran(List list, List oracleCmdSqlList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); MySqlCommand cmd = new MySqlCommand(); tion = conn; MySqlTransaction tx = ransaction(); ction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = dText; MySqlParameter[] cmdParms = (MySqlParameter[])ters; PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (NextType == tationEvent) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("违背要求"+dText+"必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (isHave) { //引发事件 citationEvent(); } } if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("SQL:违背要求" + dText + "必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须⼤于0"); //return 0; } if (NextType == HaveContine && isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须等于0"); //return 0; } continue; } int val = eNonQuery(); if (NextType == EffectRows && val == 0) { ck(); throw new Exception("SQL:违背要求" + dText + "必须有影响⾏"); //return 0; } (); } string oraConnectionString = nectionString("ConnectionStringPPC"); bool res = eSqlTran(oraConnectionString, oracleCmdSqlList); if (!res) { ck(); throw new Exception("执⾏失败"); // return -1; } (); return 1; } catch (xception e) { ck(); throw e; } catch (Exception e) { ck(); throw e; } } }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static int ExecuteSqlTran(List SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); MySqlCommand cmd = new MySqlCommand(); tion = conn; MySqlTransaction tx = ransaction(); ction = tx; try { int count = 0; for (int n = 0; n < ; n++) { string strsql = SQLStringList[n]; if (().Length > 1) { dText = strsql; count += eNonQuery(); } } (); return count; } catch { ck(); return 0; } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString, string content) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(SQLString, connection); arameter myParameter = new arameter("@content", ); = content; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (xception e) { throw e; } finally { e(); (); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static object ExecuteSqlGet(string SQLString, string content) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(SQLString, connection); arameter myParameter = new arameter("@content", ); = content; (myParameter); try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { throw e; } finally { e(); (); } } } /// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(strSQL, connection); arameter myParameter = new arameter("@fs", ); = fs; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (xception e) { throw e; } finally { e(); (); } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { (); throw e; } } } } public static object GetSingle(string SQLString, int Times) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); dTimeout = Times; object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { (); throw e; } } } } /// /// 执⾏查询语句,返回MySqlDataReader ( 注意:调⽤该⽅法后,⼀定要对MySqlDataReader进⾏Close ) /// /// 查询语句 /// MySqlDataReader public static MySqlDataReader ExecuteReader(string strSQL) { MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand cmd = new MySqlCommand(strSQL, connection); try { (); MySqlDataReader myReader = eReader(onnection); return myReader; } catch (xception e) { throw e; }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); (ds, "ds"); } catch (xception ex) { throw new Exception(e); } return ds; } } public static DataSet Query(string SQLString, int Times) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); dTimeout = Times; (ds, "ds"); } catch (xception ex) { throw new Exception(e); } return ds; } } #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = eNonQuery(); (); return rows; } catch (xception e) { throw e; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); MySqlParameter[] cmdParms = (MySqlParameter[]); PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static int ExecuteSqlTran( cmdList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { int count = 0; //循环 foreach (CommandInfo myDE in cmdList) { string cmdText = dText; MySqlParameter[] cmdParms = (MySqlParameter[])ters; PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); return 0; } if (NextType == HaveContine && isHave) { ck(); return 0; } continue; } int val = eNonQuery(); count += val; if (NextType == EffectRows && val == 0) { ck(); return 0; } (); } (); return count; } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static void ExecuteSqlTranWithIndentity( SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { int indentity = 0; //循环 foreach (CommandInfo myDE in SQLStringList) { string cmdText = dText; MySqlParameter[] cmdParms = (MySqlParameter[])ters; foreach (MySqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (MySqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { int indentity = 0; //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); MySqlParameter[] cmdParms = (MySqlParameter[]); foreach (MySqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (MySqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = eScalar(); (); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { throw e; } } } } /// /// 执⾏查询语句,返回MySqlDataReader ( 注意:调⽤该⽅法后,⼀定要对MySqlDataReader进⾏Close ) /// /// 查询语句 /// MySqlDataReader public static MySqlDataReader ExecuteReader(string SQLString, params MySqlParameter[] cmdParms) { MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand cmd = new MySqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); MySqlDataReader myReader = eReader(onnection); (); return myReader; } catch (xception e) { throw e; } // finally // { // e(); // (); // }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (MySqlDataAdapter da = new MySqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { (ds, "ds"); (); } catch (xception ex) { throw new Exception(e); } return ds; } } } private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[]
cmdParms) { if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null) { foreach (MySqlParameter parameter in cmdParms) { if ((ion == utput || ion == ) && ( == null)) { = ; } (parameter); } } } #endregion
}View Code注意,Mysql需要引⼊③ SQLite帮助类,全部代码如下: /// /// Copyright (C) 2011 Maticsoft
/// 数据访问基础类(基于SQLite) /// 可以⽤户可以修改满⾜⾃⼰项⽬的需要。 /// public abstract class DbHelperSQLite { //数据库连接字符串(来配置),可以动态更改connectionString⽀持多数据库.
public static string connectionString = tionString; public DbHelperSQLite() { } #region 公⽤⽅法
public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } public static bool Exists(string strSql, params SQLiteParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } }
#endregion #region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection)) { try { (); int rows = eNonQuery(); return rows; } catch (Exception E) { (); throw new Exception(e); } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static void ExecuteSqlTran(ArrayList SQLStringList) { using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { (); SQLiteCommand cmd = new SQLiteCommand(); tion = conn; SQLiteTransaction tx = ransaction(); ction = tx; try { for (int n = 0; n < ; n++) { string strsql = SQLStringList[n].ToString(); if (().Length > 1) { dText = strsql; eNonQuery(); } } (); } catch (Exception E) { ck(); throw new Exception(e); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString, string content) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { SQLiteCommand cmd = new SQLiteCommand(SQLString, connection); SQLiteParameter myParameter = new SQLiteParameter("@content", ); = content; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (Exception E) { throw new Exception(e); } finally { e(); (); } } } /// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { SQLiteCommand cmd = new SQLiteCommand(strSQL, connection); SQLiteParameter myParameter = new SQLiteParameter("@fs", ); = fs; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (Exception E) { throw new Exception(e); } finally { e(); (); } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection)) { try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (Exception e) { (); throw new Exception(e); } } } } /// /// 执⾏查询语句,返回SQLiteDataReader /// /// 查询语句 /// SQLiteDataReader public static SQLiteDataReader ExecuteReader(string strSQL) { SQLiteConnection connection = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(strSQL, connection); try { (); SQLiteDataReader myReader = eReader(); return myReader; } catch (Exception e) { throw new Exception(e); } } /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { DataSet ds = new DataSet(); try { (); SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection); (ds, "ds"); } catch (Exception ex) { throw new Exception(e); } return ds; } } #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = eNonQuery(); (); return rows; } catch (Exception E) { throw new Exception(e); } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SQLiteParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) { using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { (); using (SQLiteTransaction trans = ransaction()) { SQLiteCommand cmd = new SQLiteCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); SQLiteParameter[] cmdParms = (SQLiteParameter[]); PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); (); (); } } catch { ck(); throw; } } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = eScalar(); (); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (Exception e) { throw new Exception(e); } } } } /// /// 执⾏查询语句,返回SQLiteDataReader /// /// 查询语句 /// SQLiteDataReader public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms) { SQLiteConnection connection = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); SQLiteDataReader myReader = eReader(); (); return myReader; } catch (Exception e) { throw new Exception(e); } } /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { SQLiteCommand cmd = new SQLiteCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd)) { DataSet ds = new DataSet(); try { (ds, "ds"); (); } catch (Exception ex) { throw new Exception(e); } return ds; } } } private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText,
SQLiteParameter[] cmdParms) { if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null) { foreach (SQLiteParameter parm in cmdParms) (parm); } } #endregion
}View Code④Oracle帮助类,全部代码如下:/// /// Copyright (C) Maticsoft /// 数据访问基础类(基于Oracle) /// 可以⽤户可以修改满⾜⾃⼰项⽬的需要。 /// public abstract class DbHelperOra { //数据库连接字符串(来配置),可以动态更改connectionString⽀持多数据库.
public static string connectionString = tionString;
public DbHelperOra() {
} #region 公⽤⽅法
public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } public static bool Exists(string strSql, params OracleParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } }
#endregion
#region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) {
using (OracleCommand cmd = new OracleCommand(SQLString,connection)) { try {
(); int rows=eNonQuery(); return rows; } catch(Exception E) {
(); throw new Exception(e); } }
} }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static void ExecuteSqlTran(ArrayList SQLStringList) { using (OracleConnection conn = new OracleConnection(connectionString)) { (); OracleCommand cmd = new OracleCommand(); tion=conn;
OracleTransaction tx=ransaction();
ction=tx;
try {
for(int n=0;n<;n++) { string strsql=SQLStringList[n].ToString(); if (().Length>1) { dText=strsql; eNonQuery(); } }
();
} catch(Exception E) {
ck(); throw new Exception(e); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString,string content) {
using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand cmd = new OracleCommand(SQLString,connection); Parameter myParameter = new Parameter("@content",
ar); = content ; (myParameter); try { (); int rows=eNonQuery(); return rows; } catch(Exception E) {
throw new Exception(e); } finally { e(); (); }
} }
/// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL,byte[] fs) {
using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand cmd = new OracleCommand(strSQL,connection); Parameter myParameter = new Parameter("@fs",
w); = fs ; (myParameter); try { (); int rows=eNonQuery(); return rows; } catch(Exception E) {
throw new Exception(e); } finally { e(); (); }
} }
/// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { using(OracleCommand cmd = new OracleCommand(SQLString,connection)) { try { (); object obj = eScalar(); if(((obj,null))||((obj,))) {
return null; } else { return obj; }
} catch(Exception e) {
(); throw new Exception(e); }
} } } /// /// 执⾏查询语句,返回OracleDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// OracleDataReader public static OracleDataReader ExecuteReader(string strSQL) { OracleConnection connection = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand(strSQL,connection);
try { (); OracleDataReader myReader = eReader(onnection); return myReader; } catch(Exception e) {
throw new Exception(e); }
}
/// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet ds = new DataSet(); try { (); OracleDataAdapter command = new OracleDataAdapter(SQLString,connection);
(ds,"ds"); } catch(Exception ex) {
throw new Exception(e); }
return ds; }
} #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString,params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) {
using (OracleCommand cmd = new OracleCommand()) { try {
PrepareCommand(cmd, connection, null,SQLString, cmdParms); int rows=eNonQuery(); (); return rows; } catch(Exception E) {
throw new Exception(e); } }
} }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的OracleParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) {
using (OracleConnection conn = new OracleConnection(connectionString)) { (); using (OracleTransaction trans = ransaction())
{ OracleCommand cmd = new OracleCommand(); try
{ //循环 foreach (DictionaryEntry myDE in SQLStringList) {
string cmdText=ng(); OracleParameter[] cmdParms=(OracleParameter[]); PrepareCommand(cmd,conn,trans,cmdText, cmdParms); int val = eNonQuery(); (); (); }
} catch
{ ck(); throw; } }
} }
/// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString,params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand cmd = new OracleCommand()) { try { PrepareCommand(cmd, connection, null,SQLString, cmdParms); object obj = eScalar(); (); if(((obj,null))||((obj,))) {
return null; } else { return obj; }
} catch(Exception e) {
throw new Exception(e); }
} } }
/// /// 执⾏查询语句,返回OracleDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// OracleDataReader public static OracleDataReader ExecuteReader(string SQLString,params OracleParameter[] cmdParms) {
OracleConnection connection = new OracleConnection(connectionString); OracleCommand cmd = new OracleCommand();
try { PrepareCommand(cmd, connection, null,SQLString, cmdParms); OracleDataReader myReader = eReader(onnection); (); return myReader; } catch(Exception e) {
throw new Exception(e); }
}
/// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString,params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand cmd = new OracleCommand(); PrepareCommand(cmd, connection, null,SQLString, cmdParms); using( OracleDataAdapter da = new OracleDataAdapter(cmd) ) { DataSet ds = new DataSet();
try {
(ds,"ds"); (); } catch(Exception ex) {
throw new Exception(e); }
return ds; }
}
} private static void PrepareCommand(OracleCommand cmd,OracleConnection conn,OracleTransaction trans, string cmdText,
OracleParameter[] cmdParms)
{ if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null)
{ foreach (OracleParameter parm in cmdParms) (parm); } } #endregion #region 存储过程操作 /// /// 执⾏存储过程 返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 存储过程名 /// 存储过程参数 /// OracleDataReader public static OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters ) { OracleConnection connection = new OracleConnection(connectionString); OracleDataReader returnReader; (); OracleCommand command = BuildQueryCommand( connection,storedProcName, parameters ); dType = Procedure; returnReader = eReader(onnection);
return returnReader;
}
/// /// 执⾏存储过程 /// /// 存储过程名 /// 存储过程参数 /// DataSet结果中的表名 /// DataSet public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName ) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet dataSet = new DataSet(); (); OracleDataAdapter sqlDA = new OracleDataAdapter(); Command = BuildQueryCommand(connection, storedProcName, parameters ); ( dataSet, tableName ); (); return dataSet; } }
/// /// 构建 OracleCommand 对象(⽤来返回⼀个结果集,⽽不是⼀个整数值) /// /// 数据库连接 /// 存储过程名 /// 存储过程参数 /// OracleCommand private static OracleCommand BuildQueryCommand(OracleConnection connection,string storedProcName, IDataParameter[] parameters) {
OracleCommand command = new OracleCommand( storedProcName, connection ); dType = Procedure; foreach (OracleParameter parameter in parameters) { ( parameter ); } return command;
}
/// /// 执⾏存储过程,返回影响的⾏数
/// /// 存储过程名 /// 存储过程参数 /// 影响的⾏数 /// public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected ) { using (OracleConnection connection = new OracleConnection(connectionString)) { int result; (); OracleCommand command = BuildIntCommand(connection,storedProcName, parameters ); rowsAffected = eNonQuery(); result = (int)ters["ReturnValue"].Value; //(); return result; } }
/// /// 创建 OracleCommand 对象实例(⽤来返回⼀个整数值)
/// /// 存储过程名 /// 存储过程参数 /// OracleCommand 对象实例 private static OracleCommand BuildIntCommand(OracleConnection connection,string storedProcName, IDataParameter[] parameters) { OracleCommand command = BuildQueryCommand(connection,storedProcName, parameters ); ( new OracleParameter ( "ReturnValue", 32, 4, Value, false,0,0,,t,null )); return command; } #endregion
}View Code ⑤缓存帮助类,全部代码如下: using g; public class WebDataCache { /// /// 获取当前应⽤程序指定CacheKey的Cache值 /// /// /// public static object GetCache(string CacheKey) { objCache = ; return objCache[CacheKey]; } /// /// 删除当前应⽤程序指定CacheKey的Cache值 /// /// /// public static object RemoveCache(string CacheKey) { objCache = ; return (CacheKey); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// public static void SetCache(string CacheKey, object objObject) { objCache = ; (CacheKey, objObject); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) { objCache = ; (CacheKey, objObject, null, absoluteExpiration, slidingExpiration); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// /// 设置绝对过期时间 public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration) { objCache = ; (CacheKey, objObject, null, absoluteExpiration, ingExpiration); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// /// 设置滑动过期时间 public static void SetCache(string CacheKey, object objObject, TimeSpan slidingExpiration) { objCache = ; (CacheKey, objObject, null, luteExpiration, slidingExpiration); } }View Code使⽤⽅式:例如,登陆的时候可以先获取是否有该缓存: if (he(userName + "固定标识符") == null) { 实体对象= _lesOperS(userName);//根据⽤户名查询出⽤户权限 赋值到实体中 he(userName + "固定标识符", UserRolesOper,
onds((tings["Cache"].ToString())));//设置缓存过期时间,在配置⽂件中配置 }清空缓存操作: Cache(User_Name + "_MenuAction");//清空缓存缓存为空或过期需重新查出使⽤缓存: object obj = he(userName + "固定标识符"); var userRoleOper = (List<实体对象>)obj; //
⑥字符串加密组件/// /// 字符串加密组件 /// public class Encrypt { #region "定义加密字串变量" private SymmetricAlgorithm mCSP; //声明对称算法变量 private const string CIV = "Mi9l/+7Zujhy12se6Yjy111A"; //初始化向量 private const string CKEY = "jkHuIy9D/9i="; //密钥(常量) #endregion /// /// 实例化 /// public Encrypt() { mCSP = new DESCryptoServiceProvider(); //定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象,此类是SymmetricAlgorithm的派⽣类 } /// /// 加密字符串 /// /// 需加密的字符串 /// public string EncryptString(string Value) { ICryptoTransform ct; //定义基本的加密转换运算 MemoryStream ms; //定义内存流 CryptoStream cs; //定义将内存流链接到加密转换的流 byte[] byt; //CreateEncryptor创建(对称数据)加密对象 ct = Encryptor(se64String(CKEY), se64String(CIV)); //⽤指定的密钥和初始化向量创建对称数据加密标准 byt = es(Value); //将Value字符转换为UTF-8编码的字节序列 ms = new MemoryStream(); //创建内存流 cs = new CryptoStream(ms, ct, ); //将内存流链接到加密转换的流 (byt, 0, ); //写⼊内存流 inalBlock(); //将缓冲区中的数据写⼊内存流,并清除缓冲区 (); //释放内存流 return 64String(y()); //将内存流转写⼊字节数组并转换为string字符 } /// /// 解密字符串 /// /// 要解密的字符串 /// string public string DecryptString(string Value) { ICryptoTransform ct; //定义基本的加密转换运算 MemoryStream ms; //定义内存流 CryptoStream cs; //定义将数据流链接到加密转换的流 byte[] byt; ct = Decryptor(se64String(CKEY), se64String(CIV)); //⽤指定的密钥和初始化向量创建对称数据解密标准 byt = se64String(Value); //将Value(Base 64)字符转换成字节数组 ms = new MemoryStream(); cs = new CryptoStream(ms, ct, ); (byt, 0, ); inalBlock(); (); return ing(y()); //将字节数组中的所有字符解码为⼀个字符串 } /// /// ⽣成随机数 /// /// /// public string GetMixPwd(int num)//⽣成混合随机数 { string a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < num; i++) { (a[new Random(d().GetHashCode()).Next(0, - 1)]); } return ng(); } }View Code⑦ 输⼊汉字获取拼⾳或⾸字母⼤写 新建类 ConvertHzToPy,全部代码如下:/// /// Summary description for ConvertHzToPz_Gb2312/// public class ConvertHzToPy{ public ConvertHzToPy() { // // TODO: Add constructor logic here // } private static int[] pyvalue = new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,
-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,
-19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,
-19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,
-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526, -18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,
-18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,
-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,
-17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,
-16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,
-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,
-15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,
-15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,
-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,
-14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,
-14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,
-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,
-14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,
-13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,
-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,
-12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,
-12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,
-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,
-11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,
-10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254}; private static string[] pystr = new string[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",
"bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",
"cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",
"cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",
"dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",
"ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",
"hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",
"jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",
"keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",
"leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",
"ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",
"na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",
"nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",
"pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",
"ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",
"sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",
"shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",
"tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",
"wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",
"xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",
"yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",
"zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",
"zi","zong","zou","zu","zuan","zui","zun","zuo"}; /// /// 获取拼⾳ /// /// 汉字 /// public static string Convert(string str) { byte[] array = new byte[2]; string returnstr = ""; int chrasc = 0; int i1 = 0; int i2 = 0; char[] nowchar = Array(); for (int j = 0; j < ; j++) { byte[] btchk = es(nowchar[j].ToString()); if ( == 1) { returnstr += nowchar[j].ToString(); continue; } array = btchk; //array = es(nowchar[j].ToString()); i1 = (short)(array[0]); i2 = (short)(array[1]); chrasc = i1 * 256 + i2 - 65536; if (chrasc > 0 && chrasc < 160) { returnstr += nowchar[j]; } else { for (int i = ( - 1); i >= 0; i--) { if (pyvalue[i] <= chrasc) { returnstr += pystr[i]; break; } } } } return returnstr; } /// /// 获取拼⾳⾸字母 /// /// 汉字 /// public static string GetShortPY(string str) { string tempStr = ""; foreach (char c in str) { if ((int)c >= 33 && (int)c <= 126) { tempStr += ng(); //字母和符号原样保留
} else { if ((int)c != 32)//如果不是空格,那么转换 { tempStr += GetPYChar(ng()); //累加拼⾳声母
} else//如果是空格那么在字符串中间+上⼀个空字符 { tempStr += " "; } } } return tempStr; } private static string GetPYChar(string c) { byte[] array = new byte[2]; array = es(c); int i = (short)(array[0] - '0') * 256 + ((short)(array[1] - '0')); if (i < 0xB0A1) return ""; if (i < 0xB0C5) return "a"; if (i < 0xB2C1) return "b"; if (i < 0xB4EE) return "c"; if (i < 0xB6EA) return "d"; if (i < 0xB7A2) return "e"; if (i < 0xB8C1) return "f"; if (i < 0xB9FE) return "g"; if (i < 0xBBF7) return "h"; if (i < 0xBFA6) return "j"; if (i < 0xC0AC) return "k"; if (i < 0xC2E8) return "l"; if (i < 0xC4C3) return "m"; if (i < 0xC5B6) return "n"; if (i < 0xC5BE) return "o"; if (i < 0xC6DA) return "p"; if (i < 0xC8BB) return "q"; if (i < 0xC8F6) return "r"; if (i < 0xCBFA) return "s";
2023年6月21日发(作者:)
C#各种帮助类⼤全前⾔此篇专门记录⼀些常见DB帮助类及其他帮助类,以便使⽤时不⽤重复造轮⼦。DBHelper帮助类①⾸当其冲的就是Sql Server帮助类,创建名为DbHelperSQL 的类 ,全部代码如下:/// /// 数据访问抽象基础类 /// Copyright (C) Maticsoft
/// public abstract class DbHelperSQL { //数据库连接字符串(来配置),多数据库可使⽤DbHelperSQLP来实现. public static string connectionString = tionString;
public DbHelperSQL() {
} #region 公⽤⽅法 /// /// 判断是否存在某表的某个字段 /// /// 表名称 /// 列名称 /// 是否存在 public static bool ColumnExists(string tableName, string columnName) { string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'"; object res = GetSingle(sql); if (res == null) { return false; } return 32(res) > 0; } public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); //也可能=0 } if (cmdresult == 0) { return false; } else { return true; } } /// /// 表是否存在 /// /// /// public static bool TabExists(string TableName) { string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1"; //string strsql = "SELECT count(*) FROM s WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')"; object obj = GetSingle(strsql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } public static bool Exists(string strSql, params SqlParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } #endregion #region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); int rows = eNonQuery(); return rows; } catch (eption e) { (); throw e; } } } } public static int ExecuteSqlByTime(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); dTimeout = Times; int rows = eNonQuery(); return rows; } catch (eption e) { (); throw e; } } } }
/// /// 执⾏Sql和Oracle滴混合事务 /// /// SQL命令⾏列表 /// Oracle命令⾏列表 /// 执⾏结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执⾏成功 public static int ExecuteSqlTran(List list, List oracleCmdSqlList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); SqlCommand cmd = new SqlCommand(); tion = conn; SqlTransaction tx = ransaction(); ction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = dText; SqlParameter[] cmdParms = (SqlParameter[])ters; PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (NextType == tationEvent) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("违背要求"+dText+"必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (isHave) { //引发事件 citationEvent(); } } if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("SQL:违背要求" + dText + "必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须⼤于0"); //return 0; } if (NextType == HaveContine && isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须等于0"); //return 0; } continue; } int val = eNonQuery(); if (NextType == EffectRows && val == 0) { ck(); throw new Exception("SQL:违背要求" + dText + "必须有影响⾏"); //return 0; } (); } string oraConnectionString = nectionString("ConnectionStringPPC"); bool res = eSqlTran(oraConnectionString, oracleCmdSqlList); if (!res) { ck(); throw new Exception("Oracle执⾏失败"); // return -1; } (); return 1; } catch (eption e) { ck(); throw e; } catch (Exception e) { ck(); throw e; } } }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static int ExecuteSqlTran(List SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); SqlCommand cmd = new SqlCommand(); tion = conn; SqlTransaction tx = ransaction(); ction = tx; try { int count = 0; for (int n = 0; n < ; n++) { string strsql = SQLStringList[n]; if (().Length > 1) { dText = strsql; count += eNonQuery(); } } (); return count; } catch { ck(); return 0; } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString, string content) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, connection); ameter myParameter = new ameter("@content", ); = content; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (eption e) { throw e; } finally { e(); (); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static object ExecuteSqlGet(string SQLString, string content) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, connection); ameter myParameter = new ameter("@content", ); = content; (myParameter); try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { throw e; } finally { e(); (); } } } /// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(strSQL, connection); ameter myParameter = new ameter("@fs", ); = fs; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (eption e) { throw e; } finally { e(); (); } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { (); throw e; } } } } public static object GetSingle(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { (); dTimeout = Times; object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { (); throw e; } } } } /// /// 执⾏查询语句,返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// SqlDataReader public static SqlDataReader ExecuteReader(string strSQL) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(strSQL, connection); try { (); SqlDataReader myReader = eReader(onnection); return myReader; } catch (eption e) { throw e; }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); (ds, "ds"); } catch (eption ex) { throw new Exception(e); } return ds; } } public static DataSet Query(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); dTimeout = Times; (ds, "ds"); } catch (eption ex) { throw new Exception(e); } return ds; } } #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = eNonQuery(); (); return rows; } catch (eption e) { throw e; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); SqlParameter[] cmdParms = (SqlParameter[]); PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static int ExecuteSqlTran( cmdList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { int count = 0; //循环 foreach (CommandInfo myDE in cmdList) { string cmdText = dText; SqlParameter[] cmdParms = (SqlParameter[])ters; PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); return 0; } if (NextType == HaveContine && isHave) { ck(); return 0; } continue; } int val = eNonQuery(); count += val; if (NextType == EffectRows && val == 0) { ck(); return 0; } (); } (); return count; } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static void ExecuteSqlTranWithIndentity( SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { int indentity = 0; //循环 foreach (CommandInfo myDE in SQLStringList) { string cmdText = dText; SqlParameter[] cmdParms = (SqlParameter[])ters; foreach (SqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (SqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { (); using (SqlTransaction trans = ransaction()) { SqlCommand cmd = new SqlCommand(); try { int indentity = 0; //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); SqlParameter[] cmdParms = (SqlParameter[]); foreach (SqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (SqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = eScalar(); (); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (eption e) { throw e; } } } } /// /// 执⾏查询语句,返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// SqlDataReader public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); SqlDataReader myReader = eReader(onnection); (); return myReader; } catch (eption e) { throw e; } // finally // { // e(); // (); // }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString, params SqlParameter[] cmdParms) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { (ds, "ds"); (); } catch (eption ex) { throw new Exception(e); } return ds; } } } private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms) { if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { if ((ion == utput || ion == ) && ( == null)) { = ; } (parameter); } } } #endregion #region 存储过程操作 /// /// 执⾏存储过程,返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 存储过程名 /// 存储过程参数 /// SqlDataReader public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters) { SqlConnection connection = new SqlConnection(connectionString); SqlDataReader returnReader; (); SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); dType = Procedure; returnReader = eReader(onnection); return returnReader;
} /// /// 执⾏存储过程 /// /// 存储过程名 /// 存储过程参数 /// DataSet结果中的表名 /// DataSet public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); (); SqlDataAdapter sqlDA = new SqlDataAdapter(); Command = BuildQueryCommand(connection, storedProcName, parameters); (dataSet, tableName); (); return dataSet; } } public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); (); SqlDataAdapter sqlDA = new SqlDataAdapter(); Command = BuildQueryCommand(connection, storedProcName, parameters); dTimeout = Times; (dataSet, tableName); (); return dataSet; } } /// /// 构建 SqlCommand 对象(⽤来返回⼀个结果集,⽽不是⼀个整数值) /// /// 数据库连接 /// 存储过程名 /// 存储过程参数 /// SqlCommand private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(storedProcName, connection); dType = Procedure; foreach (SqlParameter parameter in parameters) { if (parameter != null) { // 检查未分配值的输出参数,将其分配以. if ((ion == utput || ion == ) && ( == null)) { = ; } (parameter); } } return command; } /// /// 执⾏存储过程,返回影响的⾏数
/// /// 存储过程名 /// 存储过程参数 /// 影响的⾏数 /// public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected) { using (SqlConnection connection = new SqlConnection(connectionString)) { int result; (); SqlCommand command = BuildIntCommand(connection, storedProcName, parameters); rowsAffected = eNonQuery(); result = (int)ters["ReturnValue"].Value; //(); return result; } } /// /// 创建 SqlCommand 对象实例(⽤来返回⼀个整数值)
/// /// 存储过程名 /// 存储过程参数 /// SqlCommand 对象实例 private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); (new SqlParameter("ReturnValue", , 4, Value, false, 0, 0, , t, null)); return command; } #endregion }View Code注意:根据提⽰引⽤相关命名空间即可。上述代码中⽤到了⼀个PubConstant类 ⽤来获取连接字符串,全部代码如下: public class PubConstant {
/// /// 获取连接字符串 /// public static string ConnectionString {
get
{ string _connectionString = tings["ConnectionString"];
string ConStringEncrypt = tings["ConStringEncrypt"]; if (ConStringEncrypt == "true") { _connectionString = t(_connectionString); } return _connectionString;
} } /// /// 得到⾥配置项的数据库连接字符串。 /// /// /// public static string GetConnectionString(string configName) { string connectionString = tings[configName]; string ConStringEncrypt = tings["ConStringEncrypt"]; if (ConStringEncrypt == "true") { connectionString = t(connectionString); } return connectionString; } }View CodePubConstant类中对连接字符串进⾏了DES加密解密操作,DES加密/解密帮助类全部代码如下:/// /// DES加密/解密类。 /// public class DESEncrypt { public DESEncrypt() {
} #region ========加密========
/// /// 加密 /// /// /// public static string Encrypt(string Text)
{ return Encrypt(Text, "MATICSOFT"); } ///
/// 加密数据
///
///
///
///
public static string Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=es(Text);
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
Stream ms=new Stream();
CryptoStream cs=new CryptoStream(ms,Encryptor(),);
(inputByteArray,0,);
inalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in y())
{
Format("{0:X2}",b);
}
return ng();
}
#endregion
#region ========解密========
/// /// 解密 /// /// /// public static string Decrypt(string Text)
{ return Decrypt(Text, "MATICSOFT"); } ///
/// 解密数据
///
///
///
///
public static string Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len=/2;
byte[] inputByteArray = new byte[len];
int x,i;
for(x=0;x {
i = 32(ing(x * 2, 2), 16);
inputByteArray[x]=(byte)i;
}
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
= es(sswordForStoringInConfigFile(sKey,
"md5").Substring(0, 8));
Stream ms=new Stream();
CryptoStream cs=new CryptoStream(ms,Decryptor(),);
(inputByteArray,0,); inalBlock();
return ing(y());
}
#endregion
}View Code
强烈建议使⽤ESC加密全部代码如下: public class DESCrypto { private static string key = "KABCMS2017"; /// /// 加密(UTF-8) /// /// 待加密字符串 /// 密钥(长度不能少于8位字符) /// public static string Encode(string str, string secKey = "") { if (OrEmpty(secKey)) secKey = key; else if ( < 8) return ""; DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); = es(ing(0, 8)); = es(ing(0, 8)); byte[] bytes = oding("UTF-8").GetBytes(str); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, Encryptor(), ); (bytes, 0, ); inalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in y()) { Format("{0:X2}", num); } (); return ng(); } /// /// 解密(UTF-8) /// /// 待解密字符串 /// 密钥(长度不能少于8位字符) /// public static string Decode(string str, string secKey = "") { if (OrEmpty(secKey)) secKey = key; else if ( < 8) return ""; DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); = es(ing(0, 8)); = es(ing(0, 8)); byte[] buffer = new byte[ / 2]; for (int i = 0; i < ( / 2); i++) { int num2 = 32(ing(i * 2, 2), 0x10); buffer[i] = (byte)num2; } MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, Decryptor(), ); (buffer, 0, ); inalBlock(); (); return oding("UTF-8").GetString(y()); } }View Code
然后再配置⽂件中新建⼏个字符串链接,分别连接不同的库,代码如下: -- MySql -- SqlServer -- SqlLole使⽤ /// /// 根据账号获取信息 /// /// /// public ServiceProvider GetNameByQR(string name) { try { string sql = "select 字段1, 字段2, 字段3 from viw_promotion where name='" + name + "'"; DataSet ds = (sql);//此处调⽤MysqlDBHelper帮助类 DataTable dt = [0]; ServiceProvider ProviderQR = new ServiceProvider(); foreach (DataRow item in ) { if (!OrEmpty(item["字段1"].ToString())) { tName = item["字段1"].ToString(); } if (!OrEmpty(item["字段2"].ToString())) { tyURL = item["字段2"].ToString(); } if (!OrEmpty(item["字段3"].ToString())) { tyQRCode = item["字段3"].ToString(); } } return ProviderQR; //返回实体对象 } catch (Exception) { return null; } }② DbHelperMySQL帮助类,全部代码如下:
/// /// 数据访问抽象基础类 /// Copyright (C) Maticsoft /// public abstract class DbHelperMySQL { //数据库连接字符串(来配置),可以动态更改connectionString⽀持多数据库.
// public static string connectionString = tionString;Mysql_ConnectionString,ConnectionString1 public static string connectionString = tings["ConnectionString1"]; public DbHelperMySQL() {
} #region 公⽤⽅法 /// /// 得到最⼤值 /// /// /// /// public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } /// /// 是否存在 /// /// /// public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } }
/// /// 是否存在(基于MySqlParameter) /// /// /// /// public static bool Exists(string strSql, params MySqlParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } #endregion #region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); int rows = eNonQuery(); return rows; } catch (xception e) { (); throw e; } } } } public static int ExecuteSqlByTime(string SQLString, int Times) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); dTimeout = Times; int rows = eNonQuery(); return rows; } catch (xception e) { (); throw e; } } } }
/// /// 执⾏MySql和Oracle滴混合事务 /// /// SQL命令⾏列表 /// Oracle命令⾏列表 /// 执⾏结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执⾏成功 public static int ExecuteSqlTran(List list, List oracleCmdSqlList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); MySqlCommand cmd = new MySqlCommand(); tion = conn; MySqlTransaction tx = ransaction(); ction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = dText; MySqlParameter[] cmdParms = (MySqlParameter[])ters; PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (NextType == tationEvent) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("违背要求"+dText+"必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (isHave) { //引发事件 citationEvent(); } } if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); throw new Exception("SQL:违背要求" + dText + "必须符合select count(..的格式"); //return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须⼤于0"); //return 0; } if (NextType == HaveContine && isHave) { ck(); throw new Exception("SQL:违背要求" + dText + "返回值必须等于0"); //return 0; } continue; } int val = eNonQuery(); if (NextType == EffectRows && val == 0) { ck(); throw new Exception("SQL:违背要求" + dText + "必须有影响⾏"); //return 0; } (); } string oraConnectionString = nectionString("ConnectionStringPPC"); bool res = eSqlTran(oraConnectionString, oracleCmdSqlList); if (!res) { ck(); throw new Exception("执⾏失败"); // return -1; } (); return 1; } catch (xception e) { ck(); throw e; } catch (Exception e) { ck(); throw e; } } }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static int ExecuteSqlTran(List SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); MySqlCommand cmd = new MySqlCommand(); tion = conn; MySqlTransaction tx = ransaction(); ction = tx; try { int count = 0; for (int n = 0; n < ; n++) { string strsql = SQLStringList[n]; if (().Length > 1) { dText = strsql; count += eNonQuery(); } } (); return count; } catch { ck(); return 0; } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString, string content) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(SQLString, connection); arameter myParameter = new arameter("@content", ); = content; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (xception e) { throw e; } finally { e(); (); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static object ExecuteSqlGet(string SQLString, string content) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(SQLString, connection); arameter myParameter = new arameter("@content", ); = content; (myParameter); try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { throw e; } finally { e(); (); } } } /// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(strSQL, connection); arameter myParameter = new arameter("@fs", ); = fs; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (xception e) { throw e; } finally { e(); (); } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { (); throw e; } } } } public static object GetSingle(string SQLString, int Times) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { (); dTimeout = Times; object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { (); throw e; } } } } /// /// 执⾏查询语句,返回MySqlDataReader ( 注意:调⽤该⽅法后,⼀定要对MySqlDataReader进⾏Close ) /// /// 查询语句 /// MySqlDataReader public static MySqlDataReader ExecuteReader(string strSQL) { MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand cmd = new MySqlCommand(strSQL, connection); try { (); MySqlDataReader myReader = eReader(onnection); return myReader; } catch (xception e) { throw e; }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); (ds, "ds"); } catch (xception ex) { throw new Exception(e); } return ds; } } public static DataSet Query(string SQLString, int Times) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { DataSet ds = new DataSet(); try { (); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); dTimeout = Times; (ds, "ds"); } catch (xception ex) { throw new Exception(e); } return ds; } } #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = eNonQuery(); (); return rows; } catch (xception e) { throw e; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); MySqlParameter[] cmdParms = (MySqlParameter[]); PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static int ExecuteSqlTran( cmdList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { int count = 0; //循环 foreach (CommandInfo myDE in cmdList) { string cmdText = dText; MySqlParameter[] cmdParms = (MySqlParameter[])ters; PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
if (NextType == veContine || NextType == HaveContine) { if (r().IndexOf("count(") == -1) { ck(); return 0; } object obj = eScalar(); bool isHave = false; if (obj == null && obj == ) { isHave = false; } isHave = 32(obj) > 0; if (NextType == veContine && !isHave) { ck(); return 0; } if (NextType == HaveContine && isHave) { ck(); return 0; } continue; } int val = eNonQuery(); count += val; if (NextType == EffectRows && val == 0) { ck(); return 0; } (); } (); return count; } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static void ExecuteSqlTranWithIndentity( SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { int indentity = 0; //循环 foreach (CommandInfo myDE in SQLStringList) { string cmdText = dText; MySqlParameter[] cmdParms = (MySqlParameter[])ters; foreach (MySqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (MySqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[]) public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { (); using (MySqlTransaction trans = ransaction()) { MySqlCommand cmd = new MySqlCommand(); try { int indentity = 0; //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); MySqlParameter[] cmdParms = (MySqlParameter[]); foreach (MySqlParameter q in cmdParms) { if (ion == utput) { = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); foreach (MySqlParameter q in cmdParms) { if (ion == ) { indentity = 32(); } } (); } (); } catch { ck(); throw; } } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = eScalar(); (); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (xception e) { throw e; } } } } /// /// 执⾏查询语句,返回MySqlDataReader ( 注意:调⽤该⽅法后,⼀定要对MySqlDataReader进⾏Close ) /// /// 查询语句 /// MySqlDataReader public static MySqlDataReader ExecuteReader(string SQLString, params MySqlParameter[] cmdParms) { MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand cmd = new MySqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); MySqlDataReader myReader = eReader(onnection); (); return myReader; } catch (xception e) { throw e; } // finally // { // e(); // (); // }
} /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (MySqlDataAdapter da = new MySqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { (ds, "ds"); (); } catch (xception ex) { throw new Exception(e); } return ds; } } } private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[]
cmdParms) { if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null) { foreach (MySqlParameter parameter in cmdParms) { if ((ion == utput || ion == ) && ( == null)) { = ; } (parameter); } } } #endregion
}View Code注意,Mysql需要引⼊③ SQLite帮助类,全部代码如下: /// /// Copyright (C) 2011 Maticsoft
/// 数据访问基础类(基于SQLite) /// 可以⽤户可以修改满⾜⾃⼰项⽬的需要。 /// public abstract class DbHelperSQLite { //数据库连接字符串(来配置),可以动态更改connectionString⽀持多数据库.
public static string connectionString = tionString; public DbHelperSQLite() { } #region 公⽤⽅法
public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } public static bool Exists(string strSql, params SQLiteParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } }
#endregion #region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection)) { try { (); int rows = eNonQuery(); return rows; } catch (Exception E) { (); throw new Exception(e); } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static void ExecuteSqlTran(ArrayList SQLStringList) { using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { (); SQLiteCommand cmd = new SQLiteCommand(); tion = conn; SQLiteTransaction tx = ransaction(); ction = tx; try { for (int n = 0; n < ; n++) { string strsql = SQLStringList[n].ToString(); if (().Length > 1) { dText = strsql; eNonQuery(); } } (); } catch (Exception E) { ck(); throw new Exception(e); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString, string content) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { SQLiteCommand cmd = new SQLiteCommand(SQLString, connection); SQLiteParameter myParameter = new SQLiteParameter("@content", ); = content; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (Exception E) { throw new Exception(e); } finally { e(); (); } } } /// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { SQLiteCommand cmd = new SQLiteCommand(strSQL, connection); SQLiteParameter myParameter = new SQLiteParameter("@fs", ); = fs; (myParameter); try { (); int rows = eNonQuery(); return rows; } catch (Exception E) { throw new Exception(e); } finally { e(); (); } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection)) { try { (); object obj = eScalar(); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (Exception e) { (); throw new Exception(e); } } } } /// /// 执⾏查询语句,返回SQLiteDataReader /// /// 查询语句 /// SQLiteDataReader public static SQLiteDataReader ExecuteReader(string strSQL) { SQLiteConnection connection = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(strSQL, connection); try { (); SQLiteDataReader myReader = eReader(); return myReader; } catch (Exception e) { throw new Exception(e); } } /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { DataSet ds = new DataSet(); try { (); SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection); (ds, "ds"); } catch (Exception ex) { throw new Exception(e); } return ds; } } #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = eNonQuery(); (); return rows; } catch (Exception E) { throw new Exception(e); } } } } /// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的SQLiteParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) { using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { (); using (SQLiteTransaction trans = ransaction()) { SQLiteCommand cmd = new SQLiteCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = ng(); SQLiteParameter[] cmdParms = (SQLiteParameter[]); PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = eNonQuery(); (); (); } } catch { ck(); throw; } } } } /// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = eScalar(); (); if (((obj, null)) || ((obj, ))) { return null; } else { return obj; } } catch (Exception e) { throw new Exception(e); } } } } /// /// 执⾏查询语句,返回SQLiteDataReader /// /// 查询语句 /// SQLiteDataReader public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms) { SQLiteConnection connection = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); SQLiteDataReader myReader = eReader(); (); return myReader; } catch (Exception e) { throw new Exception(e); } } /// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { SQLiteCommand cmd = new SQLiteCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd)) { DataSet ds = new DataSet(); try { (ds, "ds"); (); } catch (Exception ex) { throw new Exception(e); } return ds; } } } private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText,
SQLiteParameter[] cmdParms) { if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null) { foreach (SQLiteParameter parm in cmdParms) (parm); } } #endregion
}View Code④Oracle帮助类,全部代码如下:/// /// Copyright (C) Maticsoft /// 数据访问基础类(基于Oracle) /// 可以⽤户可以修改满⾜⾃⼰项⽬的需要。 /// public abstract class DbHelperOra { //数据库连接字符串(来配置),可以动态更改connectionString⽀持多数据库.
public static string connectionString = tionString;
public DbHelperOra() {
} #region 公⽤⽅法
public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return (ng()); } } public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } } public static bool Exists(string strSql, params OracleParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if (((obj, null)) || ((obj, ))) { cmdresult = 0; } else { cmdresult = (ng()); } if (cmdresult == 0) { return false; } else { return true; } }
#endregion
#region 执⾏简单SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) {
using (OracleCommand cmd = new OracleCommand(SQLString,connection)) { try {
(); int rows=eNonQuery(); return rows; } catch(Exception E) {
(); throw new Exception(e); } }
} }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// 多条SQL语句
public static void ExecuteSqlTran(ArrayList SQLStringList) { using (OracleConnection conn = new OracleConnection(connectionString)) { (); OracleCommand cmd = new OracleCommand(); tion=conn;
OracleTransaction tx=ransaction();
ction=tx;
try {
for(int n=0;n<;n++) { string strsql=SQLStringList[n].ToString(); if (().Length>1) { dText=strsql; eNonQuery(); } }
();
} catch(Exception E) {
ck(); throw new Exception(e); } } } /// /// 执⾏带⼀个存储过程参数的的SQL语句。 /// /// SQL语句 /// 参数内容,⽐如⼀个字段是格式复杂的⽂章,有特殊符号,可以通过这个⽅式添加 /// 影响的记录数 public static int ExecuteSql(string SQLString,string content) {
using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand cmd = new OracleCommand(SQLString,connection); Parameter myParameter = new Parameter("@content",
ar); = content ; (myParameter); try { (); int rows=eNonQuery(); return rows; } catch(Exception E) {
throw new Exception(e); } finally { e(); (); }
} }
/// /// 向数据库⾥插⼊图像格式的字段(和上⾯情况类似的另⼀种实例) /// /// SQL语句 /// 图像字节,数据库的字段类型为image的情况 /// 影响的记录数 public static int ExecuteSqlInsertImg(string strSQL,byte[] fs) {
using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand cmd = new OracleCommand(strSQL,connection); Parameter myParameter = new Parameter("@fs",
w); = fs ; (myParameter); try { (); int rows=eNonQuery(); return rows; } catch(Exception E) {
throw new Exception(e); } finally { e(); (); }
} }
/// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { using(OracleCommand cmd = new OracleCommand(SQLString,connection)) { try { (); object obj = eScalar(); if(((obj,null))||((obj,))) {
return null; } else { return obj; }
} catch(Exception e) {
(); throw new Exception(e); }
} } } /// /// 执⾏查询语句,返回OracleDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// OracleDataReader public static OracleDataReader ExecuteReader(string strSQL) { OracleConnection connection = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand(strSQL,connection);
try { (); OracleDataReader myReader = eReader(onnection); return myReader; } catch(Exception e) {
throw new Exception(e); }
}
/// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet ds = new DataSet(); try { (); OracleDataAdapter command = new OracleDataAdapter(SQLString,connection);
(ds,"ds"); } catch(Exception ex) {
throw new Exception(e); }
return ds; }
} #endregion #region 执⾏带参数的SQL语句 /// /// 执⾏SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public static int ExecuteSql(string SQLString,params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) {
using (OracleCommand cmd = new OracleCommand()) { try {
PrepareCommand(cmd, connection, null,SQLString, cmdParms); int rows=eNonQuery(); (); return rows; } catch(Exception E) {
throw new Exception(e); } }
} }
/// /// 执⾏多条SQL语句,实现数据库事务。 /// /// SQL语句的哈希表(key为sql语句,value是该语句的OracleParameter[]) public static void ExecuteSqlTran(Hashtable SQLStringList) {
using (OracleConnection conn = new OracleConnection(connectionString)) { (); using (OracleTransaction trans = ransaction())
{ OracleCommand cmd = new OracleCommand(); try
{ //循环 foreach (DictionaryEntry myDE in SQLStringList) {
string cmdText=ng(); OracleParameter[] cmdParms=(OracleParameter[]); PrepareCommand(cmd,conn,trans,cmdText, cmdParms); int val = eNonQuery(); (); (); }
} catch
{ ck(); throw; } }
} }
/// /// 执⾏⼀条计算查询结果语句,返回查询结果(object)。 /// /// 计算查询结果语句 /// 查询结果(object) public static object GetSingle(string SQLString,params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand cmd = new OracleCommand()) { try { PrepareCommand(cmd, connection, null,SQLString, cmdParms); object obj = eScalar(); (); if(((obj,null))||((obj,))) {
return null; } else { return obj; }
} catch(Exception e) {
throw new Exception(e); }
} } }
/// /// 执⾏查询语句,返回OracleDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 查询语句 /// OracleDataReader public static OracleDataReader ExecuteReader(string SQLString,params OracleParameter[] cmdParms) {
OracleConnection connection = new OracleConnection(connectionString); OracleCommand cmd = new OracleCommand();
try { PrepareCommand(cmd, connection, null,SQLString, cmdParms); OracleDataReader myReader = eReader(onnection); (); return myReader; } catch(Exception e) {
throw new Exception(e); }
}
/// /// 执⾏查询语句,返回DataSet /// /// 查询语句 /// DataSet public static DataSet Query(string SQLString,params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand cmd = new OracleCommand(); PrepareCommand(cmd, connection, null,SQLString, cmdParms); using( OracleDataAdapter da = new OracleDataAdapter(cmd) ) { DataSet ds = new DataSet();
try {
(ds,"ds"); (); } catch(Exception ex) {
throw new Exception(e); }
return ds; }
}
} private static void PrepareCommand(OracleCommand cmd,OracleConnection conn,OracleTransaction trans, string cmdText,
OracleParameter[] cmdParms)
{ if ( != ) (); tion = conn; dText = cmdText; if (trans != null) ction = trans; dType = ;//cmdType; if (cmdParms != null)
{ foreach (OracleParameter parm in cmdParms) (parm); } } #endregion #region 存储过程操作 /// /// 执⾏存储过程 返回SqlDataReader ( 注意:调⽤该⽅法后,⼀定要对SqlDataReader进⾏Close ) /// /// 存储过程名 /// 存储过程参数 /// OracleDataReader public static OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters ) { OracleConnection connection = new OracleConnection(connectionString); OracleDataReader returnReader; (); OracleCommand command = BuildQueryCommand( connection,storedProcName, parameters ); dType = Procedure; returnReader = eReader(onnection);
return returnReader;
}
/// /// 执⾏存储过程 /// /// 存储过程名 /// 存储过程参数 /// DataSet结果中的表名 /// DataSet public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName ) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet dataSet = new DataSet(); (); OracleDataAdapter sqlDA = new OracleDataAdapter(); Command = BuildQueryCommand(connection, storedProcName, parameters ); ( dataSet, tableName ); (); return dataSet; } }
/// /// 构建 OracleCommand 对象(⽤来返回⼀个结果集,⽽不是⼀个整数值) /// /// 数据库连接 /// 存储过程名 /// 存储过程参数 /// OracleCommand private static OracleCommand BuildQueryCommand(OracleConnection connection,string storedProcName, IDataParameter[] parameters) {
OracleCommand command = new OracleCommand( storedProcName, connection ); dType = Procedure; foreach (OracleParameter parameter in parameters) { ( parameter ); } return command;
}
/// /// 执⾏存储过程,返回影响的⾏数
/// /// 存储过程名 /// 存储过程参数 /// 影响的⾏数 /// public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected ) { using (OracleConnection connection = new OracleConnection(connectionString)) { int result; (); OracleCommand command = BuildIntCommand(connection,storedProcName, parameters ); rowsAffected = eNonQuery(); result = (int)ters["ReturnValue"].Value; //(); return result; } }
/// /// 创建 OracleCommand 对象实例(⽤来返回⼀个整数值)
/// /// 存储过程名 /// 存储过程参数 /// OracleCommand 对象实例 private static OracleCommand BuildIntCommand(OracleConnection connection,string storedProcName, IDataParameter[] parameters) { OracleCommand command = BuildQueryCommand(connection,storedProcName, parameters ); ( new OracleParameter ( "ReturnValue", 32, 4, Value, false,0,0,,t,null )); return command; } #endregion
}View Code ⑤缓存帮助类,全部代码如下: using g; public class WebDataCache { /// /// 获取当前应⽤程序指定CacheKey的Cache值 /// /// /// public static object GetCache(string CacheKey) { objCache = ; return objCache[CacheKey]; } /// /// 删除当前应⽤程序指定CacheKey的Cache值 /// /// /// public static object RemoveCache(string CacheKey) { objCache = ; return (CacheKey); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// public static void SetCache(string CacheKey, object objObject) { objCache = ; (CacheKey, objObject); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) { objCache = ; (CacheKey, objObject, null, absoluteExpiration, slidingExpiration); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// /// 设置绝对过期时间 public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration) { objCache = ; (CacheKey, objObject, null, absoluteExpiration, ingExpiration); } /// /// 设置当前应⽤程序指定CacheKey的Cache值 /// /// /// /// 设置滑动过期时间 public static void SetCache(string CacheKey, object objObject, TimeSpan slidingExpiration) { objCache = ; (CacheKey, objObject, null, luteExpiration, slidingExpiration); } }View Code使⽤⽅式:例如,登陆的时候可以先获取是否有该缓存: if (he(userName + "固定标识符") == null) { 实体对象= _lesOperS(userName);//根据⽤户名查询出⽤户权限 赋值到实体中 he(userName + "固定标识符", UserRolesOper,
onds((tings["Cache"].ToString())));//设置缓存过期时间,在配置⽂件中配置 }清空缓存操作: Cache(User_Name + "_MenuAction");//清空缓存缓存为空或过期需重新查出使⽤缓存: object obj = he(userName + "固定标识符"); var userRoleOper = (List<实体对象>)obj; //
⑥字符串加密组件/// /// 字符串加密组件 /// public class Encrypt { #region "定义加密字串变量" private SymmetricAlgorithm mCSP; //声明对称算法变量 private const string CIV = "Mi9l/+7Zujhy12se6Yjy111A"; //初始化向量 private const string CKEY = "jkHuIy9D/9i="; //密钥(常量) #endregion /// /// 实例化 /// public Encrypt() { mCSP = new DESCryptoServiceProvider(); //定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象,此类是SymmetricAlgorithm的派⽣类 } /// /// 加密字符串 /// /// 需加密的字符串 /// public string EncryptString(string Value) { ICryptoTransform ct; //定义基本的加密转换运算 MemoryStream ms; //定义内存流 CryptoStream cs; //定义将内存流链接到加密转换的流 byte[] byt; //CreateEncryptor创建(对称数据)加密对象 ct = Encryptor(se64String(CKEY), se64String(CIV)); //⽤指定的密钥和初始化向量创建对称数据加密标准 byt = es(Value); //将Value字符转换为UTF-8编码的字节序列 ms = new MemoryStream(); //创建内存流 cs = new CryptoStream(ms, ct, ); //将内存流链接到加密转换的流 (byt, 0, ); //写⼊内存流 inalBlock(); //将缓冲区中的数据写⼊内存流,并清除缓冲区 (); //释放内存流 return 64String(y()); //将内存流转写⼊字节数组并转换为string字符 } /// /// 解密字符串 /// /// 要解密的字符串 /// string public string DecryptString(string Value) { ICryptoTransform ct; //定义基本的加密转换运算 MemoryStream ms; //定义内存流 CryptoStream cs; //定义将数据流链接到加密转换的流 byte[] byt; ct = Decryptor(se64String(CKEY), se64String(CIV)); //⽤指定的密钥和初始化向量创建对称数据解密标准 byt = se64String(Value); //将Value(Base 64)字符转换成字节数组 ms = new MemoryStream(); cs = new CryptoStream(ms, ct, ); (byt, 0, ); inalBlock(); (); return ing(y()); //将字节数组中的所有字符解码为⼀个字符串 } /// /// ⽣成随机数 /// /// /// public string GetMixPwd(int num)//⽣成混合随机数 { string a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < num; i++) { (a[new Random(d().GetHashCode()).Next(0, - 1)]); } return ng(); } }View Code⑦ 输⼊汉字获取拼⾳或⾸字母⼤写 新建类 ConvertHzToPy,全部代码如下:/// /// Summary description for ConvertHzToPz_Gb2312/// public class ConvertHzToPy{ public ConvertHzToPy() { // // TODO: Add constructor logic here // } private static int[] pyvalue = new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,-20032,-20026,
-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,-19756,-19751,-19746,-19741,-19739,-19728,
-19725,-19715,-19540,-19531,-19525,-19515,-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,
-19261,-19249,-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,-19003,-18996,
-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,-18731,-18722,-18710,-18697,-18696,-18526, -18518,-18501,-18490,-18478,-18463,-18448,-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,
-18181,-18012,-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,-17733,-17730,
-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,-17468,-17454,-17433,-17427,-17417,-17202,
-17185,-16983,-16970,-16942,-16915,-16733,-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,
-16452,-16448,-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,-16212,-16205,
-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,-15933,-15920,-15915,-15903,-15889,-15878,
-15707,-15701,-15681,-15667,-15661,-15659,-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,
-15408,-15394,-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,-15149,-15144,
-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,-14941,-14937,-14933,-14930,-14929,-14928,
-14926,-14922,-14921,-14914,-14908,-14902,-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,
-14663,-14654,-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,-14170,-14159,
-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,-14109,-14099,-14097,-14094,-14092,-14090,
-14087,-14083,-13917,-13914,-13910,-13907,-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,
-13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,-13340,-13329,
-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,-13068,-13063,-13060,-12888,-12875,-12871,
-12860,-12858,-12852,-12849,-12838,-12831,-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,
-12320,-12300,-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,-11781,-11604,
-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,-11055,-11052,-11045,-11041,-11038,-11024,
-11020,-11019,-11018,-11014,-10838,-10832,-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,
-10329,-10328,-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254}; private static string[] pystr = new string[]{"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao",
"bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen",
"cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui",
"cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan",
"dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao",
"ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang",
"hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian",
"jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken",
"keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei",
"leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo",
"ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu",
"na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong",
"nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie",
"pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun",
"ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang",
"sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua",
"shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai",
"tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo",
"wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing",
"xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you",
"yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang",
"zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo",
"zi","zong","zou","zu","zuan","zui","zun","zuo"}; /// /// 获取拼⾳ /// /// 汉字 /// public static string Convert(string str) { byte[] array = new byte[2]; string returnstr = ""; int chrasc = 0; int i1 = 0; int i2 = 0; char[] nowchar = Array(); for (int j = 0; j < ; j++) { byte[] btchk = es(nowchar[j].ToString()); if ( == 1) { returnstr += nowchar[j].ToString(); continue; } array = btchk; //array = es(nowchar[j].ToString()); i1 = (short)(array[0]); i2 = (short)(array[1]); chrasc = i1 * 256 + i2 - 65536; if (chrasc > 0 && chrasc < 160) { returnstr += nowchar[j]; } else { for (int i = ( - 1); i >= 0; i--) { if (pyvalue[i] <= chrasc) { returnstr += pystr[i]; break; } } } } return returnstr; } /// /// 获取拼⾳⾸字母 /// /// 汉字 /// public static string GetShortPY(string str) { string tempStr = ""; foreach (char c in str) { if ((int)c >= 33 && (int)c <= 126) { tempStr += ng(); //字母和符号原样保留
} else { if ((int)c != 32)//如果不是空格,那么转换 { tempStr += GetPYChar(ng()); //累加拼⾳声母
} else//如果是空格那么在字符串中间+上⼀个空字符 { tempStr += " "; } } } return tempStr; } private static string GetPYChar(string c) { byte[] array = new byte[2]; array = es(c); int i = (short)(array[0] - '0') * 256 + ((short)(array[1] - '0')); if (i < 0xB0A1) return ""; if (i < 0xB0C5) return "a"; if (i < 0xB2C1) return "b"; if (i < 0xB4EE) return "c"; if (i < 0xB6EA) return "d"; if (i < 0xB7A2) return "e"; if (i < 0xB8C1) return "f"; if (i < 0xB9FE) return "g"; if (i < 0xBBF7) return "h"; if (i < 0xBFA6) return "j"; if (i < 0xC0AC) return "k"; if (i < 0xC2E8) return "l"; if (i < 0xC4C3) return "m"; if (i < 0xC5B6) return "n"; if (i < 0xC5BE) return "o"; if (i < 0xC6DA) return "p"; if (i < 0xC8BB) return "q"; if (i < 0xC8F6) return "r"; if (i < 0xCBFA) return "s";
发布评论