背景:需要传递null值到数据库
//封装对象
Book objBook = new Book()
{
BookImage =this.pbCurrentImage.Image==null?null: new Common.SerializeObjectToString().SerializeObject(this.pbCurrentImage.Image),
//其他参数...
};
调用存储过程后出现异常:
存储过程代码:
create procedure usp_EditBook
@BookImage text,
as
update Books set BookImage=@BookImage
where BookId=@BookId
go
看了网上,然后自己也尝试了一下:
①把封装对象代码改成空字符串,这样数据库的内容会改成空白(""传递到数据库里是空白,null传递会显示Null,有区别);
BookImage =this.pbCurrentImage.Image==null?"":
new Common.SerializeObjectToString().SerializeObject(this.pbCurrentImage.Image),
②VS的代码不变,修改数据库的存储过程,加上null默认值
BookImage =this.pbCurrentImage.Image==null?null:
new Common.SerializeObjectToString().SerializeObject(this.pbCurrentImage.Image),
create procedure usp_EditBook
@BookImage text=null,
as
update Books set BookImage=@BookImage
where BookId=@BookId
go
问题:以两种方法都会一定程度上改动数据库内容。
是否还有其他方法传递null值?以及第二种方法是否会造成其他影响?有待补充...