我有这样的表格:
如何使用这些字段打开与数据库的连接?
答案 0 :(得分:0)
using(SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source=[SERVERNAME];Initial Catalogue=[DATABASENAME];User id=[USERNAME];Password=[PASSWORD];";
conn.Open();
// use the connection here
}
答案 1 :(得分:0)
我使用连接构建器
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
// Supply the additional values.
builder.DataSource = Server.Text +":" Port.Text;
builder.UserID = userName;
builder.Password = userPassword;
builder.InitialCatelog = Database.Text;
using (SqlConnection connection =
new SqlConnection(builder.ConnectionString))
{
connection.Open();
// Now use the open connection.
Console.WriteLine("Database = " + connection.Database);
}