Solved

how to send parameters to a query

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 47
designing in object programming 12 79
How to parse the string and split the sub directory names in asp.net 7 43
.NET 2008 VB and C# 6 28
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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 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