Solved

SQL Like Statement C#

Posted on 2012-12-26
4
425 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
ID: 38721991
That should work, remove the comma after PName.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 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