本文介绍了如何在连接字符串中动态输入数据库的服务器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ConnectionString = "Data Source=LUSPOC-PC\LUSPOC;Initial Catalog=Sales_Inventory;Integrated Security=True"
这是我将vb.net连接到MS SQL Server 2014的代码。是否可以从我的GUI输入LUSPOC-PC\LUSPOC,以便可以从不同的数据库服务器使用它?是的如何?
我的尝试:
that is my code for connecting vb.net to MS SQL server 2014. Is it Possible to input "LUSPOC-PC\LUSPOC" from my GUI so that it can be use from different Database servers ? is Yes How ?
What I have tried:
ConnectionString = "Data Source=@SName;Initial Catalog=Sales_Inventory;Integrated Security=True"
Dim cmd As New SqlCommand
cmd.Parameters.AddWithValue("@SName", TextBox1.Text)
推荐答案
Dim connstr As System.Data.SqlClient.SqlConnectionStringBuilder
connstr.InitialCatalog = "Sales_Inventory"
connstr.IntegratedSecurity = True
connstr.DataSource = TextBox1.Text
System.Windows.Forms.MessageBox.Show(connstr.ConnectionString)
这篇关于如何在连接字符串中动态输入数据库的服务器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!