简体   繁体   English

如何在ADO.Net中潜在地将null作为参数传递

[英]How to potentially pass null in as a parameter in ADO.Net

I'm trying to pass a parameter into a stored proc using ADO.NET. 我正在尝试使用ADO.NET将参数传递到存储的proc中。 This paramater is of type int but the field in the database is nullable. 该参数的类型为int,但数据库中的字段可以为空。 Here's my code: 这是我的代码:

AddInParameter(cmd, "@i_Required_License_List_PKID", DbType.Int32, RequiredLicenseListPkid==""? null : Convert.ToInt32(RequiredLicenseListPkid);

When I do this I get an error saying that there's no conversion between null and int. 当我这样做时,我收到一条错误消息,指出null和int之间没有转换。

I have also tried DbNull.Value in place of null but with the same result. 我也尝试过DbNull.Value代替null,但结果相同。

I can't seem to find any nullable parameter types in the DbType class but I wonder is that the solution. 我似乎在DbType类中找不到任何可为空的参数类型,但我想知道解决方案是什么。

Use: DBNull.Value; 使用:DBNull.Value;

as the value of the parameter 作为参数的值

AddInParameter(cmd,“ @i_Required_License_List_PKID”,DbType.Int32,RequiredLicenseListPkid ==“”?DBNull.Value:Convert.ToInt32(RequiredLicenseListPkid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM