Array of SQLParameter

  • Thread starter kagitas via .NET 247
  • Start date
K

kagitas via .NET 247

(Type your message here)
Code taken from MSDN:

public void CreateMySqlCommand(SqlConnection myConnection,
string mySelectQuery, SqlParameter[] myParamArray) {
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers WHERE Country = @Country AND City = @City";
myCommand.Parameters.Add(myParamArray);
for (int j=0; j<myParamArray.Length; j++)
{
myCommand.Parameters.Add(myParamArray[j]) ;
}
string myMessage = "";
for (int i = 0; i < myCommand.Parameters.Count; i++)
{
myMessage += myCommand.Parameters.ToString() + "\n";
}
MessageBox.Show(myMessage);
}

In this query "SELECT CustomerID, CompanyName FROM Customers WHERE Country = @Country AND City = @City";
how do we know which parameter of the parameter array is passed to the query.
For Eg: what would be the value of @Country as we are not using any indexer.

Thanks in adavnce,
Jaya.
 
W

W.G. Ryan [eMVP]

The first thing to do is look at the values in the params and see how they
are coming in. You are using named parameters so their position doesn't
matter as long as the spelling is correct. However this lacks some
elegance. I'd use a collection (or a hashtable) so i could load those
values as param names and then directly find their values.

HTH,

Bill

www.knowdotnet.com
kagitas via .NET 247 said:
(Type your message here)
Code taken from MSDN:

public void CreateMySqlCommand(SqlConnection myConnection,
string mySelectQuery, SqlParameter[] myParamArray) {
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers
WHERE Country = @Country AND City = @City";
myCommand.Parameters.Add(myParamArray);
for (int j=0; j<myParamArray.Length; j++)
{
myCommand.Parameters.Add(myParamArray[j]) ;
}
string myMessage = "";
for (int i = 0; i < myCommand.Parameters.Count; i++)
{
myMessage += myCommand.Parameters.ToString() + "\n";
}
MessageBox.Show(myMessage);
}

In this query "SELECT CustomerID, CompanyName FROM Customers WHERE Country = @Country AND City = @City";
how do we know which parameter of the parameter array is passed to the query.
For Eg: what would be the value of @Country as we are not using any indexer.

Thanks in adavnce,
Jaya.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top