Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

Sponsored Links

Discuss "Parameterized Dynamic Query" in the .Net Development forum.

.Net Development - Learn about the Microsoft.Net framework and how to create powerful web-based (ASP.net) and client-based (Windows Forms) applications utilizing various languages such as C#, VB.Net, J# and others.


Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old June 6th, 2008, 09:47 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default 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
Reply With Quote
Sponsored Links
  #2  
Old June 6th, 2008, 09:57 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 949
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 6
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

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 with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #3  
Old June 6th, 2008, 10:09 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

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
Reply With Quote
  #4  
Old June 6th, 2008, 10:12 AM
Shem's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 263
Thanks: 31
Thanked 5 Times in 5 Posts
Rep Power: 1
Shem is on a distinguished road
Default

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
Reply With Quote
  #5  
Old June 6th, 2008, 10:20 AM
Wolffy's Avatar
Slaprentice of Wolves
 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 176
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

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()

Comments on this post
lewy agrees:
Shem agrees: Thanks for the example
__________________
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. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.
Reply With Quote
The Following 2 Users Say Thank You to Wolffy For This Useful Post:
jmurrayhead (June 6th, 2008), Shem (June 6th, 2008)
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Query Counts Jaykappy Microsoft Access 13 May 14th, 2008 01:39 PM
SQL VBA Query Jaykappy Microsoft Access 12 May 8th, 2008 09:30 AM
Using Dropdown List Value in Database SQL Query richyrich ASP Code Samples 0 April 2nd, 2008 10:05 AM


All times are GMT -4. The time now is 03:59 PM.



Content Relevant URLs by vBSEO 3.2.0