?
Solved

SQL Like Statement C#

Posted on 2012-12-26
4
Medium Priority
?
444 Views
Last Modified: 2012-12-26
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
Comment
Question by:RecipeDan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 38721991
That should work, remove the comma after PName.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38722056
this will work better:
adapter = new SqlDataAdapter("SELECT PName, FROM MID WHERE PName LIKE '%' + @PName + '%'",
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 38722118
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
 
LVL 1

Author Comment

by:RecipeDan
ID: 38722249
Thanks everyone for your help.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question