Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

how to send parameters to a query

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
mhdhallak
Asked:
mhdhallak
  • 2
  • 2
1 Solution
 
JoeisanerdCommented:
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
 
programmer_bmbCommented:
thank you so much for your fast response i'll check it out and get back to you
0
 
JoeisanerdCommented:
you have two accounts in experts-exchange?
0
 
programmer_bmbCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now