public void TestPerforman() { DateTime timeNow = DateTime.Now; //Console.Out.WriteLine("time now:" + (DateTime.Now)); for (int i = 0; i < 1000; i++) { string constr = ConfigCache.GetDBConnectStr("SQLSERVER"); MySQLHelper.ExecuteDataset(constr, CommandType.Text, "select * from city ", null); MySQLHelper.ExecuteDataset(constr, CommandType.Text, "select * from city ", null); MySQLHelper.ExecuteDataset(constr, CommandType.Text, "select * from city ", null); } //Console.Out.WriteLine("time now:" + (DateTime.Now)); Console.Out.WriteLine("Aop use time:" + timeNow.Subtract(DateTime.Now).Duration()); timeNow = DateTime.Now; //Console.Out.WriteLine("time now:" + (DateTime.Now)); for (int i = 0; i < 1000; i++) { BizBO bizBo = TransactionAOP.newInstance(typeof(BizBO)) as BizBO; bizBo.queryCity(); } //Console.Out.WriteLine("time now:" + (DateTime.Now)); Console.Out.WriteLine("No Aop use time:" + timeNow.Subtract(DateTime.Now).Duration()); }
[TestMethod]//获取数据库连接语句 oracle public void GetDBConnectStrTest_ORACLE() { ConfigCache.LoadAppConfig(null); Assert.AreEqual( "Data Source=172.30.1.65:1522/xe;user id=czjd_sd;password=czjd_sd;", ConfigCache.GetDBConnectStr("ORACLE")); }
[TestMethod]//获取数据库连接语句 sqlserver public void GetDBConnectStrTest_SQLSERVER() { ConfigCache.LoadAppConfig(null); Assert.AreEqual( "User ID=sa;initial catalog=DCETLBig;Data Source=sep;Password=Pw123456;", ConfigCache.GetDBConnectStr("SQLSERVER")); }
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]//获取数据库连接语句 mysql 暂时未实现 public void GetDBConnectStrTest_MYSQL() { ConfigCache.LoadAppConfig(null); Assert.AreEqual( "", ConfigCache.GetDBConnectStr("MYSQL")); }