WARDY IT Solutions - SQL Server Blog

SMO Execute SQL Script

One of the questions asked during my recent presentation at the PASS Community summit was how to execute a script that is contained in a file using SMO.  Unfortunately we ran out of time to answer this question during the session.  So below is a VB.Net code snippet that can be used to execute a SQL Script using SMO:

Dim sr As StreamReader = New StreamReader("C:\script.sql")
Dim script As String = sr.ReadToEnd

Dim SMOServer As Server = New Server
Dim db As Database = SMOServer.Databases("northwind")
db.ExecuteNonQuery(script)
Published Friday, November 24, 2006 6:16 PM by admin
Anonymous comments are disabled