I am maintaining a simple DB tool that executes CreateDatabase on our EF model, and uses SMO to run some .sql scripts.
It currently uses:
var svrConnection = new ServerConnection(sqlConnection);
var server = new Server(svrConnection);
server.ConnectionContext.ExecuteNonQuery(fullSqlScript);
Is there a way to execute a TSQL script in .Net without using SMO?
Or, is there a way to successfully use SMO in an application without installing the thing on the server I’m running it on?
Any alternative that will be useful to me will require no installation on the box, besides xcopy of assemblies for my tool. It also must guarantee that the script will operate in exactly the same way without additional testing/verification.
The scripts use GO, etc, and cannot/should not be broken up – they’re generated by third-party tools (aspnet_regsql.exe), and by hand (but are stale at this point), so I do not want to touch them if I can avoid it.
I’m pretty sure the suggestions in the comments on this answer are wrong, because I’m pretty sure GO isn’t directly replacable with ;. If I’m wrong, please let me know 🙂
I’m trying to work around this problem:
Could not load file or assembly ‘Microsoft.SqlServer.SqlClrProvider, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ or one of its dependencies. The system cannot find the file specified.
The fix mentioned here was to install SMO, which requires other packages, which isn’t ideal for me.
Final solution, based off Russell McClure’s answer:
In the end I am going to end up with sqlcmd.exe, since scripts written for it (anything with GO in it) could do way too much to replicate through SqlCommand.ExecuteNonQuery in my own code. And since it has the same level of dependencies as SMO, and would take some work to wrap in a programatically clean way, I’m just going to stick with SMO.
All the libraries and tools I mentioned are part of :
Run your T-SQL through sqlcmd.exe.
Or, what I normally do is actually read the file contents (my sql files are normally embedded resources) and then pass it to this function to split it into batches acceptable to ADO.NET:
Here is an example usage: