ADO.NET - The Size property has an invalid size of 0

前端 未结 10 994
小蘑菇
小蘑菇 2020-12-14 13:41

I\'m trying to get output value from DB via ADO.NET. There\'s a client code:

    using (var connection = new SqlConnection(ConnectionString))
    {
                


        
相关标签:
10条回答
  • 2020-12-14 14:37

    I had this error occur, supplying a value that had a non-string, but nullable, type. In my case, an int?. I fixed it by passing in a non-nullable int instead.

    0 讨论(0)
  • 2020-12-14 14:40

    Check MSDN : SqlParameter.Size Property

    For bidirectional and output parameters, and return values, you must set the value of Size. This is not required for input parameters, and if not explicitly set, the value is inferred from the actual size of the specified parameter when a parameterized statement is executed.

    0 讨论(0)
  • 2020-12-14 14:41

    VarChar and NVarChar are variable width character fields (thus var+char). You have to set the length, otherwise the default is zero.

    0 讨论(0)
  • 2020-12-14 14:44

    Incidentally, setting the size property of an output parameter is necessary even if it isn't a string-type parameter. For example, if you are using a System.Data.SqlDbType.Int, you should set the size to be 4.

    0 讨论(0)
提交回复
热议问题