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. SQL in C# question

SQL in C# question

Scheduled Pinned Locked Moved C#
databasehelpquestioncsharpalgorithms
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.
  • P Offline
    P Offline
    puppiesLover
    wrote on last edited by
    #1

    I'm doing some database in C# using MS SQL. Problem #1: Everytime i do a connection to my database, i have to copy and paste the code over and over. I heard of a way to put these code in the configuration file, but i don't know how to. Problem #2: I also heard of someway(interface, object) allowing me to do SQL query indirectly instead of passing the query string directly into the SqlCommand object. And, again, I don't know how to. Problem #3: Is there anyway i can pass in my sql query dynamically(ie, as a varible)? I just learn C# on my own, and having problem searching for things like this Please help me out! THANKS A BUNCH!!!

    A D 2 Replies Last reply
    0
    • P puppiesLover

      I'm doing some database in C# using MS SQL. Problem #1: Everytime i do a connection to my database, i have to copy and paste the code over and over. I heard of a way to put these code in the configuration file, but i don't know how to. Problem #2: I also heard of someway(interface, object) allowing me to do SQL query indirectly instead of passing the query string directly into the SqlCommand object. And, again, I don't know how to. Problem #3: Is there anyway i can pass in my sql query dynamically(ie, as a varible)? I just learn C# on my own, and having problem searching for things like this Please help me out! THANKS A BUNCH!!!

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      You should only need one IDbConnection instance to one database, and you can just reuse it every time (just open and close it). I am not sure what you mean by indirect query; however I think its good practice to use the interfaces rather than the specific providers as much as possible, as your provider might change. (i.e. IDbCommand cmd = new OldDbCommand("S...). As for the queries, I keep mine as strings, not as string literals, and have them in a config file. That way I don't need to recompile every time i change a query. It also makes it easy to add on constraints on the query ( cmd.CommandText = selectString + "WHERE Cust = " + custId.ToString()).

      1 Reply Last reply
      0
      • P puppiesLover

        I'm doing some database in C# using MS SQL. Problem #1: Everytime i do a connection to my database, i have to copy and paste the code over and over. I heard of a way to put these code in the configuration file, but i don't know how to. Problem #2: I also heard of someway(interface, object) allowing me to do SQL query indirectly instead of passing the query string directly into the SqlCommand object. And, again, I don't know how to. Problem #3: Is there anyway i can pass in my sql query dynamically(ie, as a varible)? I just learn C# on my own, and having problem searching for things like this Please help me out! THANKS A BUNCH!!!

        D Offline
        D Offline
        Danny Rodriguez
        wrote on last edited by
        #3

        FYI, Problem #1 Where are you connecting from? Are you connecting from a web service? Windows Form? Asp.net? Problem #2 Problem #3 Read on on the .NET Documentation. Specifically with the SqlCommandBuilder.. It will automatically generate the SQL queries you require.. Its not as fast, to my understanding, as your own - danny

        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