Escape quote in web.config connection string

后端 未结 5 1743
梦谈多话
梦谈多话 2020-11-30 02:13

I have a connection string in my web config:



        
5条回答
  •  青春惊慌失措
    2020-11-30 02:43

    Use " 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"word"
    

    See this forum thread.

    Update:

    " 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"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"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.

提交回复
热议问题