+ Reply to Thread
Results 1 to 5 of 5

Thread: Parameterized Dynamic Query

  1. #1
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    305
    Rep Power
    4

    Parameterized Dynamic Query

    hi, i need to convert this example as it uses a stored procedure and i won't.

    Code:
     Dim myCommand = New MySqlCommand(QueryProjectsDB.Save(myProjects.Idkey), myConnection)
                    myCommand.CommandType = CommandType.StoredProcedure
                    myCommand.Parameters.AddWithValue("@id", myProjects.Idkey)
                    myCommand.Parameters.AddWithValue("@street", myProjects.TheDate)
                    myCommand.Parameters.AddWithValue("@houseNumber", myProjects.Name)
                    myCommand.Parameters.AddWithValue("@zipCode", myProjects.ProjectNumber)
                    myCommand.Parameters.AddWithValue("@city", myProjects.Description)
                    Dim returnValue As DbParameter
                    returnValue = myCommand.CreateParameter
                    returnValue.Direction = ParameterDirection.ReturnValue
                    myCommand.Parameters.Add(returnValue)
                    myConnection.Open()
                    myCommand.ExecuteNonQuery()
                    result = Convert.ToInt32(returnValue.Value)
                    myConnection.Close()
    
    Thanks
    Shem

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Hmmm...something about this doesn't quite look right. Why are you calling your DAL in your MySQLCommand?

    And why won't you use a Stored Procedure?

    Anyway, change CommandType.StoredProcedure to CommandType.CommandText, create a dynamic SQL string, but replace the variables in the string (i.e. @id) to a question mark.

    Code:
    Dim sql As String
    sql = "INSERT INTO yourtable (id, street, housenumber, ...) VALUES(?,?,?, ...)"
    Dim myCommand = New MySqlCommand(sql, myConnection)
                    myCommand.CommandType = CommandType.CommandText                
                    myCommand.Parameters.AddWithValue("@id", myProjects.Idkey)
                    myCommand.Parameters.AddWithValue("@street", myProjects.TheDate)
                    myCommand.Parameters.AddWithValue("@houseNumber", myProjects.Name)
                    myCommand.Parameters.AddWithValue("@zipCode", myProjects.ProjectNumber)
                    myCommand.Parameters.AddWithValue("@city", myProjects.Description)
                    Dim returnValue As DbParameter
                    returnValue = myCommand.CreateParameter
                    returnValue.Direction = ParameterDirection.ReturnValue
                    myCommand.Parameters.Add(returnValue)
                    myConnection.Open()
                    myCommand.ExecuteNonQuery()
                    result = Convert.ToInt32(returnValue.Value)
                    myConnection.Close()
    
    If you're calling your DAL like that, then it's completely wrong. In fact, all of this code above, should BE IN your DAL.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    305
    Rep Power
    4

    In My DAL i have a class that holds my queries, i did this for the fact that
    if i need to call data with diff filters, i simply just call the right query, and don't have to rebuild the function that talks to the db and gets the results.

    But as you said, why not use a stored procedure, well it's easier for me or some
    other developer to quickly write a query and save it in the query class and use it in where he needs it and then doesn't need to re-write anything else.

    Data collection will always be the same from a specific table, but the filtering
    that data can always change, get my drift.

    Shem

  4. #4
    Barn Enthusiast Shem is on a distinguished road Shem's Avatar
    Join Date
    Mar 2008
    Posts
    305
    Rep Power
    4

    Quote Originally Posted by jmurrayhead View Post
    If you're calling your DAL like that, then it's completely wrong. In fact, all of this code above, should BE IN your DAL.
    IT is dude

  5. #5
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    JMH got in here while I was typing out an example, but I'll post it anyway:
    Code:
    Dim sqlStr as String = "INSERT INTO someTable " + _
             "Values(@id, @street, @houseNumber, @zipCode, @city)"
    
    Dim myCommand = New MySqlCommand(sqlStr, myConnection)
     
    myCommand.Parameters.AddWithValue("@id", myProjects.Idkey)
    myCommand.Parameters.AddWithValue("@street", myProjects.TheDate)
    myCommand.Parameters.AddWithValue("@houseNumber", myProjects.Name)
    myCommand.Parameters.AddWithValue("@zipCode", myProjects.ProjectNumber)
    myCommand.Parameters.AddWithValue("@city", myProjects.Description)
    
    Dim rowsAffected as Int32
    
    myConnection.Open()
    rowsAffected = smyCommand.ExecuteNonQuery()
    myConnection.Close()
    
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. Query Counts
    By Jaykappy in forum Microsoft Access
    Replies: 13
    Last Post: May 14th, 2008, 02:39 PM
  2. SQL VBA Query
    By Jaykappy in forum Microsoft Access
    Replies: 12
    Last Post: May 8th, 2008, 10:30 AM
  3. Using Dropdown List Value in Database SQL Query
    By richyrich in forum ASP Code Samples
    Replies: 0
    Last Post: April 2nd, 2008, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO