Introduction:
In this article,i am going to explain about how to execute a sql script file using asp.net and c#.
Main:
we can automate some installtion sql scripts like below,
string sqlConnectionString = "Data Source=(local);Initial Catalog=NetProgrammingHelp;Integrated Security=True";
FileInfo file = new FileInfo("C:\\SampleScript.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
string sqlConnectionString = "Data Source=(local);Initial Catalog=NetProgrammingHelp;Integrated Security=True"; FileInfo file = new FileInfo("C:\\SampleScript.sql"); string script = file.OpenText().ReadToEnd(); SqlConnection conn = new SqlConnection(sqlConnectionString); Server server = new Server(new ServerConnection(conn)); server.ConnectionContext.ExecuteNonQuery(script); |
Conclusion:
Hope this helps,
Happy coding.