当前位置:编程学习 > C#/ASP.NET >>

Hashtable 的问题

我是初学者,各位不要见笑,问题是这样的:
 MyHashTable.Add("@ID|int", 1);
 MyHashTable.Add("@No|string", "a");
 MyHashTable.Add("@BuyTime|datetime", BuyTimestr);
其中BuyTimestr变量是string型的,执行
MyDB.ExecuteSQL("update table1 set No=@No,BuyTime=@BuyTime where ID=@ID", MyHashTable);
如果变量BuyTimestr是空值就会报错,如果是空值时,我希望给字段BuyTime赋null,请问各位大侠,这段代码怎么改?
--------------------编程问答-------------------- BuyTimestr == string.Empty ? DBNull.Value : BuyTimestr --------------------编程问答-------------------- MyDB.ExecuteSQL("update table1 set No=@No,BuyTime= case when @BuyTime = '' Then NULL else  @BuyTime end   
where ID=@ID", MyHashTable); 

你看看 --------------------编程问答-------------------- 楼主你要给BuyTime赋null,那你可以这样,你直接在数据库字段中让这个字段可以为空,然后Update语句中不要给提到这个字段就像这样:update table1 set No=@No  where ID=@ID
--------------------编程问答-------------------- --------------------编程问答-------------------- 也许是我表达的不清楚,我的意思是:
MyHashTable.Add("@ID|int", 1); 
MyHashTable.Add("@No|string", "a"); 
MyHashTable.Add("@BuyTime|datetime", BuyTimestr); 
其中BuyTimestr变量是string型的,执行 
MyDB.ExecuteSQL("update table1 set No=@No,BuyTime=@BuyTime where ID=@ID", MyHashTable); 
如果变量BuyTimestr是空值就会报错,怎么解决报错的问题,(BuyTimestr如果是空值,表示@BuyTime|datetime 没有被赋值)
麻烦各位大侠再看看 --------------------编程问答-------------------- MyDB.ExecuteSQL("update table1 set No=@No,BuyTime= (case when @BuyTime = '' Then NULL else  @BuyTime end ) 
where ID=@ID", MyHashTable); 

你看看 --------------------编程问答-------------------- 报错的原因是把string类型转换成datetime时出的错,因为BuyTimestr变量是空值 --------------------编程问答-------------------- @BuyTime是datetime类型的啊 大侠 --------------------编程问答--------------------
引用 7 楼 xingtao1016 的回复:
报错的原因是把string类型转换成datetime时出的错,因为BuyTimestr变量是空值

那就用ISNULL() --------------------编程问答-------------------- MyHashTable.Add("@ID|int", 1);
MyHashTable.Add("@No|string", "a");
MyHashTable.Add("@BuyTime|datetime", BuyTimestr); 
if(string.isnullorempty(BuyTimestr))
MyDB.ExecuteSQL("update table1 set No=@No where ID=@ID", MyHashTable); 
else
MyDB.ExecuteSQL("update table1 set No=@No,BuyTime=@BuyTime where ID=@ID", MyHashTable);  --------------------编程问答--------------------
引用 7 楼 xingtao1016 的回复:
报错的原因是把string类型转换成datetime时出的错,因为BuyTimestr变量是空值

DateTime is a value type, it cannot represent null (or DBNull)

simple way is just to set it to DateTime.MinValue and when setting values
for stored proc parameters, check for DateTime.MinValue, and when that's the
value send null to the database (set DBNull.Value to be param value)
--------------------编程问答-------------------- 楼主首先要设置表中BuyTime可以为NULL
MyHashTable.Add("@BuyTime|datetime", BuyTimestr==null||BuyTimestr.equal("")?DBNull.Value:BuyTimestr); --------------------编程问答-------------------- MyHashTable.Add("@BuyTime|datetime", BuyTime == string.Empty ? DBNull.Value : BuyTime);
这句报错啊 ‘无法确定条件表达式的类型,因为“System.DBNull”和“String”之间没有隐式转换’
怎么回事啊? --------------------编程问答-------------------- 还没解决啊 帮帮各位大侠 --------------------编程问答-------------------- 高手都到那去了,这么个问题都解决不了?!!!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,