0

I need to compose a SQL Server connection string on-the-fly in my C# code and I was curious if there's a built-in way of doing it? So far I have this:

string connectionString = "Server=" + server + 
      "; Database=" + db + 
      "; User Id=" + userName + 
      "; password=" + pass + 
      "; Connection Timeout=15";

And also, do I need to include all names in double quotes as such?

string connectionString = "Server=\"" + server + 
      "\"; Database=\"" + db + 
      "\"; User Id=\"" + userName + 
      "\"; password=\"" + pass + 
      "\"; Connection Timeout=15";
4

1 回答 1

5

您应该使用SqlConnectionStringBuilder该类,它会为您完成所有这些工作并为您提供方便的属性。

于 2013-10-21T00:29:53.050 回答