Hi all,
Simply i am unable to get parameters with SqlDataAdapter's GetFillParameters() method. Here is my code sample :
private
void button1_Click(object sender, System.EventArgs e){
SqlConnection s =
new SqlConnection(@"Integrated Security=SSPI;Data Source=(localhost);Initial Catalog=Northwind");SqlDataAdapter cmd =
new SqlDataAdapter("SELECT * FROM Customers WHERE City = @City", s); try{
s.Open();
MessageBox.Show(cmd.GetFillParameters().Length.ToString());
}
catch(SqlException ex){
MessageBox.Show(ex.Message);
}
When i run this code sample i get "0" in a message box but no errors. Why can't i get the parameters in my sql code
Any comments are welcome.
Polat PINAR

GetFillParameters Problem
Steven D
If you want to find the parameters in an ad-hoc query then you'll have to parse the text out by hand looking for @ AFAIK. There is no available functionality to parse arbitrary SQL blocks looking for parameters. Even if you do get the parameter names you won't get any type information.
Michael Taylor - 10/30/06
imshally81
Thanks for suggestions but I am sorry i could not be clear.
What i am trying to do is to get the parameters declared in a T-SQL statement just like query builder does.
I don't want to use cmd.Parameters.Add("@City", SqlDbType.Char, 2).Value = "NY";
Polat PINAR
Kirill Tropin
Try something like this:
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Data Source=(localhost);Initial Catalog=Northwind");
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE City = @City", conn);
cmd.Parameters.Add("@City", SqlDbType.Char, 2).Value = "NY";
try
{
conn.Open();
DataTable custTable = new DataTable();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(custTable);
custTable.TableName = "Customers";
gridCustomers.DataSource = custTable;
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
Saravanan.Chinnusamy
GetFillParameters is used to get the parameters associated with a command. You are passing raw SQL so there are no parameters. Instead you'll need to create and use a SqlCommand object. You can then get the parameters to be used.
Michael Taylor - 10/30/06
Curt Zarger
Hi Michael,
I am afraid i could not understand what you mean. Because SqlDataAdapter's SelectCommand object is an SqlCommand object. So i think GetFillParameters() method should get the fill parameters in my sql code.
Thanks
Polat PINAR