2017-08-17 54 views
2

我的連接字符串:如何在SQL連接字符串的密碼中使用分號字符?

con = new SqlConnection("Server=localhost;Database=mainDB;User Id=sqluser;Password=Y;[email protected])V_"); 

,在密碼分號導致異常。如何將此密碼寫入連接字符串?

+0

爲什麼不直接在密碼中使用不同的字符?將它改爲克拉或其他東西。^ –

+2

用引號括起密碼值。 –

+0

@SeanLange我不是數據庫管理員,我沒有權限。 – Nathan

回答

2

您是否考慮過使用SqlConnectionStringBuilder

var sqlBuilder = new SqlConnectionStringBuilder 
{ 
    DataSource = "localhost", 
    InitialCatalog = "mainDB", 
    Password = "Y;[email protected])V_", 
    UserID = "sqluser" 
}; 

con = new SqlConnection(sqlBuilder.ToString()); 
+1

你打我吧:)'sqlBuilder.ConnectionString'可以用來代替'sqlBuilder.ToString()'。 –

0

也許你可以設置SqlCredentials算賬:

con = new SqlConnection("Server=localhost;Database=mainDB;User Id=sqluser;Password=dummy"); 
con.Credential = new SqlCredential("sqluser", "Y;[email protected])V_"); 
0

只需像這樣使用。

con = new SqlConnection("Server=localhost;Database=mainDB;User Id=sqluser;Password='Y;[email protected])V_'"); 
+0

欲瞭解更多信息 https://stackoverflow.com/questions/23014180/sql-server-2008-password-ending-in-a-semicolon –