Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Running a T-SQL script file using C#

Running a T-SQL script file using C#

Scheduled Pinned Locked Moved C#
csharpdatabasetoolsquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wasife
    wrote on last edited by
    #1

    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.

    M C 2 Replies Last reply
    0
    • W wasife

      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.

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      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.aspx

      Thanks 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. :)

      1 Reply Last reply
      0
      • W wasife

        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.

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups