Yeah, you make an sproc that contains the script and execute the sproc
in the console application using ADO.NET SQL Command object.
I am not sure if it is as simple as that ... Or maybe I miss
understood your suggestion.
My T-SQL script includes various GO's and creates a database, its log,
filegroups, tables, relationships, etc.
Since the database does not exist I use a connection to master and I
use the following C# code:
using System;
using System.IO;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
public static class Lab {
public static void Run() {
string sqlConnectionString = "Data Source=WorkGroup\
\SQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI";
FileInfo file = new FileInfo("C:\\Users\\Miguel\\Projects\\WCA\
\Solution\\WCA.Lab\\Site.sql");
String script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
}
This is working fine. Any suggestion to improve it is welcome.
Thanks,
Miguel