如何获取目前连接的连接字符串

问题描述:

如何获取现在连接到数据库的连接字符串?如何获取目前连接的连接字符串

我可以得到所有的连接字符串,但我怎么能得到哪一个连接?

在此先感谢。

+0

您可以通过两种方式。首先由通过此对象连接到sql的.NET'SqlConnection.ConnectionString'对象。第二次使用自己的Sql Server来告诉你什么是当前连接字符串,如果你使用从sql server数据库! – Behzad

对于未使用默认连接的连接,例如。\ SQLEXPRESS下面将给你存储在app.config中的连接和一个跟踪连接的方法。在表单示例中,我使用了ms-access,但这也适用于sql-server。

编辑here is a fully function code example上我刚刚创建的微代码示例web sute。

支持类

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 

工人类来获取连接名称,索引和连接字符串

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中的所有信息。按下按钮,我们在运行时更改连接,而第二个按钮显示底层连接字符串。

请注意导入语句,我将工作类放入类项目中,以便表单项目必须具有对它的引用,然后是导入语句。

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 

截图 enter image description here