Resmy Ravi

Resmy Ravi

  • NA
  • 128
  • 0

Executing script in sql server using c# code

Oct 12 2010 5:27 AM

hi experts..
I need to execute a script in sql server using c#.i have used this code
string
sqlConnectionString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=FinAccounts;Data Source=computer3\\SQLEXPRESS";
FileInfo file = new FileInfo(@"E:\SQLQuery3.sql");
string script = file.OpenText().ReadToEnd();
MessageBox.Show(script.ToString());
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
file.OpenText().Close();
but an error "Failed to connect to the server" appears...what is the problem.
what should i change in the code?..please replay...
 

Answers (3)