parameterized queries

Posted on 2006-06-08
Last Modified: 2010-04-16
Is it ok to set the parameters of an SqlCommand before they appear in the command text?  ie,

myCommand.Parameters.add("@value", myInt);
myCommand.CommandText = "SELECT * FROM table WHERE val=@value";
somereader = myComand.ExecuteReader();

It's a long story why I'm asking....would this throw an exception?
Question by:ARACK04
    LVL 5

    Expert Comment

    Yes it is ok
    LVL 5

    Accepted Solution

    Yes, because the actual command text is not considered until it is executed.  (i.e. myComand.ExecuteReader();

    so it doesn't matter what order, as long as they are both before any processing

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now