Solved

SQL Like Statement C#

Posted on 2012-12-26
4
423 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
4 Comments
 
LVL 9

Expert Comment

by:dustock
Comment Utility
That should work, remove the comma after PName.
0
 
LVL 142

Accepted Solution

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

Expert Comment

by:jonnidip
Comment Utility
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
Comment Utility
Thanks everyone for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now