在web.config连接字符串中转义引用 [英] Escape quote in web.config connection string

查看:171
本文介绍了在web.config连接字符串中转义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < add name =MyConStringconnectionString =Server = dbsrv; User ID = myDbUser; Password = somepasswordproviderName =System.Data.SqlClient/> 

如你所见,密码中有一个引号()(由其他部门提供,我不能更改此db用户密码)。



如何避免这个连接字符串中的引号?



Btw:我已经尝试过了QUOT;在字符串中。这没有工作 - ado.net得到一个ArgumenException然后:初始化字符串的格式不符合规范从索引57开始。
57是& QUOT;在我的连接字符串。
我也尝试将密码部分包含在' - 也没有工作。



也试过和\ - web.config不能感谢解决方案:



我不得不将转义的双引号和将密码放在单引号中:

 < add name =MyConStringconnectionString =Server = dbsrv; User ID = myDbUser ; Password ='somepass& quot; word'providerName =System.Data.SqlClient/> 


解决方案

使用& quot; 而不是



web.config是一个XML文件,因此您应该使用XML转义。

  connectionString =Server = dbsrv; User ID = myDbUser; Password = somepass&

请参阅论坛线。



更新



& quot; / code>应该工作,但是没有,你尝试过一些其他的字符串转义序列为.NET? \



更新2:



尝试使用connectionString的单引号:

  connectionString ='Server = dbsrv; User ID = myDbUser; Password = somepassword'

或者:

  connectionString ='Server = dbsrv; User ID = myDbUser; Password = somepass& quot; word'

更新3:



MSDN (SqlConnection.ConnectionString属性):


要包含包含分号,单引号字符或双引号字符的值,该值必须包含在双引号。如果值包含分号和双引号字符,则该值可以用单引号括起来。


所以: / p>

  connectionString =Server = dbsrv; User ID = myDbUser; Password ='somepass& quot; word'

问题不在于web.config,而是连接字符串的格式。在一个连接字符串中,如果一个值(键值对)中有一个,则需要将值包含在'所以,虽然 Password = somepassword 不起作用, Password ='somepassword'


I have a connection string in my web config:

<add name="MyConString" connectionString="Server=dbsrv;User ID=myDbUser;Password=somepass"word" providerName="System.Data.SqlClient" />

As you see, there is a quotation sign ( " ) in the password (given from other dept. I can't change this db users password).

How do I have to escape the quote in this connection string?

Btw: I already tried & quot; in the string. That didn't work - ado.net got an ArgumenException then: "Format of the initialization string does not conform to specification starting at index 57." 57 is where the & quot; is in my connection string. I also tried enclosing the password part in ' - didn't work either.

Also tried "" and \" - web.config can't be parsed then.

Thanks for the solution:

I had to combine the escaping of the double quote and putting the password in single quotes:

<add name="MyConString" connectionString="Server=dbsrv;User ID=myDbUser;Password='somepass&quot;word'" providerName="System.Data.SqlClient" />

解决方案

Use &quot; instead of " to escape it.

web.config is an XML file so you should use XML escaping.

connectionString="Server=dbsrv;User ID=myDbUser;Password=somepass&quot;word"

See this forum thread.

Update:

&quot; should work, but as it doesn't, have you tried some of the other string escape sequences for .NET? \" and ""?

Update 2:

Try single quotes for the connectionString:

connectionString='Server=dbsrv;User ID=myDbUser;Password=somepass"word'

Or:

connectionString='Server=dbsrv;User ID=myDbUser;Password=somepass&quot;word'

Update 3:

From MSDN (SqlConnection.ConnectionString Property):

To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks. If the value contains both a semicolon and a double-quote character, the value can be enclosed in single quotation marks.

So:

connectionString="Server=dbsrv;User ID=myDbUser;Password='somepass&quot;word'"

The issue is not with web.config, but the format of the connection string. In a connection string, if you have a " in a value (of the key-value pair), you need to enclose the value in '. So, while Password=somepass"word does not work, Password='somepass"word' does.

这篇关于在web.config连接字符串中转义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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