Nick
Nick

Reputation: 1891

Hyphen in a sql select statement

This is my very simple select statement:

select * from ProductDimensions where Item='100-1000'

When entered into SSMS it queries fine and returns the results. When run through my program in VS it returns nothing. An item without a hyphen returns fine. Is there some special syntax required for this to work from my C# code?

The C# code:

adapter = new SqlDataAdapter("select * from ProductDimensions where Item='100-1000'", conn);
adapter.Fill(dataSet);

Like I said if I put in a number with no hyphen, such as '1000' it works fine.

Upvotes: 2

Views: 2088

Answers (1)

Iain Ward
Iain Ward

Reputation: 9946

Try using a parameterised SQL string instead and you'll avoid issues like this, as well as others like SQL injection:

adapter = new SqlDataAdapter("select * from ProductDimensions where Item=@Item";
adapter.Parameters.Add("@Item", SqlDbType.NChar, 15, "100-1000");

(You may have to adjust the SqlDbType and length)

Upvotes: 5

Related Questions