[英]Using VB.net IF statement with SQL value
I am trying to use an IF
statement to do a certain action if my SQL value is greater than 1. 如果我的SQL值大于1,我尝试使用
IF
语句执行某些操作。
However when I run the below code, it returns an error. 但是,当我运行以下代码时,它将返回错误。
Operator '>' is not defined for types 'System.Data.SqlClient.SqlCommand' and 'Integer'.
没有为类型'System.Data.SqlClient.SqlCommand'和'Integer'定义运算符'>'。
Here is the code: 这是代码:
Using cmdb = New SqlCommand("INSERT INTO AdditionalDaisyCodes (AddCode) VALUES (@AddCode)", con)
con.Open()
cmdb.ExecuteNonQuery()
con.Close()
If cmdb > 1 Then
MessageBox.Show("Duplicate Codes Exist!", "Indigo Billing", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
End If
End Using
Can anybody help with the correct syntax? 有人可以提供正确语法的帮助吗?
Use this 用这个
int result=cmdb.ExecuteNonQuery();
if result > 1 then
//code here
else
end if
You have a bigger problem than just a syntax error. 您有一个更大的问题,不仅仅是语法错误。 Based on your posted code, it looks like you want to inform the user that duplicate codes exist.
根据您发布的代码,您似乎想通知用户存在重复的代码。 Since you have an
Else
, I'm going to make a guess and say that if duplicate codes aren't found, you want the insertion made. 既然您有
Else
,我将作一个猜测,并说如果找不到重复的代码,则需要插入。
Currently the number of rows affected by ExecuteNonQuery()
will never be greater than 1. In addition, in your posted code you never assign a value to the @AddCode
parameter - if the AddCode
column cannot be null, that could cause an error. 当前,受
ExecuteNonQuery()
影响的行数永远不会大于1。此外,在发布的代码中,您永远不会为@AddCode
参数分配值-如果AddCode
列不能为null,则可能导致错误。
What you probably want to do is to check to see if the code exists, and if not then add it. 您可能想要做的是检查代码是否存在,如果不存在,则添加它。 Something like this would do the trick:
这样的事情可以解决问题:
Using cmdb = New SqlCommand("SELECT * FROM AdditionalDaisyCodes WHERE AddCode = @AddCode", con)
con.Open()
cmdb.Parameters.AddWithValue("@AddCode", txtAddCode.Text)
Dim reader As SqlDataReader = cmdb.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("Duplicate Codes Exist!", "Indigo Billing", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
cmdb.CommandText = "INSERT INTO AdditionalDaisyCodes(AddCode) Values(@AddCode)"
cmdb.Parameters.AddWithValue("@AddCode", txtAddCode.Text)
cmdb.ExecuteNonQuery()
End If
con.Close()
End Using
txtAddCode.Text
in the above example is assuming the code is coming from a TextBox
named txtAddCode
. txtAddCode.Text
中的txtAddCode.Text
假设代码来自于名为txtAddCode
的TextBox
。 First it checks to see if the code is already in the table by running a SELECT query restrained by a WHERE clause. 首先,它通过运行受WHERE子句约束的SELECT查询来检查表中是否已存在代码。 If the returned
SqlDataReader
has rows, then that code is already in the table and the message box is displayed. 如果返回的
SqlDataReader
有行,则该代码已在表中,并显示消息框。
If the SqlDataReader
does not have rows, then the code is not in the table and cmdb
is reused to do the insert. 如果
SqlDataReader
没有行,则该代码不在表中,并且cmdb
被重用于执行插入操作。
Also note that you can put the SqlConnection
in a Using
block as well, eliminating the need to call Close()
on the connection explicitly. 还要注意,您也可以将
SqlConnection
放在Using
块中,从而无需显式调用连接上的Close()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.