'Create Date : 2014-08-16 สดสดร้อนร้อน
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data.OracleClient
Imports MySql.Data.MySqlClient
Public Class DbProvider
Private Shared sqlFactory As DbProviderFactory
Private Shared oracleFactory As DbProviderFactory
Private Shared mysqlFactory As DbProviderFactory
Private Shared oledbFactory As DbProviderFactory
Private Shared sqliteFactory As DbProviderFactory
Private Shared db2Factory As DbProviderFactory
Private Shared vfpFactory As DbProviderFactory 'Visual FoxPro Provider
Private Shared unknowFactory As DbProviderFactory
Private Shared providerName As String = PubConstant.ProviderName 'declare in web.config
Public Shared Function GetFactory(Optional ByVal pvdStr As String = "") As DbProviderFactory
If pvdStr = String.Empty Then
pvdStr = providerName
End If
Select Case pvdStr
Case "System.Data.SqlClient"
If sqlFactory Is Nothing Then
sqlFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return sqlFactory
Case "System.Data.OracleClient"
If oracleFactory Is Nothing Then
oracleFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return oracleFactory
Case "System.Data.MySqlClient"
If mysqlFactory Is Nothing Then
mysqlFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return mysqlFactory
Case "System.Data.OleDb"
If oledbFactory Is Nothing Then
oledbFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return oledbFactory
Case "System.Data.DB2Client"
If db2Factory Is Nothing Then
db2Factory = DbProviderFactories.GetFactory(pvdStr)
End If
Return db2Factory
Case "System.Data.VFPClient" 'FastDBClient (www.codeplex.com)
If vfpFactory Is Nothing Then
vfpFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return vfpFactory
Case Else
If unknowFactory Is Nothing Then
unknowFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return unknowFactory
End Select
End Function
Public Shared Function CreateConnection(Optional ByVal pvdStr As String = "") As DbConnection
If pvdStr = String.Empty Then
pvdStr = providerName
End If
Dim cn As DbConnection = DbProvider.GetFactory(pvdStr).CreateConnection()
cn.ConnectionString = PubConstant.ConnectionString 'declare in web.config
Return cn
End Function
Public Function BuildParameter(ByVal pName As String, pValue As Object, Optional ByVal pvdStr As String = "") As DbParameter
Dim dbParam As DbParameter = Nothing
If pvdStr = String.Empty Then
pvdStr = providerName
End If
Select Case pvdStr
Case "System.Data.SqlClient"
dbParam = New SqlParameter() With {.ParameterName = pName, .Value = pValue}
Case "System.Data.OracleClient"
dbParam = New OracleParameter() With {.ParameterName = pName, .Value = pValue}
Case "System.Data.MySqlClient"
dbParam = New MySqlParameter() With {.ParameterName = pName, .Value = pValue}
Case "System.Data.OleDb"
dbParam = New OleDbParameter With {.ParameterName = pName, .Value = pValue}
Case "System.Data.DB2Client"
'
Case "System.Data.VFPClient" 'FastDBClient (www.codeplex.com)
'
Case Else
'Not Implement yet.
End Select
Return dbParam
End Function
Public Shared Function FixedParam(ByVal s As String, Optional ByVal pvdStr As String = "") As String
If pvdStr = String.Empty Then
pvdStr = providerName
End If
Select Case pvdStr
Case "System.Data.SqlClient", "System.Data.MySqlClient"
s = s.Replace("@_@", "@")
Case "System.Data.OracleClient"
s = s.Replace("@_@", ":")
Case "System.Data.OleDb", "System.Data.VFPClient"
s = s.Replace("@_@", "?")
Case "System.Data.DB2Client"
'
Case "System.Data.VFPClient" 'FastDBClient (www.codeplex.com)
'
Case Else
'Not Implement yet.
End Select
Return s
End Function
'ทดสอบ
Public Sub Test()
Using cn As DbConnection = CreateConnection()
Using cmd As DbCommand = cn.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = FixedParam("Insert into Student Values(@_@StudentCode, @_@StudentName, @_@Age)")
Dim lstParam As New List(Of DbParameter) From
{BuildParameter("StudentCode", "0001"),
BuildParameter("StudentName", "ตัวเล็ก หน้าอกใหญ่"),
BuildParameter("Age", 18)
}
cmd.Parameters.Clear()
cmd.Parameters.Add(lstParam)
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
Dim dtReturn As New DataTable()
Using Conn As New SqlConnection("Data Source = ServerName;...")
Using da As New SqlDataAdapter("Select * From Table...", Conn)
da.Fill(dtReturn)
End Using
End Using
Dim dtReturn As New DataTable()
Using Conn As DbConnection = DbProvider.CreateConnection()
Using da As DbDataAdapter = DbProvider.GetFactory().CreateDataAdapter()
Using cmd As DbCommand = Conn.CreateCommand()
cmd.CommandText = "Select * From Table..."
da.SelectCommand = cmd
da.Fill(dtReturn)
End Using
End Using
End Using
Imports System.ComponentModel
Imports System.Configuration
Public Class PubConstant
Sub New()
End Sub
Public Enum Language
<Description("th-TH")> _
Thai
<Description("en-US")> _
English
<Description("jp-JP")> _
Japan
<Description("lao-LAO")> _
Lao
End Enum
Public Enum SearchOption
Equal
NotEqual
MoreThan
LessThan
MoreThanOrEqual
LessThanOrEqual
Between
StartWith
EndWith
Contain
End Enum
Public Enum RightsOption
View
Insert
Update
Delete
Approved
Print
Export
End Enum
'
'อ่านจากจาก web.config/app.config
'
Public Shared ReadOnly Property GetAppSettingsValue(ByVal key As String) As String
Get
Return ConfigurationManager.AppSettings(key)
End Get
End Property
Public Shared ReadOnly Property ProviderName() As String
Get
Return GetAppSettingsValue("ProviderName")
End Get
End Property
Public Shared ReadOnly Property ProviderName(ByVal cs As String) As String
Get
Return ConfigurationManager.ConnectionStrings(cs).ProviderName
End Get
End Property
Public Shared ReadOnly Property ConnectionString() As String
Get
Dim _connectionString As String = ConfigurationManager.AppSettings("ConnectionString")
Dim IsEncrypt As String = Security.Crypter.DESDecrypt(ConfigurationManager.AppSettings("ConnectionStringEncrypt"))
If IsEncrypt = "true" Then
_connectionString = Security.Crypter.DESDecrypt(_connectionString)
End If
Return _connectionString
End Get
End Property
Public Shared ReadOnly Property ConnectionString(ByVal cs As String) As String
Get
Return ConfigurationManager.ConnectionStrings(cs).ConnectionString
End Get
End Property
End Class
ตัวอย่างไฟล์ web.config
Code (XML)
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!--<sectionGroup name="system.web">
<section name="sanitizer" requirePermission="false" type="AjaxControlToolkit.Sanitizer.ProviderSanitizerSection, AjaxControlToolkit"/>
</sectionGroup>-->
</configSections>
<connectionStrings>
<add name="Mssql" connectionString="Data Source=SOMPARN-PC;Initial Catalog=SomparnDB;User ID=sa;Password=9876" providerName="System.Data.SqlClient" />
<add name="Oracle" connectionString="XXXX" providerName="System.Data.OracleClient"/>
<add name="MySQL" connectionString="Server=191.168.1.1;Database=myDataBase;Uid=myUsername;Pwd=myPassword;" providerName="MySql.Data.MySqlClient"/>
<add name="SQlite" connectionString="Data Source= ~/SQLiteDB/Test_DB_V3.s3db;Version=3;UTF8Encoding=True;Password=9876;" providerName="System.Data.SqlLite" />
</connectionStrings>
<appSettings>
<add key="ConnectionStringEncrypt" value="870042BB4DFE7420" />
<!-- true = 870042BB4DFE7420, false = A154273BF2464F4E -->
<add key="ConnectionString" value="EB4BE3F2D06F5CC873B2C82C8BC034C04B2F4ABC592AFB46ABC33BF919EE02729742B42202B5B10128131010FB11A0F49BA59AF24FC07A44DA70AC41250923EF045B855B802EAF02769B274B42A00E5D" />
<add key="providerName" value="System.Data.SqlClient" />
<add key="RDBMSType" value="MSSQL" />
Public Shared Function GetFactory(Optional ByVal pvdStr As String = "") As DbProviderFactory
If pvdStr = String.Empty Then
pvdStr = providerName
End If
Select Case pvdStr
Case "System.Data.SqlClient"
If sqlFactory Is Nothing Then
sqlFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return sqlFactory
Case "System.Data.OracleClient"
If oracleFactory Is Nothing Then
oracleFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return oracleFactory
Case "MySql.Data.MySqlClient"
If mysqlFactory Is Nothing Then
mysqlFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return mysqlFactory
Case "System.Data.OleDb"
If oledbFactory Is Nothing Then
oledbFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return oledbFactory
Case "System.Data.SQLite"
If sqliteFactory Is Nothing Then
sqliteFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return sqliteFactory
Case "FirebirdSql.Data.FirebirdClient" 'Version 4.5.0.0 (2014-08-17)
If firebirdFactory Is Nothing Then
firebirdFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return firebirdFactory
Case "System.Data.DB2Client"
If db2Factory Is Nothing Then
db2Factory = DbProviderFactories.GetFactory(pvdStr)
End If
Return db2Factory
Case "System.Data.VFPClient" '(www.codeplex.com)
If vfpFactory Is Nothing Then
vfpFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return vfpFactory
Case Else
If unknowFactory Is Nothing Then
unknowFactory = DbProviderFactories.GetFactory(pvdStr)
End If
Return unknowFactory
End Select
End Function