I've searching for this and I thought I found the answer on here. this is the code I found to run a sql script through c#:
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace SeleniumTest2
{
class CreateSchema
{
public void Schema_Create()
{
string sqlConnectionString = "connection string here";
FileInfo file = new FileInfo(@"filepath to script.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
//DOESNTLIKE
server.ConnectionContext.ExecuteNonQuery(script);
file.OpenText().Close();
conn.Close();
}
}
}
But I keep getting the following error:
An unhandled exception of type 'Microsoft.SqlServer.Management.Common.ExecutionFailureException' occurred in Microsoft.SqlServer.ConnectionInfo.dll
Additional information: An exception occurred while executing a Transact-SQL statement or batch.
Can anyone tell me how to overcome this error? THANKS!!