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. Transact-SQL calls from within C#

Transact-SQL calls from within C#

Scheduled Pinned Locked Moved C#
csharpdatabasesysadminquestionlearning
6 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.
  • B Offline
    B Offline
    betterc
    wrote on last edited by
    #1

    I'm learning to deliver some additional functionality to the end user I'm going to have to envoke some transact-sql statements to detach and reattach my SQL-Server (MSDE) database. My C# application uses standard ADO.NET but I can't find any examples of Transact-sql statements be called from within a C# program. Any hints or an snippet of code on how I would do this from within a C# application? Thanks, cb

    M T 2 Replies Last reply
    0
    • B betterc

      I'm learning to deliver some additional functionality to the end user I'm going to have to envoke some transact-sql statements to detach and reattach my SQL-Server (MSDE) database. My C# application uses standard ADO.NET but I can't find any examples of Transact-sql statements be called from within a C# program. Any hints or an snippet of code on how I would do this from within a C# application? Thanks, cb

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      betterc wrote: Any hints Search for SqlCommand class. You can run any query or t-sql statement with it. Mazy "A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope

      1 Reply Last reply
      0
      • B betterc

        I'm learning to deliver some additional functionality to the end user I'm going to have to envoke some transact-sql statements to detach and reattach my SQL-Server (MSDE) database. My C# application uses standard ADO.NET but I can't find any examples of Transact-sql statements be called from within a C# program. Any hints or an snippet of code on how I would do this from within a C# application? Thanks, cb

        T Offline
        T Offline
        turbochimp
        wrote on last edited by
        #3

        Something like the following should do it: static void DetachPubsDB() { //Creates a connection string connStr = "provider=SQLOLEDB;Data Source=(local);User ID=sa;Password=;Initial Catalog=master; Network Library=dbmssocn"; OleDbConnection conn = new OleDbConnection(connStr); try { conn.Open(); //Detaches the database OleDbCommand cmdDetach = new OleDbCommand("sp_detach_db", conn); cmdDetach.CommandType = CommandType.StoredProcedure; cmdDetach.Parameters.Add("dbname", "Pubs"); cmdDetach.ExecuteNonQuery(); //Attaches the database OleDbCommand cmdAttach = new OleDbCommand("sp_attach_db", conn); cmdAttach.CommandType = CommandType.StoredProcedure; cmdAttach.Parameters.Add("dbname", "Pubs"); cmdAttach.Parameters.Add("filename1", @"C:\Program Files\Microsoft SQL Server\Data\MSSQL\Data\pubs.mdf"); cmdAttach.Parameters.Add("filename2", @"C:\Program Files\Microsoft SQL Server\Data\MSSQL\Data\pubs_Log.ldf"); cmdAttach.ExecuteNonQuery(); } catch(Exception excp) { Console.WriteLine(excp.Message); } finally { if(conn.State == ConnectionState.Open) conn.Close(); conn.Dispose(); } }

        The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

        B 1 Reply Last reply
        0
        • T turbochimp

          Something like the following should do it: static void DetachPubsDB() { //Creates a connection string connStr = "provider=SQLOLEDB;Data Source=(local);User ID=sa;Password=;Initial Catalog=master; Network Library=dbmssocn"; OleDbConnection conn = new OleDbConnection(connStr); try { conn.Open(); //Detaches the database OleDbCommand cmdDetach = new OleDbCommand("sp_detach_db", conn); cmdDetach.CommandType = CommandType.StoredProcedure; cmdDetach.Parameters.Add("dbname", "Pubs"); cmdDetach.ExecuteNonQuery(); //Attaches the database OleDbCommand cmdAttach = new OleDbCommand("sp_attach_db", conn); cmdAttach.CommandType = CommandType.StoredProcedure; cmdAttach.Parameters.Add("dbname", "Pubs"); cmdAttach.Parameters.Add("filename1", @"C:\Program Files\Microsoft SQL Server\Data\MSSQL\Data\pubs.mdf"); cmdAttach.Parameters.Add("filename2", @"C:\Program Files\Microsoft SQL Server\Data\MSSQL\Data\pubs_Log.ldf"); cmdAttach.ExecuteNonQuery(); } catch(Exception excp) { Console.WriteLine(excp.Message); } finally { if(conn.State == ConnectionState.Open) conn.Close(); conn.Dispose(); } }

          The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

          B Offline
          B Offline
          betterc
          wrote on last edited by
          #4

          Thank you sooooooo much, I can hardly wait to give it a try! Why is it that finding a simple example such as the one you have furnished is so difficult? Thanks again! cb

          T 1 Reply Last reply
          0
          • B betterc

            Thank you sooooooo much, I can hardly wait to give it a try! Why is it that finding a simple example such as the one you have furnished is so difficult? Thanks again! cb

            T Offline
            T Offline
            turbochimp
            wrote on last edited by
            #5

            Actually, it's not so bad. MSDN has lots of good articles on various facets of ADO.Net and if all else fails, there's Google. I would strongly recommend visiting Microsoft's ADO.Net Primer[^] if you plan to work with ADO.Net a lot. Most of the challenge that I've seen people suffer through with ADO.Net stems from their previous history with ADO 2.x (COM-based). The two ADO implementations share some terminology and concepts, but are largely different. Many experienced ADO 2.x developers find ADO.Net syntax more cumbersome than ADO 2.x and I suppose I agree to some extent. A very good place to start is by examining the ADO.Net object hierarchy. Take the time to read about each class (there really aren't that many, and there are only a handful that get used in most common database transactions). CodeProject is a great place to get information, but you have to have a mindset that you're prepared to explore and experiment - experience is the best teacher.

            The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

            B 1 Reply Last reply
            0
            • T turbochimp

              Actually, it's not so bad. MSDN has lots of good articles on various facets of ADO.Net and if all else fails, there's Google. I would strongly recommend visiting Microsoft's ADO.Net Primer[^] if you plan to work with ADO.Net a lot. Most of the challenge that I've seen people suffer through with ADO.Net stems from their previous history with ADO 2.x (COM-based). The two ADO implementations share some terminology and concepts, but are largely different. Many experienced ADO 2.x developers find ADO.Net syntax more cumbersome than ADO 2.x and I suppose I agree to some extent. A very good place to start is by examining the ADO.Net object hierarchy. Take the time to read about each class (there really aren't that many, and there are only a handful that get used in most common database transactions). CodeProject is a great place to get information, but you have to have a mindset that you're prepared to explore and experiment - experience is the best teacher.

              The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

              B Offline
              B Offline
              betterc
              wrote on last edited by
              #6

              Agrreed and thank you. I have bought several ADO.NET books and am picking it up really quickly. I was doing pretty good until I needed to write an application using an MSDE DB on a distributable CD. In one sense since my application will be just referencing (reading) the DB my work is easy. The hardwork is in developing a 'setup' routine that installs MDAC, .NET, MSDE, my application and reference the data on the CD. This kind of through me into using transact-sql, something I've never needed to do before. I found plenty of examples of transact-sql statements and I've written qutie a few ExecuteNonQuery() routines but I couldn't find an example that put it all together. What you pointed out was that my commandtype = storedprocedure which I didn't think I needed. So with your help and my recently purchased "The Guru's Guide to Transact-SQL" I'll get to experimenting. Thanks again, cb

              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