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 )
タグ:MySQL
|
|
【VB.NET : データベースの最新記事】
- VB.net : GetSchema メソッドでデータベース内のテーブル一覧と指定したテーブルの列名一覧を取得する
- VB.net : ListView に DB から読み込んだデータをセットする
- VB.net : 一時 PL/SQL で Data Pump Exportユーティリティと同じデータをエクスポートして処理結果を取得する
- VB.net : 一時PL/SQL 内の変数をプログラムでやり取りする
- VB.net : PL/SQL の OUT パラメータから実行結果を取得する
- VB.net : ODBC + MySQL で CSV をインポート( クラス化サンプル )
- VB.net : CSVによるインポート用ファイルの作成( つまりエクスポート )
- VB.net : insert 構文によるインポート用ファイルの作成( つまりエクスポート )
- VB.net 2008 : System.Data.Odbc データ取得(SELECT)処理( MySQL 5.1 )
- VB.net 2008 : System.Data.Odbc 更新処理( MySQL 5.1 )
- VB.net : Oracle の行を返さない SQL の実行



パラメータは、Microsoft の標準のものを使用していますが、MySQL が定義したものを利用できます。




