using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
namespace Ray.BusinessRule
{
public class LogicBase
{
protected Microsoft.Practices.EnterpriseLibrary.Data.Database db =
Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("CPDB");
/// <summary>
/// 根据传入的实体的值,进行联合查询
/// </summary>
/// <param name="source">包含查询条件的实体</param>
/// <returns>包含查询结果的数据集</returns>
public DataSet Search(Ray.SystemFramework.EntityBase source)
{
// 获取具体实体类的类型
Type myType = source.GetType();// Type.GetType("source");
PropertyInfo pi;
// 定义T-sql语句的开头
string sqlCom = "select * from " + source.TableName + " where 1=1 ";
stringBuilder sbCondition = new StringBuilder();
// 遍历实体所有属性
foreach (object obj in source.Fields.Keys)
{
pi = myType.GetProperty(Convert.ToString(source.Fields[obj]));
//System.Reflection.PropertyAttributes pa = pi.Attributes;
object value = pi.GetValue(source, null); // 获取属性值
// 若属性值为空,跳过
if (value == null)
{
continue;
} // 否则拼接T-sql语句
sbCondition.Append("and " + Convert.ToString(obj)); // 读取表字段名,查询条件为AND
sbCondition.Append("='" + Convert.ToString(value) + "' "); // 读取对应字段的值
}
// 生成T-sql命令字串
sqlCom += sbCondition.ToString();
// 调用Enterprise Library中Database的方法,执行sqlCom,并返回数据集
return db.ExecuteDataSet(CommandType.Text, sqlCom);
}
}
}大哥大姐们,帮我看看,我的这个程序传入的是一个实体.然后用Relection得到他的类型,取得他的属性值,属性值是用Hashtable组织的.现在是红色部门的语句,老是在运行时报错:未将对象引用设置到对象的实例。
请大家指点一下,那里有问题啊!
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
namespace Ray.BusinessRule
{
public class LogicBase
{
protected Microsoft.Practices.EnterpriseLibrary.Data.Database db =
Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("CPDB");
/// <summary>
/// 根据传入的实体的值,进行联合查询
/// </summary>
/// <param name="source">包含查询条件的实体</param>
/// <returns>包含查询结果的数据集</returns>
public DataSet Search(Ray.SystemFramework.EntityBase source)
{
// 获取具体实体类的类型
Type myType = source.GetType();// Type.GetType("source");
PropertyInfo pi;
// 定义T-sql语句的开头
string sqlCom = "select * from " + source.TableName + " where 1=1 ";
stringBuilder sbCondition = new StringBuilder();
// 遍历实体所有属性
foreach (object obj in source.Fields.Keys)
{
pi = myType.GetProperty(Convert.ToString(source.Fields[obj]));
//System.Reflection.PropertyAttributes pa = pi.Attributes;
object value = pi.GetValue(source, null); // 获取属性值
// 若属性值为空,跳过
if (value == null)
{
continue;
} // 否则拼接T-sql语句
sbCondition.Append("and " + Convert.ToString(obj)); // 读取表字段名,查询条件为AND
sbCondition.Append("='" + Convert.ToString(value) + "' "); // 读取对应字段的值
}
// 生成T-sql命令字串
sqlCom += sbCondition.ToString();
// 调用Enterprise Library中Database的方法,执行sqlCom,并返回数据集
return db.ExecuteDataSet(CommandType.Text, sqlCom);
}
}
}大哥大姐们,帮我看看,我的这个程序传入的是一个实体.然后用Relection得到他的类型,取得他的属性值,属性值是用Hashtable组织的.现在是红色部门的语句,老是在运行时报错:未将对象引用设置到对象的实例。
请大家指点一下,那里有问题啊!
解决方案 »
- 保存图片到数据库里面
- 问一个关于ScrollBar的问题
- 几个关于listBox和label的小问题
- 讨论:这两种连接方式,哪种好一些。
- 一个关于多线程同步的问题
- C#.net2005中水晶报表打包部署找不到水晶报表的合并模块
- 请问:那种工具软件可以把各种音频视频文件压缩到最小?
- c# 有个savefiledialog 的问题
- ado.net的dataset保存的xml文件,用ado的recordset如何打开?
- 在服务/service里启动外部应用程序/创建进程,运行不正常
- 有没开发过短信猫程序的大大亚?请问有那些短信猫用C#用起来方便亚?最近要做个相关的,但是不知道该买那种?
- 提示更新成功,为何数据库数据没更新
仔细检查一下你的这一句:
pi = myType.GetProperty(Convert.ToString(source.Fields[obj])); 可能是myType没有source.Fields[obj] 这个属性,或者 source.Fields[obj] 本身有问题。
如果此属性不属于这个实体呢,怎么取值?
仔细检查一下吧
或者加上 if(pi == null){}