通过用户输入连接到本地数据库

时间:2017-05-09 14:01:39

标签: c# sql-server c#-4.0

我有一个程序,在单击事件中,SQL将打开并连接到服务器实例和数据库。我想让程序更加动态并允许用户输入,但我正在努力让这个按需工作。

旧代码如下,这有效:

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

我尝试了以下操作,但这只是打开SQL但状态不会使用enterd的详细信息来连接数据库。

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.

2 个答案:

答案 0 :(得分:0)

这是通过以下信息完成的

Ssms Utility

答案 1 :(得分: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; 
相关问题