KySoto

Connection String Builder

Jun 18th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.18 KB | None | 0 0
  1. Imports System.Reflection
  2.  
  3.    Public Enum ConnectionClient
  4.         'may eventually add other clients,but untill we actually support them, whats the point?
  5.         <StringValue("")> NoClient = 0
  6.         <StringValue("SQLNCLI10")> SqlClient10 = 1
  7.         'this is the OLEDB provider for sql client 10
  8.         <StringValue("Microsoft.Jet.OLEDB.4.0")> AccessJet4 = 2
  9.         <StringValue("Microsoft.ACE.OLEDB.12.0")> AccessAce12 = 3
  10.         <StringValue("Microsoft.Jet.OLEDB.4.0")> ExcelJet4 = 4
  11.         <StringValue("Microsoft.ACE.OLEDB.12.0")> ExcelAce12 = 5
  12.     End Enum
  13.  
  14.     Public Class StringValueAttribute
  15.         Inherits Attribute
  16.  
  17.         Public Property Value As String
  18.         Public Sub New(ByVal val As String)
  19.             Value = val
  20.         End Sub
  21.  
  22.     End Class
  23.  
  24.     Public Function GetEnumByStringValueAttribute(value As String, enumType As Type) As Object
  25.         For Each val As [Enum] In [Enum].GetValues(enumType)
  26.             Dim fi As FieldInfo = enumType.GetField(val.ToString())
  27.             Dim attributes As StringValueAttribute() = DirectCast(fi.GetCustomAttributes(GetType(StringValueAttribute), False), StringValueAttribute())
  28.             Dim attr As StringValueAttribute = attributes(0)
  29.             If attr.Value = value Then
  30.                 Return val
  31.             End If
  32.         Next
  33.         Throw New ArgumentException("The value '" & value & "' is not supported.")
  34.     End Function
  35.  
  36.     Public Function GetEnumByStringValueAttribute(Of YourEnumType)(value As String) As YourEnumType
  37.         Return CType(GetEnumByStringValueAttribute(value, GetType(YourEnumType)), YourEnumType)
  38.     End Function
  39.  
  40.     Public Function GetEnumValue(Of YourEnumType)(p As YourEnumType) As String
  41.         Return DirectCast(Attribute.GetCustomAttribute(ForValue(p), GetType(StringValueAttribute)), StringValueAttribute).Value
  42.     End Function
  43.  
  44.     Private Function ForValue(Of YourEnumType)(p As YourEnumType) As MemberInfo
  45.         Return GetType(YourEnumType).GetField([Enum].GetName(GetType(YourEnumType), p))
  46.     End Function
  47.  
  48.     Public Class ConnectionStringBuilder
  49.         Private _Client As ConnectionClient
  50.         Private _ServerName As String
  51.         Private _Database As String
  52.         Private _DataSource As String
  53.         Private _ConnectionString As String
  54.         Private _connectionStringBuilt As Boolean
  55.         Private _HDR As Boolean = True
  56.  
  57.         Public Property Client As ConnectionClient
  58.             Get
  59.                 Return _Client
  60.             End Get
  61.             Set(value As ConnectionClient)
  62.                 _connectionStringBuilt = False
  63.                 If value = ConnectionClient.NoClient Then
  64.                     _Client = value
  65.                     ServerName = ""
  66.                     Database = ""
  67.                     DataSource = ""
  68.                 ElseIf value = ConnectionClient.SqlClient10 Then
  69.                     _Client = value
  70.                     DataSource = ""
  71.                 ElseIf value = ConnectionClient.AccessJet4 Or value = ConnectionClient.AccessAce12 Or value = ConnectionClient.ExcelJet4 Or value = ConnectionClient.ExcelAce12 Then
  72.                     _Client = value
  73.                     ServerName = ""
  74.                     Database = ""
  75.                 Else
  76.                     _Client = ConnectionClient.NoClient
  77.                     _ServerName = ""
  78.                     Database = ""
  79.                     DataSource = ""
  80.                 End If
  81.             End Set
  82.         End Property
  83.  
  84.         Public Sub BuildConnectionString()
  85.             Dim out As String = "Provider=" & GetEnumValue(Client)
  86.             If Client = ConnectionClient.NoClient Then
  87.                 out = ""
  88.             ElseIf Client = ConnectionClient.SqlClient10 Then
  89.                 If Not ServerName.Equals("") And Not Database.Equals("") Then
  90.                     out += ";Server=" & ServerName & ";Database=" & Database & ";Trusted_Connection=yes;"
  91.                 Else
  92.                     out = ""
  93.                 End If
  94.  
  95.             ElseIf Client = ConnectionClient.AccessJet4 Then
  96.                 If DataSource Like "*\*.mdb" Then
  97.                     out += ";Data Source=" & ControlChars.Quote & DataSource & ControlChars.Quote & ";User Id=admin;Password=;"
  98.                 Else
  99.                     out = ""
  100.                 End If
  101.             ElseIf Client = ConnectionClient.AccessAce12 Then
  102.                 If DataSource Like "*\*.mdb" Or DataSource Like "*\*.accdb" Then
  103.                     out += ";Data Source=" & ControlChars.Quote & DataSource & ControlChars.Quote & ";Persist Security Info=False;"
  104.                 Else
  105.                     out = ""
  106.                 End If
  107.             ElseIf Client = ConnectionClient.ExcelJet4 Then
  108.                 If DataSource Like "*\*.xls" Then
  109.                     out += ";Data Source=" & ControlChars.Quote & DataSource & ControlChars.Quote & ";Extended Properties=" & ControlChars.Quote & "Excel 8.0;HDR="
  110.                     If HDR Then
  111.                         out += "YES;"
  112.                     Else
  113.                         out += "NO;"
  114.                     End If
  115.                     out += "IMEX=1" & ControlChars.Quote & ";"
  116.                 Else
  117.                     out = ""
  118.                 End If
  119.             ElseIf Client = ConnectionClient.ExcelAce12 Then
  120.                 If DataSource Like "*\*.xls" Then
  121.                     out += ";Data Source=" & ControlChars.Quote & DataSource & ControlChars.Quote & ";Extended Properties=" & ControlChars.Quote & "Excel 8.0;HDR="
  122.                     If HDR Then
  123.                         out += "YES;"
  124.                     Else
  125.                         out += "NO;"
  126.                     End If
  127.                     out += ControlChars.Quote & ";"
  128.                 ElseIf DataSource Like "*\*.xlsx" Then
  129.                     out += ";Data Source=" & ControlChars.Quote & DataSource & ControlChars.Quote & ";Extended Properties=" & ControlChars.Quote & "Excel 12.0 Xml;HDR="
  130.                     If HDR Then
  131.                         out += "YES;"
  132.                     Else
  133.                         out += "NO;"
  134.                     End If
  135.                     out += "IMEX=1" & ControlChars.Quote & ";"
  136.                 ElseIf DataSource Like "*\*.xlsb" Then
  137.                     out += ";Data Source=" & ControlChars.Quote & DataSource & ControlChars.Quote & ";Extended Properties=" & ControlChars.Quote & "Excel 12.0;HDR="
  138.                     If HDR Then
  139.                         out += "YES;"
  140.                     Else
  141.                         out += "NO;"
  142.                     End If
  143.                     out += ControlChars.Quote & ";"
  144.                 ElseIf DataSource Like "*\*.xlsm" Then
  145.                     out += ";Data Source=" & ControlChars.Quote & DataSource & ControlChars.Quote & ";Extended Properties=" & ControlChars.Quote & "Excel 12.0 Macro;HDR="
  146.                     If HDR Then
  147.                         out += "YES;"
  148.                     Else
  149.                         out += "NO;"
  150.                     End If
  151.                     out += "IMEX=1" & ControlChars.Quote & ";"
  152.                 Else
  153.                     out = ""
  154.                 End If
  155.             End If
  156.             _connectionStringBuilt = True
  157.             _ConnectionString = out
  158.         End Sub
  159.  
  160.         Public ReadOnly Property ConnectionString As String
  161.             Get
  162.                 If _connectionStringBuilt = False Then
  163.                     BuildConnectionString()
  164.                 End If
  165.                 Return _ConnectionString
  166.             End Get
  167.         End Property
  168.  
  169.         Public Property ServerName As String
  170.             Get
  171.                 Return _ServerName
  172.             End Get
  173.             Set(value As String)
  174.                 _connectionStringBuilt = False
  175.                 _ServerName = value
  176.             End Set
  177.         End Property
  178.  
  179.         Public Property Database As String
  180.             Get
  181.                 Return _Database
  182.             End Get
  183.             Set(value As String)
  184.                 _connectionStringBuilt = False
  185.                 _Database = value
  186.             End Set
  187.         End Property
  188.  
  189.         Public Property DataSource As String
  190.             Get
  191.                 Return _DataSource
  192.             End Get
  193.             Set(value As String)
  194.                 _connectionStringBuilt = False
  195.                 _DataSource = value
  196.             End Set
  197.         End Property
  198.  
  199.         Public Property HDR As Boolean
  200.             Get
  201.                 Return _HDR
  202.             End Get
  203.             Set(value As Boolean)
  204.                 _connectionStringBuilt = False
  205.                 _HDR = value
  206.             End Set
  207.         End Property
  208.  
  209.         Public Sub New()
  210.             Client = ConnectionClient.NoClient
  211.         End Sub
  212.  
  213.         Public Sub New(ByVal argClient As ConnectionClient, argServername As String, argDatabase As String)
  214.             Client = argClient
  215.             ServerName = argServername
  216.             Database = argDatabase
  217.             BuildConnectionString()
  218.         End Sub
  219.  
  220.         Public Sub New(ByVal argClient As ConnectionClient, argDataSource As String, Optional argHDR As Boolean = False)
  221.             Client = argClient
  222.             DataSource = argDataSource
  223.             HDR = argHDR
  224.             BuildConnectionString()
  225.         End Sub
  226.     End Class
Advertisement
Add Comment
Please, Sign In to add comment