Run a .sql script file in C#
        Posted  
        
            by 
                SAMIR BHOGAYTA
            
        on Samir ASP.NET with C# Technology
        
        See other posts from Samir ASP.NET with C# Technology
        
            or by SAMIR BHOGAYTA
        
        
        
        Published on 2011-03-17T05:19:00.000-07:00
        Indexed on 
            2011/03/17
            16:18 UTC
        
        
        Read the original article
        Hit count: 382
        
Run a .sql script file in C#
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
FileInfo file = new FileInfo("C:\\myscript.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
}
}
© Samir ASP.NET with C# Technology or respective owner