How to Execute SQL Scripts in .NET

Add the Required DLL

using System.IO;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Management.Common;

Create a connection to SQL server

SqlConnection sqCon = new SqlConnection("Connection String.....");

Execute SQL queries, procedures and functions

SqlCommand sqlCmd = new SqlCommand();
sqlCmd .CommandType = CommandType.Text;
sqlCmd .Connection = sqCon;
sqlCmd .CommandText = "sql query....."
sqCon.Open();
sqlCmd .ExecuteNonQuery();
sqCon.Close();

OR

SqlConnection sqCon = new SqlConnection("Your Connection String");
SqlCommand sqlCmd = new SqlCommand();
SqlDataReader reader;
sqlCmd.CommandText = " sql query.....";
sqlCmd.CommandType = CommandType.Text;
sqlCmd.Connection = sqCon;
sqCon.Open();
reader = sqlCmd.ExecuteReader();
sqCon.Close();

 

Next Recommended Reading Crop Image in ASP.Net using Script