Feedback

SQL INSERT-Stringbuilder Function

Sprache: VB

Benutzung: Dim fishe() As String = {"Goldfische", "Blas"} Dim werte() As String = {"24 Goldies", "37 Blas"} MsgBox(SQL_Build_INSERT("Tiere", fishe, werte)) Ergebis: INSERT INTO Tiere (`Goldfische`, `Blas`) VALUES ('Goldfische', 'Blas') Hoffe es hilft euch weiter… GreeZ TTP
Private Function SQL_Build_INSERT(ByVal Tablename As String, ByVal Felder As Array, ByVal Werte As Array) As String
        Dim res As String = Nothing
        If Tablename = Nothing Or Felder.Length = 0 Or Felder.Length <> Werte.Length Then
            Throw New Exception("Tablename or Felder = NOTHING or Felder.Length <> Werte.Length")
        End If
        Dim ai As Integer = Felder.Length
        Dim i As Integer
        res = "INSERT INTO " & Tablename & " ("
        For i = 0 To Felder.Length - 1
            If i = 0 Then
                res &= "`" & Felder(i).ToString & "`"
            Else
                res &= ", `" & Felder(i).ToString & "`"
            End If
        Next
        res &= ") VALUES ("
        For i = 0 To Werte.Length - 1
            If i = 0 Then
                If Werte(i).ToString = Nothing Then
                    res &= "NULL"
                Else
                    res &= "'" & Felder(i).ToString & "'"
                End If
            Else
                If Werte(i).ToString = Nothing Then
                    res &= ", NULL"
                Else
                    res &= ", '" & Felder(i).ToString & "'"
                End If
            End If
        Next
        res &= ")"
        Return res
    End Function
Private Function SQL_Build_INSERT(ByVal Tablename As String, ByVal Felder As Array, ByVal Werte As Array) As String
        Dim res As String = Nothing
        If Tablename = Nothing Or Felder.Length = 0 Or Felder.Length <> Werte.Length Then
            Throw New Exception("Tablename or Felder = NOTHING or Felder.Length <> Werte.Length")
        End If
        Dim ai As Integer = Felder.Length
        Dim i As Integer
        res = "INSERT INTO " & Tablename & " ("
        For i = 0 To Felder.Length - 1
            If i = 0 Then
                res &= "`" & Felder(i).ToString & "`"
            Else
                res &= ", `" & Felder(i).ToString & "`"
            End If
        Next
        res &= ") VALUES ("
        For i = 0 To Werte.Length - 1
            If i = 0 Then
                If Werte(i).ToString = Nothing Then
                    res &= "NULL"
                Else
                    res &= "'" & Felder(i).ToString & "'"
                End If
            Else
                If Werte(i).ToString = Nothing Then
                    res &= ", NULL"
                Else
                    res &= ", '" & Felder(i).ToString & "'"
                End If
            End If
        Next
        res &= ")"
        Return res
    End Function

5 Kommentare

  1. Der Code ist auch nur Gedacht um die Programmierung von festen SQL-Abfragen zu vereinfachen. Ich hab ein Programm was mehrere verschiedene SQL Abfragen ändert. Der Betreiber des SQL-Servers ändert nur gern mal Feldnamen und so macht es mir das einfacher und schneller den Code zu ändern und nicht alle SQL-Strings per Hand umzuschreiben, denn ich brauch dann nur den nötigen Array ändern.

    Der Meinung, dass man diesen Code vor SQL-Injection schĂĽtzen muss, bin ich aber auch!

  2. http://dotnet-snippets.de/dns/c-schutz-vor-sql-injektion-SID201.aspx
    C# Schutz vor SQL-Injektion

    Das ganze noch fĂĽr die Update-Routinge
    [code]Private Function SQL_Build_UPDATE(ByVal Tablename As String, ByVal Felder As Array, ByVal Werte As Array, ByVal WHERE As String, Optional ByVal LIMIT As String = „“) As String
    Dim res As String = Nothing
    If Tablename = Nothing Or Felder.Length = 0 Or Felder.Length <> Werte.Length Then
    Throw New Exception(„Tablename or Felder = NOTHING or Felder.Length <> Werte.Length“)
    End If
    Dim ai As Integer = Felder.Length
    Dim i As Integer
    res = „UPDATE “ & Tablename & “ SET “
    For i = 0 To Felder.Length – 1
    If Werte(i).ToString = Nothing Then
    If i = 0 Then
    res &= „`“ & Felder(i).ToString & „` = NULL“
    Else
    res &= „, `“ & Felder(i).ToString & „` = NULL“
    End If
    Else
    If i = 0 Then
    res &= „`“ & Felder(i).ToString & „` = ‚“ & Werte(i).ToString & „‚“
    Else
    res &= „, `“ & Felder(i).ToString & „` = ‚“ & Werte(i).ToString & „‚“
    End If
    End If
    Next

    res &= “ WHERE “ & WHERE
    If Not LIMIT = „“ Then
    res &= “ LIMIT “ & LIMIT
    End If

    Return res
    End Function[/code]

    Funktionsweise:
    [code] Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim felder() As String = {„id“, „name“, „change“}
    Dim werte() As String = {„6“, „bla“, „“}
    MsgBox(SQL_Build_UPDATE(„Table“, felder, werte, „id=’4′“, „1“))
    End Sub[/code]

  3. Im Zusammenspiel mit dem MSSQL Server 2008 musste ich ein paar kleine Änderungen vornehmen, folgender Abschnitt scheint mir aber einen kleinen Fehler zu enthalten:

    [code] For i = 0 To Werte.Length – 1
    If i = 0 Then
    If Werte(i).ToString = Nothing Then
    res &= „NULL“
    Else
    res &= „‚“ & [b]Werte[/b](i).ToString & „‚“
    End If
    Else
    If Werte(i).ToString = Nothing Then
    res &= „, NULL“
    Else
    res &= „, ‚“ & [b]Werte[/b](i).ToString & „‚“
    End If
    End If
    Next
    [/code]
    MSSQL 2008
    [code]Public Function SQL_Build_INSERT(ByVal Tablename As String, ByVal Felder As Array, ByVal Werte As Array) As String
    Dim res As String = Nothing
    If Tablename = Nothing Or Felder.Length = 0 Or Felder.Length <> Werte.Length Then
    Throw New Exception(„Tablename or Felder = NOTHING or Felder.Length <> Werte.Length“)
    End If
    Dim ai As Integer = Felder.Length
    Dim i As Integer
    res = „INSERT INTO “ & Tablename & “ (“
    For i = 0 To Felder.Length – 1
    If i = 0 Then
    res &= „“““ & Felder(i).ToString & „“““
    Else
    res &= „, „““ & Felder(i).ToString & „“““
    End If
    Next
    res &= „) VALUES (“
    For i = 0 To Werte.Length – 1
    If i = 0 Then
    If Werte(i).ToString = Nothing Then
    res &= „NULL“
    Else
    res &= „‚“ & Werte(i).ToString & „‚“
    End If
    Else
    If Werte(i).ToString = Nothing Then
    res &= „, NULL“
    Else
    res &= „, ‚“ & Werte(i).ToString & „‚“
    End If
    End If
    Next
    res &= „)“
    Return res
    End Function[/code]