Execute Store procedure form Csharp (System.Diagnostics.Process)

Step1 :   Create a Query for creating a table and name this query Proc.sql and store it into C Drive

if object_Id('test_table')is not null drop table test_table
create table test_table
id int,
[Name] nvarchar(30)



Step2 :    in .CS file copy and paste this code

//change your dbname

ViewState["dbname"] = "yourdbname";
string ServerName = System.Environment.MachineName;
string CommandLineStringInfo = "-S " + ServerName + " -d " + ViewState["dbname"].ToString() + " -i C:\\proc.sql -o C:\\proc.Log";
System.Diagnostics.ProcessStartInfo myInfoInfo = new System.Diagnostics.ProcessStartInfo("sqlCMD", CommandLineStringInfo);
myInfoInfo.CreateNoWindow = true;
myInfoInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = myInfoInfo;



Step 3: For testing check if table created or not

Run this query

use [yourdbname]
select * from test_table
This entry was posted in Asp.Net, Sql. Bookmark the permalink.

One Response to Execute Store procedure form Csharp (System.Diagnostics.Process)

  1. says says:

    greate… job…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s