Running a T-SQL script file using C#
-
Hi, I have a T-SQL Script file which i want to run without opening it and reading its contents. Please tell me how can i do it using ADO.Net or any other way. Regards, Wasif Ehsan.
try the following code from this post. http://blog.miseldine.com/?p=24
private static void ExecuteSql(SqlConnection ___conn, string ___fileName) { //array for storage of SQL string[] __sqlStatements; //the only clever bit: spilt at each GO statement //as the normal SQL connector will spit out anything //after the GO. Regex __r = new Regex(“^GO”, RegexOptions.Multiline); //for file reading: just replace with the string of //SQL if not from file. Or embed SQL as resource… StreamReader __sr = new StreamReader(___fileName); string __sql = __sr.ReadToEnd(); __sqlStatements = __r.Split(__sql); SqlCommand __comm = new SqlCommand(); __comm.Connection = ___conn; //loop through each block in the spilt SQL and //execute. foreach (string __sqlBlock in __sqlStatements) { if (__sqlBlock.Trim().Length > 0) { __comm.CommandText = __sqlBlock; try { __comm.ExecuteNonQuery(); } catch (SqlException) { //here, execute your DROP script break; } } } }
thks to original author. here is another one... http://msdn2.microsoft.com/en-us/library/ms811086.aspxThanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)
-
Hi, I have a T-SQL Script file which i want to run without opening it and reading its contents. Please tell me how can i do it using ADO.Net or any other way. Regards, Wasif Ehsan.
wasife wrote:
I have a T-SQL Script file which i want to run without opening it and reading its contents. Please tell me how can i do it using ADO.Net or any other way.
If you want to use ADO.NET you need to open it and read its contents. Then pass those contents onto to a SqlCommand. If you want to use any other process then that will have to open and read the contents too. There is no way around that. Why do you have an aversion to opening and reading the file?
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website