通过表单在客户端计算机和服务器计算机之间创建SQL Server连接

时间:2018-09-24 13:42:59

标签: c# sql-server

我有这样的表格:

image

如何使用这些字段打开与数据库的连接?

2 个答案:

答案 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);
            }