VSTO (Access) How to execute a SQL Statement from a File?
public bool ExecuteSqlFromFile(String MyConnectionString, String MyTextFilePath)
{
using (OleDbConnection sqlConnection = new OleDbConnection(MyConnectionString))
{
OleDbCommand cmd = new OleDbCommand();
try
{
using (StreamReader fh = new StreamReader(MyTextFilePath))
{
String MyText;
string s;
while ((s = fh.ReadLine()) != null)
MyText = s;
fh.Close();
cmd.CommandText = s;
}
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
cmd.ExecuteNonQuery();
sqlConnection.Close();
return true;
}
catch (Exception)
{
throw;
}
finally
{
sqlConnection.Close();
cmd.Dispose();
}
}
}