Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to send parameters to a query

Posted on 2004-04-03
4
Medium Priority
?
388 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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