In C#, how do I manually build a proper connection string? I have the开发者_Python百科 server name, database name, user name, and password. This is a SQL Server database and .NET 4.0.
I looked at the SQLConnectionStringBuilder and that appears to be what I want but I don't know how to specify the server.
On the SqlConnectionStringBuilder use the DataSource property for the server host name/ip address.
Have a look at the examples at MSDN:
MSDN - SqlConnectionStringBuilder Class
The line of code in question:
builder["Server"] = yourServerName;
Your looking for the DataSource Property
Although you could just set the ConnectionString to something like
Data Source =myServerAddress; Initial Catalog =myDataBase; User Id =myUsername; Password =myPassword;
For alternative connection string for SQL Server 2008 see
http://www.connectionstrings.com/sql-server-2008#p1
You're right : SqlConnectionStringBuilder
is the way to go. The DataSource
property is the server name. You can find more info about this class on the msdn library. One easy way to figure out what properties you have to set may be to initialize a SqlConnectionStringBuilder
using an existing connection string and seeing which properties are used.
For instance, it's likely that you'll use IntialCatalog
(the name of the database you want to connect to).
I suggest taking a look at connectionstrings.com.
You can use the SQLConnectionStringBuilder
constructor overload that takes a connectionstring as described in the above site.
精彩评论