Luigi Di Biasi


XP Programming Style
Sondaggio
Argomento Spider e WebCrawler

Interessante
Non interessante

Costruire a runtime una stringa SQL di INSERT o UPDATE parametrizzata con valori DBNULL

Costruire a Runtime una stringa SQL di INSERT o UPDATE
Questa classe è stata scritta per facilitare la costruzione di stringhe di INSERT e UPDATE nel caso in cui è necessario gestire i campi di tipo NULL. 

Il suo funzionamento è molto semplice:

1) Accetta una lista di parametri (una terna composta da nome del campo del db, valore da inserire e tipo di dato)
2) Generare una stringa di INSERT o UPDATE accettando come parametro la tabella dove eseguirla (e la condizione where nel caso in cui sia una update)
3) Generare le stringhe in modo da omettere tutti i campi che come valore hanno nothing. (E questo il modo in cui viene discriminato un campo null)
4) Ritornare un SqlCommand da poter collegare ad una connection ed eseguirlo.


 Public Class _CreateParametrizedCommand

            Private toReturn As SqlClient.SqlCommand
            Private ParamToInsert As New Dictionary(Of String, SqlDbType)

            Public Function AddParameter(Of K)(ByVal ParamName As String, ByVal value As K, ByVal _type As SqlDbType)
                If IsNothing(toReturn) Then
                    toReturn = New SqlClient.SqlCommand()
                    toReturn.Parameters.Clear()
                End If
                ' parametro già inserito
                If ParamToInsert.ContainsKey(ParamName) Then
                    Throw New Exception("Esiste già un parametro di nome: " & ParamName)
                    Return False
                End If
                ParamToInsert.Add(ParamName, _type)

                 '#se non è nothing lo aggiunge
                If Not IsNothing(value) Then
                    toReturn.Parameters.AddWithValue(ParamName, _type).Value = value
                End If
                Return True

            End Function
            Public Function Get_INSERT_InstanceOfParametrizedCommand(ByVal TableName As String) As SqlClient.SqlCommand
                If ParamToInsert.Count = 0 Then
                    Throw New Exception("Nessun parametro inserito.")
                End If
 
                toReturn.CommandText = "INSERT INTO " & TableName & " ("
                For Each it As KeyValuePair(Of String, SqlDbType) In ParamToInsert
                    toReturn.CommandText &= it.Key & ","
                Next it
                toReturn.CommandText = Mid(toReturn.CommandText, 1, Len(toReturn.CommandText) - 1) & ") "
                toReturn.CommandText &= " VALUES ("
                For Each it As KeyValuePair(Of String, SqlDbType) In ParamToInsert
                    toReturn.CommandText &= "@" & it.Key & ","
                Next it
                toReturn.CommandText = Mid(toReturn.CommandText, 1, Len(toReturn.CommandText) - 1) & ") "
                Return toReturn
            End Function

            Public Function Get_UPDATE_InstanceOfParametrizedCommand(ByVal TableName As String, ByVal WHERE_CONDITION As String) As SqlClient.SqlCommand
                If ParamToInsert.Count = 0 Then
                    Throw New Exception("Nessun parametro inserito.")
                End If
                toReturn.CommandText = "UPDATE " & TableName & " SET "
                For Each it As KeyValuePair(Of String, SqlDbType) In ParamToInsert
                    toReturn.CommandText &= it.Key & "=@" & it.Key & ","
                Next it
                toReturn.CommandText = Mid(toReturn.CommandText, 1, Len(toReturn.CommandText) - 1) & " "
                toReturn.CommandText &= " " & WHERE_CONDITION
                Return toReturn
            End Function
      End Class
    End Class


Categoria: Come fare a ...
martedì, 08 feb 2011 Ore. 17.40
Statistiche
  • Views Home Page: 25.827
  • Views Posts: 49.658
  • Views Gallerie: 0
  • n° Posts: 41
  • n° Commenti: 33
Archivio Posts
Anno 2012

Anno 2011

Anno 2010

Anno 2009

Anno 2008
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003