Extract parameters from SQL statement

  • Thread starter Thread starter Phil
  • Start date Start date
P

Phil

Hei,
I'm currently trying to write a program in C# that will allow users to
parametrize their queries.
For instance, I have a query like this:

SELECT * FROM Customers Where Region = @Region AND Gender > @Gender

How can I extract the Parameters names without using String
manipulation (which is not perfect since sql statements can also
contain '@' in LIKE clauses for example)

I tried this way
Dim comm As New OleDbCommand(SqlStatement, myCon)
comm.Prepare()
MsgBox(comm.Parameters.Count.ToString())
comm.Dispose()
myCon.Close()
but this always return me 0 (zero)

Do you have any idea on how to solve this generic problem?
Regars
Philippe Graca
 
public void CreateMyOleDbCommand(OleDbConnection myConnection,
string mySelectQuery, OleDbParameter[] myParamArray) {
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
myCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND 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);
}

Good luck,
chanmm
 
Hi chanmm,
the issue I have is that I don't know in advance the parameters list....
any thoughts?
Regards
Phil
 
From the way I look at your code. You may try:

comm.Parameters.Add(myParamArray);

Good luck,
chanmm
 
Back
Top