C# - App - SQL parametes questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxx233
    New Member
    • Nov 2007
    • 32

    C# - App - SQL parametes questions

    Hello,
    After doing a couple small projects where I concatenated my SQL queries, I decided I needed to finally bite the bullet and do this the right way with parameters.

    What I have so far is code that can insert to the DB, and gets the ID returned of the row that was just inserted. That DBID gets stored for later use by another part of my program.

    So my question is, if I want to do a "SELECT someColumn FROM someTable WHERE DBID=theOneFrom Earlier", is it correct to somehow do a SQL parameter on the DBID I'm passing, or at this point is it OK to simply concatenate that variable into my SQL statement?

    If passing DBID as a parameter is still correct, how is that done when my end result is reading data? I know I could do it the same way I returned the DBID from my INSERT statement (Setting up a new SqlParameter, setting it's direction to output, etc) but this seems like I *lot* of overhead if I were setting it up for 15 fields I needed returned?

    If it's standard practice to concatenate at this point, I can do that fine and get myself a DataReader as I was doing before with no problems. Any advice is much appreciated!

    Maxx
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    You can select the max id in a query after you run the insert statement. I assume you execute your queries in try catch block. HTH.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      You can use the DBID as a parameter in your 2nd query if you want, but instead of using .ExecuteNonQuer y(), you would probably want to use an SqlDataAdapter and have it .Fill() a DataTable for you.

      Comment

      • krille
        New Member
        • Feb 2008
        • 26

        #4
        You can use concat for SQL if u make it safe from sql-injections wich isnt that hard... I usally just make an static method in my DB class.

        all you need to do is put ' around string and remove ' inside of string so this will work out:

        " ' " + strInput.Replac e(" ' ","") + " ' " (dont use white space in replace just did that now so you could read it)

        Cheers
        Krille

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If you wanted to keep the ' in data (sometimes it's important) you would escape it with '' (that is two single quotes, not a double quote)

          Comment

          • maxx233
            New Member
            • Nov 2007
            • 32

            #6
            For Google sake, here was the final solution I decided works for me:

            SqlConnection myConnection = new SqlConnection(m yConnectionStri ng);
            SqlCommand myCommand = new SqlCommand();
            myConnection.Op en();
            myCommand.Conne ction = myConnection;
            myCommand.Comma ndText =
            "SELECT vendorName,vend orContact FROM venderList WHERE ID=@DBID;";
            myCommand.Param eters.Add("@DBI D", SqlDbType.Int). Value = myDBID;
            SqlDataReader myReader = myCommand.Execu teReader();
            myReader.Read() ;

            Hope that helps anyone else looking for this sort of thing. If anyone else has anything to add/correct, feel free. Thanks for the help everyone!

            Maxx

            Comment

            Working...