Reputation: 9225
code:
string qr = @"select 'TheCode','CodeDesc' from [dbo].[Table1] as \""Table1\"" order by 'CodeDesc'";
using (SqlConnection conn = new SqlConnection(connstring))
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(qr, conn);
SqlDataReader sdr = cmd.ExecuteReader();
}
catch (Exception ex) // Incorrect syntax near '\'.
{
string error = ex.Message;
}
finally
{
conn.Close();
}
}
I keep getting an error in this line: cmd.ExecuteReader();
Error:
Incorrect syntax near '\'.
How can I resolve the error.
Upvotes: 0
Views: 51
Reputation: 6696
You added @ at the beginning of the command string so it will be somthing like that :
select distinct 'TheCode','CodeDesc' from [dbo].[Table1] as \""Table1\""
order by 'CodeDesc'";
So remove \""
and \""
from the command string. You also wrapped the column names by '
that should be removed
string strQryDDL = @"select distinct TheCode,CodeDesc from [dbo].[Table1] as t
order by CodeDesc";
You did not use the alias so you can remove it,
string strQryDDL = @"select distinct TheCode,CodeDesc from [dbo].[Table1]
order by CodeDesc";
Upvotes: 2
Reputation: 11514
When using the "@" symbol in c# strings, you use don't escape quotes with a slash, you use two in a row like: "" Your other problem is the single quotes, remove them all, you do not surround field names with quotes, just literal text.
Upvotes: 1