似乎无法用双引号保存字符串 [英] Cannot seem to save a string with double quotes

查看:98
本文介绍了似乎无法用双引号保存字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要用表示

目录路径的字符串更新数据库表(MySQL 4.0)(C:\ Temp \\ \\)但是不能让C#这样做。


我从文本框中获取字符串值(myStr = myText.Text)并将

传递给一个函数。


该函数构建一个将执行的SQL字符串:


SQLString = String.Format(" UPDATE BSConfig" ; +

" SET BSConfig_Inbound = \" {0} \",myStr);


这会将引号括在文本旁边,但重新调整斜线。我用@字符试了

(不知道你叫什么lol)和\"但是

给出了一个错误。


SQLString = String.Format(@" UPDATE BSConfig" +

" SET BSConfig_Inbound = \" {0} \",myStr); //不允许


任何人帮助我?


问候,


Steven


***通过开发人员指南 http://www.developersdex发送。 com ***

不要只是参加USENET ......获得奖励!

解决方案

嗨史蒂文,


如果我理解正确你想要\和出现在

字符串中吗?


发生的事情是\是转义字符意思是下一个

字符不应该被解释为特殊的字符。

那么什么是\转换为并且\被淘汰,因为它被用作

仅作为逃避

现在如果你想让\出现你只是简单地逸出"它也是:)

\\会做的窍门


干杯,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


Steven Blair < ST ********** @ btinternet.com>在消息中写道

news:uj ************* @ TK2MSFTNGP11.phx.gbl ...



我需要使用表示目录路径(C:\Temp \ In)的字符串更新数据库表(MySQL 4.0),但不能使用C#来执行此操作。

该函数构建一个将执行的SQL字符串:

SQLString = String.Format(" UPDATE BSConfig" +
" SET BSConfig_Inbound = \" {0} \",myStr);
这会将引号括在文本中,但会删除斜杠。我尝试使用@字符(不确定你称之为lol)和\但
给出了错误。

SQLString = String.Format(@" UPDATE BSConfig" +
" SET BSConfig_Inbound = \" {0} \ ",myStr中); //不允许

任何人帮助我?

问候,

史蒂文

***通过Developersdex发送 http://www.developersdex.com ***
不要刚参加USENET ...获得奖励!



如果你想要斜杠和引号,请写下\\\"对于每一个。


因此


" \\\\\ quot; this \\\\\ quot;"


是一个包含字符的字符串:


\" this \"


Steven Blair< st ********** @ btinternet.com>写道:

我需要使用表示目录路径(C:\Temp \ In)的字符串更新数据库表(MySQL 4.0),但无法通过C#来执行此操作。

我从文本框中获取字符串值(myStr = myText.Text)并将其传递给函数。

该函数构建一个SQL字符串将被执行:

SQLString = String.Format(" UPDATE BSConfig" +
" SET BSConfig_Inbound = \" {0} \",myStr);




开始时这是一个坏主意。


使用参数代替 - 它会让它变得多*更容易处理,因为你只需要担心获取正确的数据,没有

转义成字符串,然后将其设置为参数值。


-

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

如果回复该团体,请不要给我发邮件


Hi,

I need to update a DB Table (MySQL 4.0) with a string representing a
directory path (C:\Temp\In) but cannot get C# to do this.

I take the string value from a textbox (myStr = myText.Text) and pass
this to a function.

The function builds up a SQL string which will be executed:

SQLString = String.Format( "UPDATE BSConfig " +
"SET BSConfig_Inbound = \"{0}\",myStr);

This puts the quotes round the text, but remvoes the slashes. I tried
using the @ character (not sure what you call this lol) and the \" but
was given an error.

SQLString = String.Format( @"UPDATE BSConfig " +
"SET BSConfig_Inbound = \"{0}\",myStr); //Not allowed

Anyone help me out ?

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

解决方案

Hi Steven,

If I understand correctly you want both the \ and the " to appear in the
string right?

What is happening is that \ is the "escape character" meaning that the next
character should not be interpreted as a especial char.
So what \" gets converted in " and the \ is eliminated, cause it''s used
only as a "escape"
Now if you want the \ to appear you just simply "escape" it too :)
\\ will do the trick

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Steven Blair" <st**********@btinternet.com> wrote in message
news:uj*************@TK2MSFTNGP11.phx.gbl...

Hi,

I need to update a DB Table (MySQL 4.0) with a string representing a
directory path (C:\Temp\In) but cannot get C# to do this.

I take the string value from a textbox (myStr = myText.Text) and pass
this to a function.

The function builds up a SQL string which will be executed:

SQLString = String.Format( "UPDATE BSConfig " +
"SET BSConfig_Inbound = \"{0}\",myStr);

This puts the quotes round the text, but remvoes the slashes. I tried
using the @ character (not sure what you call this lol) and the \" but
was given an error.

SQLString = String.Format( @"UPDATE BSConfig " +
"SET BSConfig_Inbound = \"{0}\",myStr); //Not allowed

Anyone help me out ?

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



If you want slashes and quotes, write \\\" for each.

Thus

"\\\"this\\\""

is a string containing the characters:

\"this\"


Steven Blair <st**********@btinternet.com> wrote:

I need to update a DB Table (MySQL 4.0) with a string representing a
directory path (C:\Temp\In) but cannot get C# to do this.

I take the string value from a textbox (myStr = myText.Text) and pass
this to a function.

The function builds up a SQL string which will be executed:

SQLString = String.Format( "UPDATE BSConfig " +
"SET BSConfig_Inbound = \"{0}\",myStr);



That''s a bad idea to start with.

Use parameters instead - it''ll make it *much* easier to deal with, as
you only need to worry about getting the correct data, with no
escaping, into a string, and then setting that as the parameter value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于似乎无法用双引号保存字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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