Eseguire una script SQL (Run a script SQL)

by 17. ottobre 2008 12.40

A me capita spesso di dover eseguire degli script anche complessi su dei database come ad esempio gli script di aspnet. Ecco quindi un po' di codice per risolvere, come sempre brillantemente :), il problema!

   1:  #Region " Codice per l'esecuzione di script SQL "
   2:   
   3:      Private Function GetScriptStatements(ByVal p As String) As String()
   4:          Dim statements As String() = p.Split(New String() {"GO " & vbCr & vbLf}, StringSplitOptions.RemoveEmptyEntries)
   5:          Return statements
   6:      End Function
   7:   
   8:      Private Sub ExecuteStatements(ByVal statements As String(), ByVal connectionString As String)
   9:          If statements.Length > 0 Then
  10:              Using conn As New SqlConnection()
  11:                  conn.ConnectionString = connectionString
  12:                  conn.Open()
  13:   
  14:                  Using command As New SqlCommand(String.Empty, conn)
  15:                      For Each statement As String In statements
  16:                          command.CommandText = statement
  17:                          command.ExecuteNonQuery()
  18:                      Next
  19:                  End Using
  20:              End Using
  21:          End If
  22:      End Sub
  23:   
  24:  #End Region

Ciao, alla prossima! Ogni tanto lasciatemi un salutino...

Tags: , , , ,

Page List

Calendar

<<  febbraio 2012  >>
lumamegivesado
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar