Running SQL Script from in a program.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi C# users;
I am trying to run an SQL Script on an Access database. I can do it
directly, but, what I would like to do is build a dll that I can call and
give it the databasename and the SQLScript filename and have it return an
integer for success or failure.

Example: int ExecuteSqlScript(string databasePathName, string script);

If you have any ideas I would be gratefull
Thanks.
Rich III
 
All you need to do is have the first parameter specify a valid connection
string, then thesecond one a valid command.

Then you can just use a OleDbCommand object, using a connection created
fromthe String. YOu can just specify the commandtext property of the
command to be the script. From there, you can call ExecuteNonQuery to fire
it.

Since you're using Access and not Sql Server, you'd need to do some
translating, but you could take a look at Microsoft's Data Access
Applicaiton Block to get an idea about how this works in more complex
scenarios - but in the simplest form - all you'd need to do is what I
mentioned above.
 
Rich,

That should be easy enough. Basically, you would load the contents of
the file into a string, and then set that string to a command (OleDbCommand
in the case of Access) object. The connection for the object (an
OleDbConnection) would be set with the provider for Access, as well as the
path (you would construct the whole thing dynamically). Then, all you have
to do is execute it.

Hope this helps.
 
Back
Top