Click here to Skip to main content
14,734,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to execute long SQL scripts (usually coming from files) using DbConnection. The scripts may contain multiple INSERT, UPDATE, CREATE statements. The database may vary. That's why I'm using DbConnection. Is it possible?

Thanks.
Posted

1 solution

Try This,
 string sqlConnectionString = @"Integrated Security=true Info=False;Initial Catalog=mydb;Data Source=SUMMAT";

FileInfo file = new FileInfo(@"C\Script.sql");

 string script = file.OpenText().ReadToEnd();

 SqlConnection conn = new SqlConnection(sqlConnectionString);

 Server server = new Server(new ServerConnection(conn));

 server.ConnectionContext.ExecuteNonQuery(script);
 file.OpenText().Close();
   
Comments
Bumbala 26-Nov-11 9:23am
   
Thank you for the answer but I think your solution is dependent to SQL Server. My database may be Oracle, Access, PG etc. Will it work for any database?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900