Solved

how to send parameters to a query

Posted on 2004-04-03
4
342 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

21 Experts available now in Live!

Get 1:1 Help Now