running sql script file

Posted by Sanmukhait.Btech under C# on 4/20/2012 | Points: 10 | Views : 1967 | Status : [Member] | Replies : 3
I want to run sql script file through my c# code by passing one parameter.how to achive this

sanmukh


Responses

Posted by: Sabarimahesh on: 4/20/2012 [Member] Bronze | Points: 25

Up
0
Down
Sanmukhait.Btech

dO wANT TI rUN A sP OR SQL SCRIPT


Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/20/2012 [Member] Bronze | Points: 25

Up
0
Down
IF u wANT TO Run a Script


This is the code


using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
FileInfo file = new FileInfo("C:\\myscript.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
}
}


Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/20/2012 [Member] Bronze | Points: 25

Up
0
Down
if you want to Run a Stored Procedure

SqlCommand cmd = new SqlCommand("SProcedureName", cn);

SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet dset = new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id.ToString();
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = NameString;
cn.Open();
cmd.ExecuteNonQuery();
adp.Fill(dset);
YourGridID.DataSource = dset;
YourGridID.DataBind();


Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response