/// <summary> /// 执行存储过程并返回结果集 /// </summary> /// <param>存储过程名</param> /// <returns>DataSet</returns> public DataSet RunProcedure(string storedProcName) { DataSet ds = new DataSet(); try { this.Open(); cmd.CommandText = storedProcName; cmd.CommandType = CommandType.StoredProcedure; Adapter.SelectCommand = cmd; //Adapter.SelectCommand.CommandTimeout = 1200;//可以设置适当的超时时间(秒),避免选择时间段过大导致填充数据集超时 Adapter.Fill(ds); } catch (Exception ex) { throw ex; } finally { this.Close(); } return ds; } /// <summary> /// 执行存储过程,方法不返回结果集 /// </summary> /// <param></param> public void RunVoidProcedure(string storedProcName) { cmd.CommandText = storedProcName; cmd.CommandType = CommandType.StoredProcedure; try { this.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { this.Close(); } }
下面说两个反射方法,测试之后为了方便调用,减少操作添加的,一个是把实体类的属性转换为参数,另一个是把从数据库取出的某条记录转换为实体类,这两个还是非常有用,尤其是在系统开发时调用比较方便,以前我是见到反射就绕道走的,这次算是第一次用反射,发现确实是很方便。如下:
复制代码 代码如下: /// <summary> /// 将实体类的属性进行参数转换(ORACLE测试通不过,必须要求所有参数都包含在语句中才行) /// </summary> /// <param></param> /// <param></param> //public void ConvertToParameters(object model, DbParameterCollection ParaCollect) //{ // Type T = model.GetType(); // PropertyInfo[] propert = T.GetProperties(); // for (int i = 0; i < propert.Length; i++) // { // AddParam(propert[i].Name, propert[i].GetValue(model, null), ParaCollect); // } //} /// <summary> /// 将实体类的属性进行参数转换 /// </summary> /// <param></param> /// <param></param> public void ConvertToParameters(object model, DbParameterCollection ParaCollect,List<string> fields) { Type T = model.GetType(); PropertyInfo[] propert = T.GetProperties(); for (int i = 0; i < propert.Length; i++) { if (fields.Contains(propert[i].Name)) //检测必须参数化的实体属性 { AddParam(propert[i].Name, propert[i].GetValue(model, null), ParaCollect); } } }
/// <summary> /// 通过反射将取出的数据写入实体类(ORACLE测试通不过,需进行类型强制转换) /// </summary> /// <param></param> /// <param></param> //public void GetModel(object model, string cmdText) //{ // PropertyInfo propertyInfo; // DbDataReader dr = ExecuteDataReader(conn, cmdText); // while (dr.Read()) // { // for (int i = 0; i < dr.FieldCount; i++) // { // propertyInfo = model.GetType().GetProperty(dr.GetName(i)); // if (propertyInfo != null) // { // if (dr.GetValue(i) != DBNull.Value) // { // //Type t = dr.GetValue(i).GetType(); // propertyInfo.SetValue(model, dr.GetValue(i), null); // } // } // } // } // dr.Close(); // conn.Close(); //} /// <summary> /// 通过反射将数据绑定到实体对象,由于不同数据库对应于.NET的数据类型不一样 /// 需做强制类型转换 /// </summary> /// <param></param> /// <param></param> public void GetModel(object model, string cmdText) { PropertyInfo propertyInfo; DbDataReader dr = ExecuteDataReader(conn, cmdText); object _value; while (dr.Read()) { for (int i = 0; i < dr.FieldCount; i++) { propertyInfo = model.GetType().GetProperty(dr.GetName(i)); if (propertyInfo != null && dr.GetValue(i) != DBNull.Value) { switch (propertyInfo.PropertyType.ToString()) { case "System.String": { _value = Convert.ToString(dr.GetValue(i));//字符串是全球通用类型,也可以不用转换 propertyInfo.SetValue(model, _value, null); }break; case "System.Int32": { _value = Convert.ToInt32(dr.GetValue(i)); propertyInfo.SetValue(model, _value, null); } break; case "System.Single": { _value = Convert.ToSingle(dr.GetValue(i)); propertyInfo.SetValue(model, _value, null); } break; case "System.Decimal": { _value = Convert.ToDecimal(dr.GetValue(i)); propertyInfo.SetValue(model, _value, null); } break; case "System.Double": { _value = Convert.ToDouble(dr.GetValue(i)); propertyInfo.SetValue(model, _value, null); } break; case "": { _value = Convert.ToDateTime(dr.GetValue(i)); propertyInfo.SetValue(model, _value, null); } break; default: break; } } } } dr.Close(); conn.Close(); }
从上面的注释掉的方法对比中可以看到为了兼容不同的数据库,必须要做额外的处理,比如类型转换,SQL SERVER的int 对应ORALCE的number,UserInfo的字段属性UserAge定义的是int类型,连接ORALCE时,.NET识别number类型为System.Decimal,把Decimal赋值给Int32当然是不行的,所以得做强制转换才行。还有一点要注意下,就是将数据绑定到实体对象时,由于ORACLE坚持大写标准和解析机制,如果属性名和字段名大小写不一致的话,propertyInfo = model.GetType().GetProperty(dr.GetName(i)) ,propertyInfo 始终是null值,比如SELECT UserName,UserAge FROM USER_TEST WHERE USERID=$USERID,SQL SERVER 执行的时候调试可以看到dr.GetName(0)是UserName,dr.GetName(1)是UserAge,ORACLE执行解析就变了,全是大写了,变成了USERNAE,USERAGE,这么一来和找不到UserInfo类的属性了,因为UserInfo类的属性是 UserName,和UserAge,C#语言变量也是区分大小写的嘛,当然就找不到了,所以propertyInfo就为null了,故在这里再次建议大家在数据库设计和程序字段属性设计时采用大写标准(如果不涉及多数据库当然也不需要这么做)。 最后说下测试调用代码,首先webconfig配置里面这样配置下,主要选取SQL SERVER和ORACLE做测试,毕竟这是.NET支持的两个典型数据库,要是把.NET所支持的所有书库都测试一遍,那测试量可不小了,呵呵。
复制代码 代码如下: (编辑:焦作站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|