views:

1623

answers:

4

I want to run my .sql script file using my ASP.NET website through ADO.NET. How it could be it is not working?

When I try

'dbScript is a string and contains contents of the .sql file'
Dim cmd As New SqlCommand(dbScript, con)
Try
    con.Open()
    cmd.ExecuteNonQuery()
Catch ex As Exception
Finally
    con.Close()
    cmd.Dispose()
End Try

I get exceptions when GO statement executed in script. How can I fix this problem?

+4  A: 

GO is not a Transact-SQL statement, is a tools batch delimiter. The server rightfully complain of a syntax error when GO is encountered in a batch. You need to split the file into batches and then execute individual batches. Use a regular expression that splits the file inot batches and recognizes GO case insensitive on a single line.

Remus Rusanu
+1  A: 

It's because GO isn't actually a native TSQL statement, it's used in Management Studio/Enterprise Manager to divide the script into batches.

You either need to:
1) split it into multiple individual scripts on each GO statement
2) use the Server class within SQL Management Objects, as exampled here

AdaTheDev
+3  A: 

See my blog post about Handling GO Separators in SQL - The Easy Way. The trick is to use SMO's ExecuteNonQuery() method. For example, here's some code that will run all scripts in a directory, regardless of GO separators:

 using System;
 using System.IO;
 using System.Data.SqlClient;
 using System.Collections.Generic;

 //Microsoft.SqlServer.Smo.dll
 using Microsoft.SqlServer.Management.Smo;
 //Microsoft.SqlServer.ConnectionInfo.dll
 using Microsoft.SqlServer.Management.Common;

 public class RunAllSqlSriptsInDirectory
 {
     public static void Main()
     {
         string scriptDirectory = "c:\\temp\\sqltest\\";
         string sqlConnectionString = "Integrated Security=SSPI;" + 
             "Persist Security Info=True;Initial Catalog=Northwind;Data Source=(local)";
         DirectoryInfo di = new DirectoryInfo(scriptDirectory);
         FileInfo[] rgFiles = di.GetFiles("*.sql");
         foreach (FileInfo fi in rgFiles)
         {
             FileInfo fileInfo = new FileInfo(fi.FullName);
             string script = fileInfo.OpenText().ReadToEnd();
             SqlConnection connection = new SqlConnection(sqlConnectionString);
             Server server = new Server(new ServerConnection(connection));
             server.ConnectionContext.ExecuteNonQuery(script);
         }
     }
 }
Jon Galloway
Note though that taking a dependency on SMO will require your application to pre-install the SMO redistributable, a minor inconvenience. But the real deal-killer is that SMO is version specific and will flat out refuse to connect to higher versions of SQL: an application developed using the SMO from SQL 2k5 will not connect to a SQL Server 2k8, requiring the developer to release a new version of its app that uses SMO 2k8.
Remus Rusanu
Remus, good points. These aren't as big of a deal when you're running scripts on your own website Muhammad is asking.
Jon Galloway
Thanks for your help. hope to find generic and platform independent solution soon. if you do pls share here. thanks again
Muhammad Adnan
You should wrap the SqlConnection declaration in a in using()
Keltex
A: 

Nice one Please Visit lasatalks.blogspot.com lasatalks.blogspot.com

lasantha