OdbcConnectionStringBuilder を使っている以外は特別なところはありません。
OdbcConnection の使い方と、接続のサンプルです
Imports System.Data.Odbc
Module Module1
Sub Main()
' 新しい OdbcConnectionStringBuilder オブジェクトを作成
Dim builder As New OdbcConnectionStringBuilder()
' ドライバ文字列をセット ( 波型括弧{} は必要ありません )
' 文字列を正確に取得するには、レジストリ : HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI
builder.Driver = "MySQL ODBC 5.1 Driver"
' 接続用のパラメータを追加
builder.Add("SERVER", "localhost")
builder.Add("DATABASE", "lightbox")
builder.Add("UID", "root")
builder.Add("PWD", "password")
' 内容を確認
Console.WriteLine(builder.ConnectionString)
' 新しい OdbcConnection オブジェクトを作成
Dim myCon As New OdbcConnection()
' 接続文字列を設定
myCon.ConnectionString = builder.ConnectionString
' 接続を開く
Try
myCon.Open()
Catch ex As OdbcException
Console.WriteLine("接続エラーです")
' Console.WriteLine( ex.Message )
Call ErrorAction( ex )
Exit Sub
End Try
' 接続を閉じる
myCon.Close()
' OdbcConnection オブジェクトに使用されているすべてのリソースを解放
myCon.Dispose()
' 処理終了
Console.WriteLine("処理が終了しました")
' 一時停止
Console.Write("Enterキーを押して下さい : ")
Console.ReadLine()
End Sub
' ******************************************************
' エラー処理
' ******************************************************
Sub ErrorAction( ex As OdbcException )
Dim CrLf As String = ControlChars.CrLf
Dim errorMessages As String = ""
Dim i As Integer
For i = 0 To ex.Errors.Count - 1
errorMessages &= _
"Index #" & i.ToString() & CrLf _
& "Message: " & ex.Errors(i).Message & CrLf _
& "NativeError: " & ex.Errors(i).NativeError.ToString() & CrLf _
& "Source: " & ex.Errors(i).Source & CrLf _
& "SQL: " & ex.Errors(i).SQLState & CrLf
Next i
Console.WriteLine(errorMessages)
End Sub
End Module
パラメータは、Microsoft の標準のものを使用していますが、MySQL が定義したものを利用できます。
(例)
builder.Add("SERVER", "localhost")
builder.Add("DATABASE", "lightbox")
builder.Add("USER", "root")
builder.Add("PASSWORD", "password")
builder.Add("PORT", "3306")
※ 全てのパラメータ
Parameter |
Comment |
user |
The user name used to connect to MySQL. |
uid |
Synonymous with user |
server |
The host name of the MySQL server. |
database |
The default database. |
option |
Options that specify how Connector/ODBC should work. See below. |
port |
The TCP/IP port to use if server is not
localhost . |
initstmt |
Initial statement. A statement to execute when connecting to MySQL. In
version 3.51 the parameter is called
stmt . Note, the driver supports the
initial statement being executed only at the time of the
initial connection. |
password |
The password for the user account on
server . |
pwd |
Synonymous with password |
socket |
The Unix socket file or Windows named pipe to connect to if
server is
localhost . |
sslca |
The path to a file with a list of trust SSL CAs. Added in 3.51.16. |
sslcapath |
The path to a directory that contains trusted SSL CA certificates in PEM
format. Added in 3.51.16. |
sslcert |
The name of the SSL certificate file to use for establishing a secure
connection. Added in 3.51.16. |
sslcipher |
A list of permissible ciphers to use for SSL encryption. The cipher list
has the same format as the openssl
ciphers command Added in 3.51.16. |
sslkey |
The name of the SSL key file to use for establishing a secure
connection. Added in 3.51.16. |
charset |
キャラクタセット設定 |
sslverify |
If set to 1, the SSL certificate will be verified when used with the
MySQL connection. If not set, then the default behavior
is to ignore SSL certificate verification. |
readtimeout |
The timeout in seconds for attempts to read from the server |
writetimeout |
The timeout in seconds for attempts to write to the server |
interactive |
Enables the CLIENT_INTERACTIVE connection option of
mysql_real_connect . |
次のステップ
VB.net 2008 : System.Data.Odbc 更新処理( MySQL 5.1 )