Caution when passing sqlparameter in .net environment

Check the followings out, i encounter with an exception when running the following code:

DataList1.DataSource = SqlHelper.ExecuteDataTable(CommandType.StoredProcedure, "artspGetPainting01", new SqlParameter("@IsForRecentOrSearch", 0));

and i get the following message:

Procedure or Function 'artspGetPainting01' expects parameter '@IsForRecentOrSearch', which was not supplied.

Open the immediate window and input the command to check the values for testing:

?new SqlParameter("@IsForRecentOrSearch", 1)
{@IsForRecentOrSearch}
    base {System.Data.Common.DbParameter}: {@IsForRecentOrSearch}
    CompareInfo: None
    DbType: Int32
    Direction: Input
    IsNullable: false
    LocaleId: 0
    Offset: 0
    ParameterName: "@IsForRecentOrSearch"
    Precision: 0
    Scale: 0
    Size: 0
    SourceColumn: ""
    SourceColumnNullMapping: false
    SourceVersion: Current
    SqlDbType: Int
    SqlValue: {1}
    TypeName: ""
    UdtTypeName: ""
    Value: 1
    XmlSchemaCollectionDatabase: ""
    XmlSchemaCollectionName: ""
    XmlSchemaCollectionOwningSchema: ""
?new SqlParameter("@IsForRecentOrSearch", 0)
{@IsForRecentOrSearch}
    base {System.Data.Common.DbParameter}: {@IsForRecentOrSearch}
    CompareInfo: None
    DbType: Int64
    Direction: Input
    IsNullable: false
    LocaleId: 0
    Offset: 0
    ParameterName: "@IsForRecentOrSearch"
    Precision: 0
    Scale: 0
    Size: 0
    SourceColumn: ""
    SourceColumnNullMapping: false
    SourceVersion: Current
    SqlDbType: BigInt
    SqlValue: null
    TypeName: ""
    UdtTypeName: ""
    Value: null
    XmlSchemaCollectionDatabase: ""
    XmlSchemaCollectionName: ""
    XmlSchemaCollectionOwningSchema: ""
?new SqlParameter("@IsForRecentOrSearch", (object)0)
{@IsForRecentOrSearch}
    base {System.Data.Common.DbParameter}: {@IsForRecentOrSearch}
    CompareInfo: None
    DbType: Int32
    Direction: Input
    IsNullable: false
    LocaleId: 0
    Offset: 0
    ParameterName: "@IsForRecentOrSearch"
    Precision: 0
    Scale: 0
    Size: 0
    SourceColumn: ""
    SourceColumnNullMapping: false
    SourceVersion: Current
    SqlDbType: Int
    SqlValue: {0}
    TypeName: ""
    UdtTypeName: ""
    Value: 0
    XmlSchemaCollectionDatabase: ""
    XmlSchemaCollectionName: ""
    XmlSchemaCollectionOwningSchema: ""

Google exception message, and find the resolution of this issue:

http://msdn.microsoft.com/zh-cn/library/0881fz2y.aspx
当在 value 参数中指定 Object 时,SqlDbType 将从 Object 的 .NET Framework 类型推断出。
请小心使用 SqlParameter 构造函数的这个重载来指定整数参数值。因为此重载接受 Object 类型的 value,所以当此值为零时,必须将整数值转换为 Object 类型,如下面的 C# 示例所示。
Parameter = new SqlParameter("@pname", Convert.ToInt32(0));
如果不执行该转换,则编译器将认为您尝试调用 SqlParameterstringSqlDbType)构造函数重载。

http://msdn.microsoft.com/en-us/library/0881fz2y.aspx
When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET Framework type of the Object.
Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.
Parameter = new SqlParameter("@pname", Convert.ToInt32(0));
If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameter (string, SqlDbType) constructor overload.


In briefly, when input '0', the program invoke SqlParameter constructor ' SqlParameter ( string, SqlDbType)', not ' SqlParameter ( string, Object) ',  the essential is that zero can not be convert to 'Object' value implicitly.

Pertinent Info:

转载于:https://www.cnblogs.com/albertly/archive/2008/04/30/1178080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值