2017-05-09 51 views
0

我有一個程序,其中點擊事件SQL將打開並連接到服務器實例和數據庫,但是。我想讓程序變得更加動態,並允許用戶輸入,但我正在努力讓它按照想要的方式工作。通過用戶輸入連接到本地數據庫

舊的代碼如下,這個工程:

Process.Start("ssms.exe", "-S .\\SQLEXPRESS -d master -E "); 

我嘗試以下但這只是打開SQL但美國不會使用明細照耀處連接到數據庫。

Process.Start("ssms.exe", DataBaseNameInput.Text); 

編輯

錯誤SQL顯示是The following files where specified on the command line: These files could not be found and will not be loaded.

+0

你看過ssms的文檔嗎?這是使用它的有效方法嗎?你有沒有*任何研究? –

+0

你是如何收集輸入的?什麼是你的用戶界面?你是否也可以包含任何異常/錯誤? –

+0

@ rory.ap我看了一些公平的文檔,但我無法找到任何與我正在嘗試的東西有關的東西:(我的意思是也許這意味着我不能,但我想這裏有人可能會指向正確的方向 – Tom

回答

0

做爲此,您需要使用SqlConnectionStringBuilder(),這允許你建立一個字符串然後可以通過。

 var Connection = new SqlConnectionStringBuilder(); 
     Connection.DataSource = ServerNameTextBox.Text; 
     Connection.InitialCatalog = DatabaseTextbox.Text; 
     Connection.UserID = UserNameTextBox.Text; 
     Connection.Password = PasswordTextBox.Text; 
     var connString = Connection.ConnectionString;