[英]Escape SQL Server connection string with C#
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? 我需要在C#代码中即时编写SQL Server连接字符串,我很好奇是否有内置的方法吗? 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";
您应该使用SqlConnectionStringBuilder
类,该类将为您完成所有这些操作并为您提供方便的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.