简体   繁体   中英

How to specify a wildcard in a SQL Server Table Adapter query

I am trying to figure out how to specify a wildcard in a SQL server table adapter query. I have already been here and here , and searched elsewhere on the Microsoft tech sites, and cannot find an appropriate example.

I have a table adapter query that is supposed to return a single value (a particular query choice in the table adapter query wizard) based on a WHERE clause with two parameters. Here is the query out of the table adapter, which works just fine testing the query in the Query Builder and using SQL Management Studio writing a query for the particular database table.

SELECT     owner_name_1
FROM         BuildingPermit.dbo.real_estate
WHERE     (prop_addr_no like ?) AND (prop_addr_str = ?)

To make a fine point, the query above is in the adapter. In my example 11% and ACADEMY ST are being passed to the query as parameters.

Basically, if the WHERE parameters are 11-11A and ACADEMY ST , I get back a non-null value when testing, but not in my C# program.

Here is a C# handler, which does not work.

private void owner_cb_DropDown(object sender, EventArgs e)
{
  string str_no = street_number_cb.Text.Trim().ToUpper() + "%"; 
  string str_name = street_name_cb.Text.Trim().ToUpper();

  string owner_name_1 = (string )real_estateTableAdapter1.owner_name_1_q(str_no,            str_name));
  owner_cb.Text = owner_name_1;
}

Update:

I have tried a full row query with the same results.

DataTable re_tbl = real_estateTableAdapter1.get_re_rec_by_str_num_name_q(search_street_number, street_name_cb.Text.ToString().ToUpper());

// Break after one loop. Only one answer expected.                
foreach (DataRow dr in re_tbl.Rows)
{
    string temp_val = (string)dr["owner_name_1"];
    owner_cb.Text = temp_val;
    break;

}

I get back a null.

I have also tried bracketing the parameter containing 11% (from my example) with sinqle quotes, and that does not work either.

And I have tried this, and the QueryBuilder won't parse it.

SELECT     owner_name_1
FROM         BuildingPermit.dbo.real_estate
WHERE     (prop_addr_no LIKE @p1) AND (prop_addr_str = @p2)

This is what I did, and it worked, I hope it's useful to you.

1.- In Dataset designer right click table adapter, add query

在此处输入图片说明

2.- Use SQL Satatements

3.- Select which returns a single value

4.- Open Query builder, It looks like this:

在此处输入图片说明

5.- And in my test I did get a value: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM