/// <summary> /// /// </summary> /// <param name="sql"></param> /// <param name="paramNames"></param> /// <param name="paramValues"></param> /// <param name="dbName"></param> /// <returns></returns> public new DataSet ExecuteX(string sql, String[] paramNames, Object[] paramValues, string dbName, CommandType pCommandType) { using (Connection = DBUtil.GetConnection(dbName)) { try { Connection.Open(); this.dbType = ConfigCache.GetDBType(dbName); this.Transaction = this.Connection.BeginTransaction(); m_CommandType = pCommandType; DataSet result = ExecuteX(sql, paramNames, paramValues); this.Transaction.Commit(); return(result); } catch (Exception e) { this.Transaction.Rollback(); throw e; } finally { if (Connection != null && ConnectionState.Open == Connection.State) { Connection.Close(); } } } }
public DataTable Query(string sql, String[] paramNames, Object[] paramValues, string dbName) { using (Connection = DBUtil.GetConnection(dbName)) { try { Connection.Open(); this.dbType = ConfigCache.GetDBType(dbName); return(Query(sql, paramNames, paramValues)); } finally { if (Connection != null && ConnectionState.Open == Connection.State) { Connection.Close(); } } } }
public void Parse(string fileName) { Application xlApp; Workbook xlWorkBook; Worksheet xlWorkSheet; ADOTemplate template = new ADOTemplate(); xlApp = new ApplicationClass(); xlWorkBook = xlApp.Workbooks.Open(fileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); for (int iSheet = 1; iSheet <= xlWorkBook.Worksheets.Count; iSheet++) { xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(iSheet); range = xlWorkSheet.UsedRange; rowCount = range.Rows.Count; if (rowCount < 2) { continue; } colCount = range.Columns.Count; this.ReadConfig(); this.ReadColumns(); IDbTransaction Transaction = null; using (IDbConnection connection = DBUtil.GetConnection(dbName)) { try { connection.Open(); Transaction = connection.BeginTransaction(); new TransactionContext(connection, Transaction, ConfigCache.GetDBType(dbName)); for (int iRow = 2; iRow <= rowCount; iRow++) { Hashtable data = this.ReadDataRow(iRow); string op = (string)data["$op"]; if ("s".Equals(op)) { template.Execute((string)data["$sql"], null, null); } else if ("i".Equals(op)) { template.Execute(this.insertSql, data); } else if ("d".Equals(op)) { template.Execute(this.deleteSql, data); } } Transaction.Commit(); } catch (Exception e) { if (Transaction != null) { Transaction.Rollback(); } throw e; } finally { if (connection != null && ConnectionState.Open == connection.State) { connection.Close(); } } } ReleaseObject(xlWorkSheet); } xlWorkBook.Close(true, null, null); xlApp.Quit(); ReleaseObject(xlWorkBook); ReleaseObject(xlApp); }
public void Intercept(IInvocation invocation) { if (count > 0) { // 嵌套调用 invocation.Proceed(); return; } count++; IDbTransaction transaction = null; string DBName = ""; object[] attrs = invocation.TargetType.GetCustomAttributes(typeof(DBSourceAttribute), false); if (attrs != null && attrs.Length > 0) { DBName = (attrs[0] as DBSourceAttribute).name; } //short dbType = 0; short dbType = ConfigCache.GetDBType(DBName); // 从类中指定, // 从方法中指定 if (connectString == null) { //connectString = System.Configuration.ConfigurationManager.ConnectionStrings["CONNECTION_STRING"].ConnectionString; connectString = ConfigCache.GetDBConnectStr(DBName); //connectString = "Data Source=172.30.1.65:1522//xe;;user id=etl;password=etl;"; //connectString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=Virgil-Chen)(PORT=1522)))(CONNECT_DATA=(SERVICE_NAME=XE)));User Id=czjd_sd;Password=czjd_sd;"; //connectString = "Data Source=Virgil-PC:1522//xe;;user id=czjd_sd;password=czjd_sd;"; //connectString = "server=localhost;User Id=root;database=sakila;Password=root;"; } using (IDbConnection connection = DBUtil.GetConnection(dbType, connectString)) { try { connection.Open(); string methodName = invocation.MethodInvocationTarget.Name.ToLower(); if (!(methodName.StartsWith("get") || methodName.StartsWith("find") || methodName.StartsWith("query") || methodName.StartsWith("list"))) { if (log.IsDebugEnabled) { log.Debug("BeginTransaction"); } transaction = connection.BeginTransaction(); } new TransactionContext(connection, transaction, dbType); invocation.Proceed(); if (transaction != null) { transaction.Commit(); } } catch (Exception e) { if (transaction != null) { transaction.Rollback(); } log.Error(e.Message, e); throw e; } finally { if (log.IsDebugEnabled) { log.Debug("connection.Close()"); } if (connection != null && ConnectionState.Open == connection.State) { connection.Close(); } count--; } } }
///// <summary> ///// 把Row类型数据,通过反射,转为指定类型的对象, ///// 转换时, ///// 1)创建objectType的实例object,遍历objectType中的所有属性 ///// 2)每一属性,先看有没有Column Attribute定义, ///// a)如果有,取出column's name, ///// b)若没有,则属性名 ///// 作为数据行字段查询名字(名字统一细写) ///// 3)从数据行查出来的值,放于实例object中 ///// </summary> ///// <param name="row">数据行</param> ///// <generic name="T">返回的对象类型</generic> ///// <returns>objectType指定类型的实例</returns> ///// author=Edgar Ng //public static T Row2Object<T>(DataRow row) where T : new() //{ // T obj = new T(); // PropertyInfo[] props = obj.GetType().GetProperties(); // foreach (PropertyInfo prop in props) // { // string proName = prop.Name; // //if it has ColumAttribute,then get ColumnAttribute name ; // object[] attrs1 = prop.GetCustomAttributes(typeof(ColumnAttribute), true); // if (attrs1.Length > 0)//if true,it has ColumnAttribute // { // ColumnAttribute colum = attrs1[0] as ColumnAttribute; // if (colum != null && colum.Name != null) // { // proName = colum.Name; // } // } // if (row.Table.Columns.Contains(proName)) // { // object val = row[proName]; // if (val == System.DBNull.Value || val == null) // { // continue; // } // //prop.SetValue(obj, val, null); // SetPropertyValue(obj, prop, val); // } // } // return obj; //} //private static void SetPropertyValue(object obj, PropertyInfo prop, object val) //{ // Type propType = prop.PropertyType; // /*if (prop.PropertyType.Equals(val.GetType())) // { // prop.SetValue(obj, val, null); // } // else*/ // if (typeof(double).Equals(propType)) // { // prop.SetValue(obj, Convert.ToDouble(val), null); // } // else // { // prop.SetValue(obj, val, null); // } //} ///// <summary> ///// 把Table中的数据,通过反射,转为指定类型的对象列表 ///// </summary> ///// author=Edgar Ng ///// <param name="DataTable">数据表</param> ///// <generic name="T">返回列表中,元素的对象类型</generic> ///// <returns>objectType指定类型的列表</returns> //public static IList<T> Table2List<T>(DataTable table) where T : new() //{ // IList<T> result = new List<T>(table.Rows.Count); // T obj = new T(); // PropertyInfo[] props = obj.GetType().GetProperties(); // IList<Object[]> proNameList = getEntityName<T>(props, table);//Object[]:Object[]{PropertyInfo,string} // foreach (DataRow row in table.Rows) // { // T t = new T(); //t is temp // for (int i = 0; i < proNameList.Count; i++) // { // PropertyInfo prop = proNameList[i][0] as PropertyInfo; // string strColumnName = proNameList[i][1] as string; // prop.SetValue(t, row[strColumnName], null); // } // result.Add(t); // } // return result; //} ///// <summary> ///// to support the function Table2List(): --by edgar ///// </summary> //private static IList<Object[]> getEntityName<T>(PropertyInfo[] props, DataTable dt) //{ // IList<Object[]> result = new List<Object[]>(); // DataRow drTemp = dt.Rows[0]; // foreach (PropertyInfo prop in props) // { // string proName = prop.Name; // object[] colAtt = prop.GetCustomAttributes(typeof(ColumnAttribute), true); // if (colAtt.Length > 0)//if true,it has ColumnAttribute // { // ColumnAttribute colum = colAtt[0] as ColumnAttribute; // if (colum != null) // { // proName = colum.Name; // } // } // if (drTemp.Table.Columns.Contains(proName)) // { // Object[] objTemp = new Object[] { prop, proName }; // result.Add(objTemp); // } // } // return result; //} #endregion /// <summary> /// get connection Object by dbType & connect String /// </summary> /// <param name="dbType"></param> /// <param name="connectString"></param> /// <returns></returns> public static IDbConnection GetConnection(string dbName) { return(GetConnection(ConfigCache.GetDBType(dbName), ConfigCache.GetDBConnectStr(dbName))); }
[TestMethod]//获取数据库类型 返回short类型 oracle-0 sqlserver-1 public void GetDBTypeTest() { ConfigCache.LoadAppConfig(null); Assert.AreEqual(0, ConfigCache.GetDBType("ORACLE")); }