没有好的办法一般是如下的形式:
if (objValue == DbNull.Value)
{
return null//return ""//return .....
}
else
{
return ......
}
if (objValue == DbNull.Value)
{
return null//return ""//return .....
}
else
{
return ......
}
解决方案 »
- 晕,怎么在窗体用sql查数据库的表跟在代码中直接编写查找的效果不一样
- PictureBox的单击事件
- C# 遭遇灵异事件!
- 关于datagridview的数据源的连接字符串的问题,在线等!
- 听说WPF支持TreeView的数据绑定,不知哪位做过,能不能举个例子?
- 求正则表达式转换方法(方法越多越好);最好能进行简要说明;
- C#中怎样调用word的宏?
- 请教BHO中如何提供浏览器外部方法?
- 请教数据库的基本操作:添加、修改、删除怎么做?
- 为什么dropdownlistbox选择后恢复到初始值
- 在web中如何实现选中一行就可以得到该列的所有值
- 如何在Grid中限制一列只能输入整数 **注:(有负数的)
public void SetDefaultValue(object value)
{
Type type=value.GetType();
foreach(PropertyInfo propertyInfo in type.GetProperties())
{
try
{
if(propertyInfo.PropertyType==typeof(int) || propertyInfo.DeclaringType==typeof(short))
propertyInfo.SetValue(value,0,null); else if(propertyInfo.PropertyType==typeof(string))
propertyInfo.SetValue(value,string.Empty,null); else if(propertyInfo.PropertyType==typeof(DateTime))
propertyInfo.SetValue(value,DateTime.Now,null); else if(propertyInfo.PropertyType==typeof(bool))
propertyInfo.SetValue(value,false,null); else if(propertyInfo.PropertyType==typeof(Decimal))
{
Decimal dValue=0;
propertyInfo.SetValue(value,dValue,null);
}
else if(propertyInfo.PropertyType.IsEnum)
{
object enumValue=Activator.CreateInstance(propertyInfo.PropertyType);
enumValue= System.Enum.GetValues(propertyInfo.PropertyType).GetValue(0);
propertyInfo.SetValue(value,enumValue,null);
}
else
{
string s=propertyInfo.Name;
//暂时就这么多类型
}
}
catch(Exception){}
}
}