2017-04-02 60 views
0

我想創建一個Windows窗體,讓用戶選擇一個SQL Server實例來連接並在該數據庫上運行一個查詢應該在那裏。讓用戶選擇一個連接到SQL Server

這可能嗎?我如何創建一個動態SQL連接?

+0

,有此類[SqlConnectionStringBuilder](https://msdn.microsoft.com/en -us /庫/ system.data.sqlclient.sqlconnectionstringbuilder(v = vs.110)的.aspx) – Steve

回答

2

要創建可以使用SqlConnectionStringBuilder連接字符串,在這之後,你可以通過創建的連接字符串到SqlConnection

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder 
{ 
    UserID = "Some User ID", 
    Password = "Some Database Password", 
    InitialCatalog = "Some Initial Catalog", 
    DataSource = "Some Data Source" 
}; 

string connectionString = builder.ToString(); 

using (SqlConnection conn = new SqlConnection(connectionString)) 
{ 
    //... 
}