2016-03-04 39 views
0

如何獲取現在連接到數據庫的連接字符串?如何獲取目前連接的連接字符串

我可以得到所有的連接字符串,但我怎麼能得到哪一個連接?

在此先感謝。

+0

您可以通過兩種方式。首先由通過此對象連接到sql的.NET'SqlConnection.ConnectionString'對象。第二次使用自己的Sql Server來告訴你什麼是當前連接字符串,如果你使用從sql server數據庫! – Behzad

回答

0

對於未使用默認連接的連接,例如。\ 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