?
Solved

C# code with a SQL string is converting single quotes to 2 single quotes.

Posted on 2014-07-22
2
Medium Priority
?
406 Views
Last Modified: 2014-07-23
I have a bit of code as below, and for some strange reason the actual code issued to SQL server has all of the single quotes replaced with 2x single quotes. Not by my code though. Any thoughts anyone ?
C# code
string sql = @"SELECT TOP 1 isnull(rtrim(JCI.joco_FirstName), '''' ) WHERE Case_CaseId = @CaseId;";
     // execute
     using (SqlDataAdapter da = new SqlDataAdapter(sql.ToString(), connection))
     {
         da.SelectCommand.Parameters.AddWithValue("@CaseId", JobId);
         da.Fill(dtDetail);
     }


Actual SQL issued (via SQL Profiler trace)
exec sp_executesql N'SELECT TOP 1 isnull(rtrim(JCI.joco_FirstName), '''''''' ) WHERE Case_CaseId = @CaseId;',N'@CaseId int',@CaseId=2237361

Open in new window

0
Comment
Question by:Phil Catterall
[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
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 40213622
SQL Server is escaping them because the parameter to sp_executesql is itself wrapped in single quotes. You don't need to double up the quotes in your query, the following should work fine:
string sql = @"SELECT TOP 1 isnull(rtrim(JCI.joco_FirstName), '' ) WHERE Case_CaseId = @CaseId;";

Open in new window

0
 

Author Closing Comment

by:Phil Catterall
ID: 40213944
Perfect. Simples !!
Thanks
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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

752 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