参数化查询需要一个参数(不接受空字符串)

时间:2011-12-20 10:44:18

标签: c# database asp.net-mvc-3 parameters

我只是在执行查询,并已开始使用参数。一切都很顺利,直到传递一个空字符串。它之前运行正常,使用"name = " + Name其中name可能或可能不为null。

有这么简单的方法吗?

同样关于这个话题,我还没有尝试过,但是我可以通过除数字之外的任何东西来识别参数。这是一个可能出错的区域,有14个数字参数,没有识别,除了希望它们的顺序正确。 (如果我来添加另一个怎么办?)

错误讯息:The parameterized query '(@0 nvarchar(5),@1 nvarchar(4),@2 nvarchar(4000),@3 nvarchar(400' expects the parameter '@2', which was not supplied.

参数化查询:

            db.Execute("Update User SET Name = @0 , Address1 = @1 , Address2 = @2 , Address3 = @3, Address4 = @4 , Postcode = @5 , Title = @6, " +
                        " Surname = @7 , Forename = @8 , Tel = @9, Fax = @10 , Mobile = @11 , Email = @12  WHERE UserNo = @13",
                        Name, Address1, Address2, Address3, Address4, Postcode, Title, Surname, Forename, Tel, Fax, Mobile, Email, UserNo);

1 个答案:

答案 0 :(得分:1)

db.Execute("Update User SET Name = @0 , Address1 = @1 , Address2 = @2 , Address3 = @3, Address4 = @4 , Postcode = @5 , Title = @6, " +
                        " Surname = @7 , Forename = @8 , Tel = @9, Fax = @10 , Mobile = @11 , Email = @12  WHERE UserNo = @13",
                        (Name==null)?"":Name, (Address1==null)?"":Address1, ...);