Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to send parameters to a query

Posted on 2004-04-03
4
359 Views
Last Modified: 2010-04-15
i've read the sql server 2000 documentation and it says that '?' stands for a query parameter in select statement.. but when the select is sent from a sqlcommmand object it returns execution error. could anyone show me a sample of how to execute a parameterized query (parameter set to the value of some variable) in C# code..
0
Comment
Question by:mhdhallak
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 50 total points
ID: 10747920
When you use Sql Server 2000 you specify parameter names in the query or if you use the ? then when you add parameters to the sqlcommand it will replace the ? in the order in which they appear.

Example: Assumes Nortwind Database and that sqlConnection1 has already been initialized

sqlConnection1.Open();
SqlCommand cmd = new SqlCommand("select * from Categories where CategoryID=@CatId",sqlConnection1);

// Create a new parameter with the name of the parameter and in this case the value
SqlParameter par = new SqlParameter("@CatId",2);

cmd.Parameters.Add(par);

SqlDataReader dr = cmd.ExecuteReader();

while(dr.Read())
{
       MessageBox.Show(dr[0].ToString());
}
sqlConnection1.Close();
0
 

Expert Comment

by:programmer_bmb
ID: 10747941
thank you so much for your fast response i'll check it out and get back to you
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10747946
you have two accounts in experts-exchange?
0
 

Expert Comment

by:programmer_bmb
ID: 10747952
no that's my friend's account he's beside me and he's checking his questions and i forgot to log out and log in again with my account... :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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