sql语句如果存在则更新else插入sql server

时间:2015-05-11 13:55:26

标签: sql-server insert

很高兴再次回来 我正在研究项目以计算度量和数字,并从number.text和measure.text在datagridview(dgv)中插入数据,但是如果在dgv son中存在的measure.text的值更新了具有新值+当前值的数字单元格 问题是怎么做.... 我尝试了很多sql语句,但我有错误

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        If Not sqlcon.State = ConnectionState.Open Then
            '  open connection
            sqlcon.Open()
        End If
                com2 = New SqlCommand("UPDATE frames SET [number] = [number] + '" & number.Text & "' where measure = '" & measure.Text & "'", sqlcon)
                com2.ExecuteNonQuery()

                com = New SqlCommand("insert into frames ([number],measure) values('" & number.Text & "','" & measure.Text & "'),('" & number1.Text & "','" & measure1.Text & "')", sqlcon)
                com.ExecuteNonQuery()

            End If
        Next


    End Sub

这是我正在处理的代码,但我搜索更好的方法并找到这些

com2 = New SqlCommand("UPDATE frames SET [number] = [number] + '" & number.Text & "' where measure = '" & measure.Text & "' if @@rowcount=0 insert into frames ([number],measure) values('" & number.Text & "','" & measure.Text & "'),('" & number1.Text & "','" & measure1.Text & "'", sqlcon)
com2.ExecuteNonQuery()

但它给了我错误'93。附近的语法不正确......... 93.5是在measure.text中存在的值,但所有的sql语句都没有,除了这个错误我不知道为什么 我尝试过另一种方式

com = New SqlCommand("IF NOT EXISTS (SELECT * FROM  frames) BEGIN insert into frames ([number],measure) values('" & number.Text & "','" & measure.Text & "'),('" & number1.Text & "','" & measure1.Text & "') END ELSE BEGIN UPDATE frames SET [number] = [number] + '" & number.Text & "' where measure = '" & measure.Text & "'", sqlcon)
com2 = New SqlCommand("IF EXISTS (SELECT * FROM frames) THEN BEGIN Update frames set [number] = [number] + '" & number.Text & "' where measure = '" & measure.Text & "' End ELSE bEGIN INSERT INTO frames ([number],measure) values('" & number.Text & "','" & measure.Text & "'),('" & number1.Text & "','" & measure1.Text & "' END", sqlcon)
com2.ExecuteNonQuery()

我收到此错误“****关键字'THEN'附近的语法不正确。

关键字“ELSE”附近的语法不正确。

关键字“END”。****“或

附近的语法不正确

我收到此错误“'61 .19'附近的语法不正确。” ......... 61.19是measure.text中存在的值

请帮我找到没有错误的方法,或者这些????

我听说过交易但是我这样搜索并且什么都不懂......

0 个答案:

没有答案