如何在UPDATE命令的WHERE子句中使用多个条件。 [英] How do I use more than one condition in WHERE clause in UPDATE command.

查看:2040
本文介绍了如何在UPDATE命令的WHERE子句中使用多个条件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我根据多个WHERE标准的建议构建语法,但它显示以下错误消息:



查询表达式中的语法错误(缺少操作)'Sap_No = 142 AND WHERE MNT = Jan AND WHERE YR = 2018'



什么是问题语法和谁来纠正它?



我试过跟随:



什么我试过了:



Private Sub BtnSave_Click(发送者作为对象,e作为EventArgs)处理BtnSave.Click

'* ************************************************** ****************************************

BtnSave。 Enabled = False:Dbconnection.Close():Dbconnection.Open()

'LEN = LvMasterAdapter.Fill(MasterDataSet,LeaveMaster)



Dim DesInd As Integer:Dim MN,YRR As String

LvMasterReader = LvMasterCmd.ExecuteReader()





LvMasterReader.Read()



如果LvMasterReader(Mnt)= CmbMonth.Text和LvMasterReader( Yr)= CmbYear.Text和LvMasterReader(SAP_No)= TxtSapID.Text然后

MN = CmbMonth.Text:YRR = CmbYear.Text

DesInd = LvMasterReader( DesigIndex)



如果LvMasterReader(LeaveStatus)=N那么



LvMasterCmd。 CommandText =UPDATE LeaveMaster SET Sap_No = @Sap_No,From_Date = @From_Date,To_Date = @ To_Date,LeaveType = @ LeaveType,Days = @ Days,LeaveStatus = @ LesveType WHERE Sap_No =& CInt(TxtSapID.Text)& 和MNT =& MN& 和YR =& YRR





尝试

LvMasterCmd.Parameters.AddWithValue(@ Sap_No,CInt(TxtSapID.Text) ))

LvMasterCmd.Parameters.AddWithValue(@ From_Date,DtpFrom.Value)

LvMasterCmd.Parameters.AddWithValue(@ To_Date,DtpTo.Value)

LvMasterCmd.Parameters.AddWithValue(@ LeaveType,CmbLeaveType.Text)

LvMasterCmd.Parameters.AddWithValue(@ Days,LblDays.Text)

LvMasterCmd.Parameters.AddWithValue(@ MNT,CmbMonth.Text)

LvMasterCmd.Parameters.AddWithValue(@ YR,CmbYear.Text)

LvMasterCmd。 Parameters.AddWithValue(@ LeaveStatus,Y)





LvMasterReader.Close()



LvMasterCmd.ExecuteNonQuery()

LvMasterCmd.Para meter.Clear()

退出Sub

Catch ex As Exception

MessageBox.Show(ex.Message)

结束尝试

结束如果



结束Sub

Hi friends,

I Have build the syntax as per suggestion with multiple WHERE criteria but it displays Following Error message:

Syntax error ( Missing Operation ) in query expression 'Sap_No = 142 AND WHERE MNT = Jan AND WHERE YR = 2018'

What is problem with Syntax and Who to Correct it ?

I Have Tried as Follows:

What I have tried:

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
'*******************************************************************************************
BtnSave.Enabled = False : Dbconnection.Close() : Dbconnection.Open()
'LEN = LvMasterAdapter.Fill(MasterDataSet, "LeaveMaster")

Dim DesInd As Integer : Dim MN, YRR As String
LvMasterReader = LvMasterCmd.ExecuteReader()


LvMasterReader.Read()

If LvMasterReader("Mnt") = CmbMonth.Text And LvMasterReader("Yr") = CmbYear.Text And LvMasterReader("SAP_No") = TxtSapID.Text Then
MN = CmbMonth.Text : YRR = CmbYear.Text
DesInd = LvMasterReader("DesigIndex")

If LvMasterReader("LeaveStatus") = "N" Then

LvMasterCmd.CommandText = "UPDATE LeaveMaster SET Sap_No= @Sap_No, From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LesveType WHERE Sap_No= " & CInt(TxtSapID.Text) & " AND WHERE MNT= " & MN & " AND WHERE YR= " & YRR


Try
LvMasterCmd.Parameters.AddWithValue("@Sap_No", CInt(TxtSapID.Text))
LvMasterCmd.Parameters.AddWithValue("@From_Date", DtpFrom.Value)
LvMasterCmd.Parameters.AddWithValue("@To_Date", DtpTo.Value)
LvMasterCmd.Parameters.AddWithValue("@LeaveType", CmbLeaveType.Text)
LvMasterCmd.Parameters.AddWithValue("@Days", LblDays.Text)
LvMasterCmd.Parameters.AddWithValue("@MNT", CmbMonth.Text)
LvMasterCmd.Parameters.AddWithValue("@YR", CmbYear.Text)
LvMasterCmd.Parameters.AddWithValue("@LeaveStatus", "Y")


LvMasterReader.Close()

LvMasterCmd.ExecuteNonQuery()
LvMasterCmd.Parameters.Clear()
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If

End Sub

推荐答案

了解更多SQL(这里是 WHERE 子句)。一般语法是

Read about SQL (here the WHERE clause). The general syntax is
WHERE <search_condition>

其中搜索条件可以是使用 AND OR NOT 的组合。



所以你的SQL语句应包含类似

where the search condition can be a combination using AND or OR and NOT.

So your SQL statement should contain something like

WHERE ap_No = 142 AND MNT = Jan AND YR = 2018



相关读取(此处为T-SQL):

WHERE(Transact -SQL)| Microsoft Docs [ ^ ]

搜索条件(Transact-SQL)| Microsoft Docs [ ^ ]


尝试:

Try:
UPDATE LeaveMaster SET Sap_No= @Sap_No, From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LesveType WHERE Sap_No= " & CInt(TxtSapID.Text) & " AND MNT= " & MN & " AND YR= " & YRR



但是......不要这样做。

你清楚地知道如何使用参数提到查询,那么为什么要在那里抛出字符串连接并让自己对SQL注入开放?



永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:


But ... don't do that.
You clearly know how to use parameterised queries, so why throw a string concatenation in there and leave yourself wide open to SQL Injection?

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


这篇关于如何在UPDATE命令的WHERE子句中使用多个条件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆