sql语句中的引号 [英] quotation marks in sql statement

查看:97
本文介绍了sql语句中的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


在我的sql语句中,我必须使用带引号的字段名称和表名

marks。

那样:

-------------------------------------- ---------------------------------

string sqlStr =" Select \" ;人名字段\,\年龄字段来自\Old

Table \ " ;;

cmd.CommandText = sqlStr;

------------------------- ----------------------------------------------

但是当我将此字符串分配给OracleCommand对象的CommandText时,它会因为反斜杠而导致错误...


我怎么能在sql语句中使用没有反斜杠的引号?


我一直坚持这一点,任何帮助都将不胜感激。


谢谢,


Adam

解决方案

2007年12月5日星期三14:05:02 - 0800,Adam Right< n /a@n/a.comwrote:


在我的sql语句中,我必须使用字段名称和表名与

报价

标记。

喜欢:

--------------- -------------------------------------------------- ------

string sqlStr =" Select \Person Name Field \,\" Age Field"来自\Old

Table \ " ;;

cmd.CommandText = sqlStr;

------------------------- ----------------------------------------------

但是当我将此字符串分配给OracleCommand对象的CommandText时,它会因为反斜杠而导致错误。



什么反斜杠?如果你已经正确构造了字符串,那么实际上不存在

反斜杠。编译器处理反斜杠引用

,并且在将字符串分配给变量时,

不会留下反斜杠。

如果你得到一个特别抱怨反斜杠的错误(是吗?实际上你确实是b $ b),那么你没有构造字符串

正确的。


当然,还有一个额外的问题,你实际上并没有发布你正在使用的

代码。我怎么知道?您发布的代码甚至不会编译,因为您错过了反斜杠。既然我们知道你并不只是复制并粘贴导致问题的代码,我们也知道示例中可能缺少各种各样的东西。 ,包括实际导致问题的'b
。 :(


如何在sql语句中使用没有反斜杠的引号?



通常情况下,你会像你试图做的那样做你发布的b / b
。所以要回答你的问题,你需要做至少两件

的东西:


1)发布你正在使用的_exact_代码

2)描述你得到的_exact_错误(特别是,除非

错误专门说了一些关于反斜杠的事情,你没有

有理由相信反斜杠绝对是问题)


Pete




" Adam Right" < n /a@n/a.com在留言中写道

新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ...


嗨!


在我的sql语句中,我必须使用字段名称和表格名称

引号。

那样:

------------------------- ----------------------------------------------

string sqlStr =" Select \Person Name Field \,\" Age Field"来自\Old

Table \ " ;;

cmd.CommandText = sqlStr;

------------------------- ----------------------------------------------

但是当我将此字符串分配给OracleCommand对象的CommandText时,它会因为反斜杠而导致错误...


我怎么能在sql语句中使用没有反斜杠的引号?



其他选项是:


使用char(34),34是引号字符的ASCII代码:


char dblquote = 34;

string sqlStr =" SELECT" + dblquote +" Person Name" + dblquote + ......


使用逐字字符串文字:


string sqlStr =" SELECT"" Person名称,年龄字段,年龄字段, FROM"" Old Table"";" ;;


>

我一直坚持这个,任何帮助将不胜感激。


谢谢,

亚当




Ben Voigt [C ++ MVP]< rb *@nospam.nospamwrote:


使用逐字string literal:


string sqlStr =" SELECT"" Person Name"","" Age Field"" FROM" Old Table";" ;;



轻微纠错(我绝对肯定Ben知道如何使用

逐字字符串文字:) - 它需要@前缀:


string sqlStr =

@" SELECT"" Person Name"","" Age Field"" FROM"" Old Table"";";

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

英国的世界级.NET培训: http://iterativetraining.co.uk


Hi !

In my sql statement, i have to use field names and table name with quotation
marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object, it
gets an error ... because of backslashes.

How can i use quotation marks without backslash in a sql statement ?

I have been stuck on this, any help will be appreciated.

Thank you,

Adam

解决方案

On Wed, 05 Dec 2007 14:05:02 -0800, Adam Right <n/a@n/a.comwrote:

In my sql statement, i have to use field names and table name with
quotation
marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object,it
gets an error ... because of backslashes.

What backslashes? If you have properly constructed the string, no
backslashes would actually be present. The backslash-quoting is handled
by the compiler, and by the time the string is assigned to the variable,
no backslashes would remain.

If you get an error specifically complaining about backslashes (is it
actually true that you are?), then you didn''t construct the string
correctly.

Of course, there is the additional issue that you didn''t actually post the
code you''re using. How do I know? The code you posted won''t even
compile, since you''re missing a backslash. Since we know you didn''t just
copy-and-paste the code causing a problem, we also know that all sorts of
things could be missing from the example, including whatever it is that''s
actually causing the problem. :(

How can i use quotation marks without backslash in a sql statement ?

Normally, you would do it just as you''ve tried to do in the example you
posted. So for your question to be answered, you need to do at least two
things:

1) post the _exact_ code you are using
2) describe the _exact_ error you are getting (in particular, unless
the error specifically says something about the backslashes, you have no
reason to believe that the backslashes are definitely the problem)

Pete



"Adam Right" <n/a@n/a.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

Hi !

In my sql statement, i have to use field names and table name with
quotation marks.
like that :
-----------------------------------------------------------------------
string sqlStr = "Select \"Person Name Field\", \"Age Field" from \"Old
Table\" ";
cmd.CommandText = sqlStr;
-----------------------------------------------------------------------
But when i assign this string to CommandText of OracleCommand object, it
gets an error ... because of backslashes.

How can i use quotation marks without backslash in a sql statement ?

The other options are:

Use char(34), 34 being the ASCII code for the quote character:

char dblquote = 34;
string sqlStr = "SELECT " + dblquote + "Person Name" + dblquote + ......

Use a verbatim string literal:

string sqlStr = "SELECT ""Person Name"", ""Age Field"" FROM ""Old Table"";";

>
I have been stuck on this, any help will be appreciated.

Thank you,

Adam




Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:

Use a verbatim string literal:

string sqlStr = "SELECT ""Person Name"", ""Age Field"" FROM ""Old Table"";";

Slight typo correction (I''m absolutely sure Ben knows how to use a
verbatim string literal :) - it needs the @ prefix:

string sqlStr =
@"SELECT ""Person Name"", ""Age Field"" FROM ""Old Table"";";
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


这篇关于sql语句中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆