• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

SQL Like Statement C#

Hello:

How do I use the SQL Like Statement in C#

adapter = new SqlDataAdapter("SELECT PName, FROM MID WHERE PName LIKE '%@PName%'", conn);
adapter.SelectCommand.Parameters.Add("@PName", System.Data.SqlDbType.NVarChar);
adapter.SelectCommand.Parameters["@PName"].Value = "Sm";

Open in new window

0
RecipeDan
Asked:
RecipeDan
1 Solution
 
dustockCommented:
That should work, remove the comma after PName.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this will work better:
adapter = new SqlDataAdapter("SELECT PName, FROM MID WHERE PName LIKE '%' + @PName + '%'",
0
 
jonnidipCommented:
As suggested before, remove the comma after PName.
I would also suggest you to specify the length of your variable.
You can also set the value of your variable in the declaration line:

adapter = new SqlDataAdapter("SELECT PName FROM MID WHERE PName LIKE '%@PName%'", conn);
adapter.SelectCommand.Parameters.Add("@PName", System.Data.SqlDbType.NVarChar, 128) { value = "Sm" };

Open in new window


Then, it is just a way of work, I always try to avoid writing inline statement, but rather use Stored Procedures.
SP are easier to manage, from both the developer's and the dba's side. Also your SQL Server will thank you, because SP execution plans are likely to be cached, and inline statements not.

Regards.
0
 
RecipeDanAuthor Commented:
Thanks everyone for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now