如何获取现在连接到数据库的连接字符串?
我可以获取所有连接字符串,但是如何才能获得哪个连接字符串?
提前致谢。
答案 0 :(得分:0)
对于未使用默认连接的连接,例如。\ SQLEXPRESS以下内容将为您提供存储在app.config中的连接以及跟踪连接的方法。在表单示例中,我使用的是ms-access,但这也适用于sql-server。
编辑:here is a fully function code example在我刚创建的Microsoft代码示例网站上。
支持班级
Public Class ConfigItem
Public Property Data As System.Configuration.ConnectionStringSettings
Public Property Index As Integer
End Class
Public Class ConnectionInfo
Public Property Name As String
Public Property ConnectionString As String
Public Property Index As Integer
End Class
用于获取连接名称,索引和连接字符串的Worker类
Imports System.Configuration
''' <summary>
''' Must add a reference to System.Configuration
''' to this project.
''' </summary>
''' <remarks></remarks>
Public Class ProjectConnections
''' <summary>
''' Storage for connections
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Items As New List(Of ConnectionInfo)
''' <summary>
''' Used to remember the current connection
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property CurrentConnectionIndex As Integer
Private RemoveThis As String = ""
Public Sub New()
' look at parent assembly as this class is in a class project used by a
' forms project
RemoveThis = Reflection.Assembly.GetEntryAssembly.GetName.Name & ".My.MySettings."
' get connection data into the Items propery of this class
GetInformation()
End Sub
''' <summary>
''' Traverse through connection strings in app.config, exclude local sql-server connection
''' </summary>
''' <remarks>
''' tested with ms-access, sql-server attached and server based
''' </remarks>
Private Sub GetInformation()
ConfigurationManager.ConnectionStrings.Cast(Of ConnectionStringSettings)().Select(
Function(item, index) New ConfigItem With {.Data = item, .Index = index}).ToList _
.ForEach(
Sub(ConfigItem)
If ConfigItem.Data.Name.Contains(".") Then
Items.Add(
New ConnectionInfo With
{
.Name = ConfigItem.Data.Name.Replace(RemoveThis, ""),
.ConnectionString = ConfigItem.Data.ConnectionString,
.Index = ConfigItem.Index
})
End If
End Sub)
End Sub
End Class
利用上述课程表格。在这种情况下,app.config中存储了两个连接。 worker类在表单级别实例化,因此我们可以使用它来跟踪当前的连接字符串。可选地,我们可以本地化类并使用私有整数变量来记住当前连接。在表单加载中,我选择使用哪个连接而不是默认值,在worker类实例中存储该连接的索引,在ComboBox中显示连接字符串名称并公开DataGridView中的所有信息。按下按钮,我们在运行时更改连接,而第二个按钮显示基础连接字符串。
请注意import语句,我将worker类放入类项目中,因此表单项目必须引用它,然后是import语句。
Imports ConfigurationLibrary
Public Class Form1
Private connections As ProjectConnections = New ProjectConnections()
Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
Handles CustomersBindingNavigatorSaveItem.Click
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Database1DataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' here I am loading a connection other than the default
CustomersTableAdapter.Connection.ConnectionString = connections.Items(1).ConnectionString
' for keeping track later as in cmdGetConnection.Click
connections.CurrentConnectionIndex = 1
Me.CustomersTableAdapter.Fill(Me.Database1DataSet.Customers)
cboConnections.DataSource = connections.Items
cboConnections.DisplayMember = "Name"
cboConnections.SelectedIndex = 1
dgvInformation.AutoGenerateColumns = False
dgvInformation.DataSource = connections.Items
CustomersDataGridView.ExpandColumns()
dgvInformation.ExpandColumns()
End Sub
Private Sub cmdSetConnection_Click(sender As Object, e As EventArgs) Handles cmdSetConnection.Click
Dim OrdinalIndex As Integer = CType(cboConnections.SelectedItem, ConnectionInfo).Index - 1
CustomersTableAdapter.Connection.Close()
CustomersTableAdapter.Connection.ConnectionString = connections.Items(OrdinalIndex).ConnectionString
CustomersTableAdapter.Connection.Open()
CustomersTableAdapter.Fill(Me.Database1DataSet.Customers)
cboConnections.SelectedIndex = OrdinalIndex
End Sub
Private Sub cmdGetConnection_Click(sender As Object, e As EventArgs) Handles cmdGetConnection.Click
Dim sb As New System.Text.StringBuilder
sb.AppendLine(cboConnections.Text)
sb.AppendLine(connections.Items(connections.CurrentConnectionIndex).ConnectionString)
MessageBox.Show(sb.ToString)
End Sub
End Class