Retrieve information from data base connection string
الكود التالي يمكن إستخدامه في إسترجاع أو في استخلاص البيانات من اسم الكونكشن أو من
ConnectionString
الكود التالي يوضح شكل الكلاس المسؤل عن عملية إسترجاع البيانات ويمكن تطويره للإستخدام بأشكال أخري
الكود التالي يوضح شكل الكلاس المسؤل عن عملية إسترجاع البيانات ويمكن تطويره للإستخدام بأشكال أخري
Imports System.Data.CommonPublic Class ConnectionManager
Implements IConnectionManager
Private Shared dataManager As IConnectionManager
Public Shared ReadOnly Property Pattern As IConnectionManager
Get
Dim result As IConnectionManager = CType(Nothing, IConnectionManager)
SyncLock GetType(IConnectionManager)
If dataManager Is Nothing Then
dataManager = New ConnectionManager
End If
result = dataManager
End SyncLock
Return result
End Get
End Property
Private ReadOnly Property AdoDotNetConnectionKeys As IEnumerable(Of String) Implements IConnectionManager.AdoDotNetConnectionKeys
Get
Return New String() {"Hostname", "Host", "Server", "Data Source", "ServerName", "ServerNode"}
End Get
End Property
Private ReadOnly Property OdbcConnectionKeys As IEnumerable(Of String) Implements IConnectionManager.OdbcConnectionKeys
Get
Return New String() {"Hostname", "Host", "Server", "ServerName", "ServerNode"}
End Get
End Property
Private ReadOnly Property OleDbConnectionKeys As IEnumerable(Of String) Implements IConnectionManager.OleDbConnectionKeys
Get
Return New String() {"Hostname", "Host", "Server", "Data Source", "ServerName", "ServerNode"}
End Get
End Property
Private Function CreateConnection() As DbConnectionStringBuilder Implements IConnectionManager.CreateConnection
Return New DbConnectionStringBuilder()
End Function
Private Function CreateConnectionStringBuilder(connectionString As String) As DbConnectionStringBuilder Implements IConnectionManager.CreateConnectionStringBuilder
Dim result As DbConnectionStringBuilder = CType(Nothing, DbConnectionStringBuilder)
Try
If connectionString.Trim().Length = 0 Then
Dim formatEx As New FormatException("ConnectionString is Null")
Throw formatEx
End If
Dim connectionBuilder As DbConnectionStringBuilder = CreateConnection()
connectionBuilder.ConnectionString = connectionString
result = connectionBuilder
Catch ex As ArgumentException
Throw New FormatException(ex.Message, ex)
End Try
Return result
End Function
Friend Function GetConnectionHostName(connectionString As String, ConnectionType As ConnectionTypes) As String Implements IConnectionManager.GetConnectionHostName
Dim hostName As String = CType(Nothing, String)
If IsHostNameHaveValue(connectionString, ConnectionType, hostName) Then
Return hostName
End If
Return hostName
End Function
Friend Function GetConnectionInfos(connectionString As String) As List(Of ConnectionInfo) Implements IConnectionManager.GetConnectionInfos
Dim connectionBuilder As DbConnectionStringBuilder = CreateConnectionStringBuilder(connectionString)
Dim result As List(Of ConnectionInfo) = New List(Of ConnectionInfo)()
For Each key As String In connectionBuilder.Keys
Dim value As String = GetValueBound(connectionBuilder(key).ToString())
Dim info As ConnectionInfo = New ConnectionInfo With {.Key = key, .Value = value}
result.Add(info)
Next
Return result
End Function
Private Function IsHostNameHaveValue(connectionString As String, ConnectionType As ConnectionTypes, ByRef hostName As String) As Boolean Implements IConnectionManager.IsHostNameHaveValue
hostName = CType(Nothing, String)
Dim connectionBuilder As DbConnectionStringBuilder = CreateConnectionStringBuilder(connectionString)
Dim keyWords As IEnumerable(Of String) = CType(Nothing, IEnumerable(Of String))
Select Case ConnectionType
Case ConnectionTypes.ADO
keyWords = AdoDotNetConnectionKeys
Case ConnectionTypes.ODBC
keyWords = OdbcConnectionKeys
Case ConnectionTypes.OLEDB
keyWords = OleDbConnectionKeys
End Select
For Each keyWord As String In keyWords
Dim value As Object = CType(Nothing, Object)
If connectionBuilder.TryGetValue(keyWord, value) Then
If hostName IsNot Nothing Then
hostName = Nothing
Return False
End If
hostName = value.ToString()
End If
Next
Return hostName <> Nothing
End Function
Protected Friend Function GetValueBound(value As String) As String
Return value
End FunctionEnd Class
الكود التالي يوضح كيفية إستخدام الكلاس أعلاهImports RiverNileDataBasePublic Class Form1
Public str As String = "provider=microsoft.ace.oledb.12.0 ; data source=" & Application.StartupPath & "\db.accdb" & ";jet oledb:database password=" & "" & ""
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = ConnectionManager.Pattern.GetConnectionHostName(str, ConnectionTypes.ADO)
For Each info As ConnectionInfo In ConnectionManager.Pattern.GetConnectionInfos(str)
ListBox1.Items.Add(info.Key)
ListBox2.Items.Add(info.Value)
Next
End Sub
End Class
DownLoad The full Code
Link
Comments
Post a Comment