如何通过更新查询设置记录sql vb.net
我有表,我想设置值,如果cell = null,然后通过文本框设置记录。 这里是我的表如何通过更新查询设置记录sql vb.net
--------------------
col1| col2| Note
1 2 aaa
5 5 (*) set record Only here if cell is Null
42 14
47 55
------------------
这里是我的代码,我的问题是每一个细胞是空查询写我想写只为下一个单元格,以前不为空
con.Open()
query = " update firealarm set [email protected] where Note Is Null"
Dim command As SqlCommand = New SqlCommand(query, con)
command.Parameters.Add(New SqlParameter("@Note", SqlDbType.NVarChar))
command.Parameters("@Note").Value = Notebox.Text.ToString
command.ExecuteNonQuery()
con.Close()
首先,让我们来解决一下即时问题:在SQL中,NULL
不等于任何东西,包括其他NULL
s,所以Note<>Null
对于所有行都是正确的。为了解决这个问题,SQL有特殊的运算符IS NULL
和IS NOT NULL
。使用where Note is not NULL
将解决问题。
但是更大的问题仍然存在:您的程序容易受到SQL注入攻击。要在Visual Basic中解决此问题,请参阅this Q&A。
编辑:(在回答这个问题的编辑)
我想写只为下一个单元格,以前它是
not null
的“下一个单元格”意味着一些订单表中的单元格。由于表格行应视为无序集合,因此让我们假设您想按col1
订购数据在您的示例中的排序方式。
查询条件成为这个更大:
UPDATE f
SET [email protected]
FROM firealarm f
WHERE Note Is Null
AND NOT EXIST (
SELECT * FROM firealarm ff WHERE ff.col1 < f.col1 AND ff.Note IS NULL
)
新WHERE
条件说,需要更新时对电池进行与在Note
列最低col1
和NULL
值。
你能成为我的导师吗? :3 – Sherlock
@dasblinkenlight ---- query =“update firealarm set Note = @ Note Note Note Null”----问题是所有单元格中的记录都是空的,我只想设置下一个单元格后不为空并保持所有的空单元格 – Subrati
@Subrati你需要'注意不是NULL'。之后,添加'query.Parameters.Add(“@ Note”,SqlDbType.VarChar,50).Value = Notebox.Text'来设置'@ Note'参数的值。 – dasblinkenlight
要更新确切行,你需要在WHERE
语句的详细情况
UPDATE firealarm SET [email protected]
WHERE Note IS NULL
AND Col1 = @ValueOfCol1
AND Col2 = @ValueOfCol2
vb.net
代码
con.Open()
Dim query As String = <![CDATA[UPDATE firealarm SET [email protected]
WHERE Note IS NULL
AND Col1 = @ValueOfCol1
AND Col2 = @ValueOfCol2]]>.Value
Dim command As SqlCommand = New SqlCommand(query, con)
With command.Parameters
.Add(New SqlParameter With {.ParameterName = "@Note",
.SqlDbType = SqlDbType.NVarChar,
.Value = Notebox.Text})
.Add(New SqlParameter With {.ParameterName = "@ValueOfCol1",
.SqlDbType = SqlDbType.Int,
.Value = 5})
.Add(New SqlParameter With {.ParameterName = "@ValueOfCol2",
.SqlDbType = SqlDbType.Int,
.Value = 5})
End With
command.ExecuteNonQuery()
con.Close()
或身份列的利用价值,如果你的表有这个
尝试此查询,它会更新只是第一行,为空
WITH UpdateList_view AS
(SELECT TOP 1 * from Fire alarm
Where NOTE is Null)
update UpdateList_view
set [email protected]
如何确保你可以更新的行将需要一个? – Fabio
没有不能保证,没有几个细节,希望必须有一些id列 –
你很容易sql注入 – Sherlock
'query =“更新firealarm设置注= @注意哪里注意是空的” – Subrati