Execute Sql Script file in Asp.net C# [Database generate]

Get your PDF copy

Overview: This article explains how to execute SQL script file in C# .i.e here in our Asp.net Webform on button click event will execute the SQL Script file and generate database .i.e Tables, Data in MS SQL server.

To create SQL Database in Asp.net is quite difficult, Here will see how to create SQL Database or how to  execute SQL Script File in asp.net c#.

First we need to import some DLL

  1. Microsoft.SqlServer.ConnectionInfo.dll
  2. Microsoft.SqlServer.Smo.dll

These two DLL we have to import for executing SQL script file from our Asp.net Web Application.

So that it iterates SQL scripts in a directory and executes them with ConnectionContext.ExecuteNonQuery.

Code :


  • Microsoft.SqlServer.Smo.dll, Microsoft.SqlServer.ConnectionInfo.dll is need.
  • You will find these online or check in your windows i.e. at C/programfiles/sqlserver/assembly/…
  • Your Database Script contains GO statement, if not using smo.dll it will throw an error.

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

Get your Printable Copy