Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

ExecuteSqlScripts - Executing an array of script files

Total Hit ( 2508)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' Execute an array of sql script files with batch statements,
' by using the same SqlCommand object
' - connString is the connection string for the destination database
'
' Example:
'  Dim connString As String = "server=(local);Persist Security
' Info=False;Integrated Security=SSPI;database=TestDB"
'  Dim script1 As String = "C:\Scripts\CreateDbTables.sql"
'  Dim script2 As String = "C:\Scripts\CreateDbSprocs.sql"
'  Dim script3 As String = "C:\Scripts\InsertData.sql"
'  ExecuteSqlScripts(connString, script1, script2, script3)

Sub ExecuteSqlScripts(ByVal connString As String, ByVal ParamArray scriptFiles() _
  As String)
  ' open the connection and create a command that uses it
  Dim cn As New SqlConnection(connString)
  Dim cmd As New SqlCommand
  Try
    cn.Open()
    cmd.Connection = cn

    Dim scriptFile As String
    For Each scriptFile In scriptFiles
      ' get the content of the script file
      Dim sr As System.IO.StreamReader = Nothing
      Try
        sr = New System.IO.StreamReader(scriptFile)
        ' use the script file's content as CommandText for the command
        ' created above
        cmd.CommandText = sr.ReadToEnd()
      Catch ex As Exception
        Throw ex
      Finally
        If Not sr Is Nothing Then sr.Close()
      End Try
      ' execute this script
      cmd.ExecuteNonQuery()
    Next
  Catch ex As Exception
    Throw ex
  Finally
    cn.Close()
  End Try
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.