[英]Assign null value to a decimal field in SQL
I want to assign null value
in a field in SQL 2014 which has a data type, decimal
. 我想在SQL 2014的字段中分配
null value
,该字段的数据类型为decimal
。 I tried to use theses codes but I didn't get the way I wanted (providing that other fieldnames and values are present.) 我尝试使用这些代码,但没有得到想要的方式(假设存在其他字段名和值。)
query = "INSERT INTO DBNAME.TLTRPRGP(DECIMALVAL) VALUES(@DECIMALVAL)"
params = New String() {"BLGSCD"}
values = New String() {Nothing}
SaveUpdateDelete(query, params, values)
query = "INSERT INTO DBNAME.TLTRPRGP(DECIMALVAL) VALUES(@DECIMALVAL)"
params = New String() {"BLGSCD"}
values = New String() {DbNull.Value}
SaveUpdateDelete(query, params, values)
query = "INSERT INTO DBNAME.TLTRPRGP(DECIMALVAL) VALUES(@DECIMALVAL)"
params = New String() {"BLGSCD"}
values = New String() {VbNull}
SaveUpdateDelete(query, params, values)
Private Sub SaveUpdateDelete(ByVal sql As String, ByVal parameters() As String, ByVal Values() As String)
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim command As New SqlCommand
command = New SqlCommand(sql, con)
For i = 0 To parameters.Count - 1
command.Parameters.AddWithValue("@" & parameters(i).ToString, Values(i))
Next
command.CommandText = sql
command.ExecuteNonQuery()
con.Close()
End Sub
The first code returns 0
. 第一个代码
returns 0
。 The second returns syntax error saying "Value System.DbNull.Value cannot be converted to String"
. 第二个返回语法错误,指出
"Value System.DbNull.Value cannot be converted to String"
。 The third code returns 1
. 第三代码
returns 1
。 All these codes failed to return the right value which is suppose to be NULL
or EMPTY
. 所有这些代码均未能返回正确的值,该值应该为
NULL
或EMPTY
。 What else is there to try? 还有什么可以尝试的?
the issue is with the datatype of the values: you constrain it to be a string but there are values that cannot be converted to strings (eg: DBNull.Value
). 问题在于值的数据类型:您将其约束为字符串,但是有些值无法转换为字符串(例如:
DBNull.Value
)。
the solution is to use the generic object
as the datatype for the parameters passed to the function SaveUpdateDelete
. 解决方案是将通用
object
用作传递给函数SaveUpdateDelete
的参数的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.