Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i execute sql 2008 R2 script using C#.net the following code is not working for
sql 2008 R2 script using

private void RunScript(string Db)
{
string sqlConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=" + Db + ";";
FileInfo file = new FileInfo("D:\\giits.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
conn.Open();
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
conn.Close();

}

please help, thank u all
Posted
Comments
Prasad Avunoori 29-Jan-14 2:42am    
What is the error you got?
FIROZ KHANIT 29-Jan-14 3:12am    
private void RunScript(string Db)
{
string sqlConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=" + Db + ";";
using (SqlConnection connection = new SqlConnection(sqlConnectionString))
using (SqlCommand command = connection.CreateCommand())
{
string script = File.ReadAllText("D:\\allgiit.sql");
command.CommandText = script.Replace("GO","");
connection.Open();
int affectedRows = command.ExecuteNonQuery();
}
table are Store in database but in stored procedure its gave error

'CREATE/ALTER PROCEDURE' must be the first statement in a query batch'.
ZurdoDev 29-Jan-14 9:13am    
So your script is bad then?
FIROZ KHANIT 29-Jan-14 13:17pm    
what you say i can't understand
ZurdoDev 29-Jan-14 13:19pm    
You are executing SQL that is in a file. It sounds like the sql that is in the file is incorrect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900