DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Execute Script File Of Sql Server From Code

  • submit to reddit
        // Execute script file of sql server from code

Public Shared Function ExceuteScriptFile(ByVal ScriptFilePath As String, Optional ByVal MyConnectionKey As String ) As Integer
        Dim db As Database
        Dim MyServer As String = AppSettings("Server")
        Dim MyUserName As String = AppSettings("userid")
        Dim MyPassword As String = AppSettings("password")
        Dim MyDbName As String = AppSettings("DBName")

        Dim ScriptExeceute As New Process
        Dim osqlParams As String = ""
        db = DatabaseFactory.CreateDatabase(MyConnectionKey )
        ScriptFilePath = """" + ScriptFilePath + """"
        osqlParams = String.Format("-S {0} -U {1} -P {2} -d {3} -i ", MyServer, MyUserName, MyPassword, MyDbName)
        ScriptExeceute.StartInfo.FileName = "sqlcmd.exe"
            ScriptExeceute.StartInfo.Arguments = osqlParams & ScriptFilePath
            ScriptExeceute.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        Catch ex As Exception

        End Try
        Return 0
End Function