DeveloperBarn Forums

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

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 « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old June 6th, 2008, 09:47 AM
Shem's Avatar
Contributing Member

 
Join Date: Mar 2008
Posts: 186
Thanks: 23
Thanked 4 Times in 4 Posts
Rep Power: 1
Shem is an unknown quantity at this point
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 (permalink)  
Old June 6th, 2008, 09:57 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server 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
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
Reply With Quote
  #3 (permalink)  
Old June 6th, 2008, 10:09 AM
Shem's Avatar
Contributing Member

 
Join Date: Mar 2008
Posts: 186
Thanks: 23
Thanked 4 Times in 4 Posts
Rep Power: 1
Shem is an unknown quantity at this point
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 (permalink)  
Old June 6th, 2008, 10:12 AM
Shem's Avatar
Contributing Member

 
Join Date: Mar 2008
Posts: 186
Thanks: 23
Thanked 4 Times in 4 Posts
Rep Power: 1
Shem is an unknown quantity at this point
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 (permalink)  
Old June 6th, 2008, 10:20 AM
Wolffy's Avatar
Slaprentice of Wolves


 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 146
Thanks: 1
Thanked 23 Times in 20 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 Classic ASP 0 April 2nd, 2008 10:05 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 10:34 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46