删除SQL更新中WHERE之前的最后一个逗号

时间:2011-05-25 08:50:08

标签: asp.net sql vb.net string

我手动从“SET Column = @param”字符串构建SQL UPDATE语句。我遇到的问题是WHERE之前的这些SET语句中的最后一个不应该包含逗号,因为这是语法错误。

所以我的代码看起来像这样:

Public Sub Update(byval id as Integer, Optional byval col1 as String = Nothing, Optional byval col2 as Integer = -1)

 Dim sqlupdateid As SqlParameter
 Dim sqlupdatecol1 As SqlParameter
 Dim sqlupdatecol2 As SqlParameter

 Using sqlupdate As SqlCommand = _connection.CreateCommand()
    sqlupdate.CommandType = CommandType.Text
    Dim updatedelim As String = ","
    Dim setcol1 As String = "SET Col1 = @col1"
    Dim setcol2 As String = "SET Col2 = @col2"

   sqlupdate.CommandText = "UPDATE dbo.MyTable"

    If Not IsNothing(col1) Then
        sqlupdate.CommandText += " "
        sqlupdate.CommandText += setcol1
        sqlupdate.CommandText += updatedelim

        sqlupdatecol1 = sqlupdate.CreateParameter()
        sqlupdatecol1.ParameterName = "@col1"
        sqlupdatecol1.DbType = SqlDbType.VarCHar
        sqlupdatecol1.Value = col1
        sqlupdate.Parameters.Add(sqlupdatecol1)
    End If
    If Not col2 = -1 Then
        sqlupdate.CommandText += " "
        sqlupdate.CommandText += setcol2
        sqlupdate.CommandText += updatedelim

        sqlupdatecol2 = sqlupdate.CreateParameter()
        sqlupdatecol2.ParameterName = "@col2"
        sqlupdatecol2.DbType = SqlDbType.Int
        sqlupdatecol2.Value = col2
        sqlupdate.Parameters.Add(sqlupdatecol2)
    End If

    sqlupdate.CommandText += " WHERE ID = @id"
    //try to remove the last comma before the WHERE... (doesn't work)
    Dim temp As String = sqlupdate.CommandText
    Dim space As Char = " "
    Dim list As String() = temp.Split(space)
    Dim last As String = String.Empty
    Dim removed As String = String.Empty
    For Each s As String In list
        If s.Contains("WHERE") Then
            If last.Contains(",") Then
                removed = last.TrimEnd(",")
            End If
        End If
        last = s
    Next

    Dim cmd As String = list.ToString()
    sqlupdate.CommandText = cmd
    sqlupdateid = sqlupdate.CreateParameter()
    sqlupdateid.ParameterName = "@id"
    sqlupdateid.DbType = SqlDbType.Int
    sqlupdateid.Value = id
    sqlupdate.Parameters.Add(sqlupdateid)

    sqlupdate.ExecuteNonQuery()
  End Using
End Sub

任何人都可以建议一个更好的算法来删除SQL语句中WHERE之前的最后一个逗号,还要记住它之前的SET语句数量会有所不同吗?

由于我在每组之后添加逗号,因为另一个可以跟随或不跟随,所以我必须在构建UPDATE后,找到最后一个逗号并将其删除。

所以看起来应该是这样的:

UPDATE dbo.MyTable SET Col1 = @col1, SET Col2 = @col2 WHERE id = @id

4 个答案:

答案 0 :(得分:3)

String Query = Query.Substring(0,Query.Length-1)

答案 1 :(得分:3)

动态创建UPDATE查询(不带WHERE子句)。

然后做if (q.EndsWith(",")) then q = q.Substr(0,q.Length-1)

最后附加到q WHERE子句。

答案 2 :(得分:3)

当您大量使用+=运算符时,我建议您完全改变方法。您可以将字符串放在列表中并将它们连接起来以形成逗号分隔列表:

Public Sub Update(byval id as Integer, Optional byval col1 as String = Nothing, Optional byval col2 as Integer = -1)

  Dim sqlupdateid As SqlParameter
  Dim sqlupdatecol1 As SqlParameter
  Dim sqlupdatecol2 As SqlParameter

  Using sqlupdate As SqlCommand = _connection.CreateCommand()
    sqlupdate.CommandType = CommandType.Text

    Dim sets As New List(Of String)

    If Not IsNothing(col1) Then
      sets.Add("Col1 = @col1")
      sqlupdatecol1 = sqlupdate.CreateParameter()
      sqlupdatecol1.ParameterName = "@col1"
      sqlupdatecol1.DbType = SqlDbType.VarChar
      sqlupdatecol1.Value = col1
      sqlupdate.Parameters.Add(sqlupdatecol1)
    End If
    If Not col2 = -1 Then
      sets.Add("Col2 = @col2")
      sqlupdatecol2 = sqlupdate.CreateParameter()
      sqlupdatecol2.ParameterName = "@col2"
      sqlupdatecol2.DbType = SqlDbType.Int
      sqlupdatecol2.Value = col2
      sqlupdate.Parameters.Add(sqlupdatecol2)
    End If

    sqlupdate.CommandText = _
      "UPDATE dbo.MyTable SET " +_
      String.Join(", ", sets.ToArray()) +_
      " WHERE ID = @id"

    sqlupdateid = sqlupdate.CreateParameter()
    sqlupdateid.ParameterName = "@id"
    sqlupdateid.DbType = SqlDbType.Int
    sqlupdateid.Value = id
    sqlupdate.Parameters.Add(sqlupdateid)

    sqlupdate.ExecuteNonQuery()
  End Using
End Sub

答案 3 :(得分:1)

而不是花哨的字符串操作,你可以这样做

UPDATE dbo.MyTable
SET Col1 = @col1, Col2 = @col2, @id = @id
WHERE id = @id

即,追加@id = @id

您可以为UPDATE

中的变量指定值
...
      | @variable = expression
      | @variable = column = expression
      ....
      | @variable { += | -= | *= | /= | %= | &= | ^= | |= } expression
      | @variable = column { += | -= | *= | /= | %= | &= | ^= | |= } expression

课程表达可以是另一个@variable