Top Index
001 | 'ADO.NETを利用のため、あらかじめインポートしておく | |
002 | Imports System.Data.OleDb | |
003 | Imports System.IO | |
004 | ||
005 | Public Class Form1 | |
006 | Inherits System.Windows.Forms.Form | |
007 | ||
008 | ┼ | #Region " Windows フォーム デザイナで生成されたコード " |
009 | ||
010 | Public Sub New() | |
011 | MyBase.New() | |
012 | ||
013 | ' この呼び出しは Windows フォーム デザイナで必要です。 | |
014 | InitializeComponent() | |
015 | ||
016 | ' InitializeComponent() 呼び出しの後に初期化を追加します。 | |
017 | ||
018 | End Sub | |
019 | ||
020 | ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 | |
021 | Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) | |
022 | If disposing Then | |
023 | If Not (components Is Nothing) Then | |
024 | components.Dispose() | |
025 | End If | |
026 | End If | |
027 | MyBase.Dispose(disposing) | |
028 | End Sub | |
029 | ||
030 | ' Windows フォーム デザイナで必要です。 | |
031 | Private components As System.ComponentModel.IContainer | |
032 | ||
033 | ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 | |
034 | 'Windows フォーム デザイナを使って変更してください。 | |
035 | ' コード エディタを使って変更しないでください。 | |
036 | Friend WithEvents Button1 As System.Windows.Forms.Button | |
037 | <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() | |
038 | Me.Button1 = New System.Windows.Forms.Button | |
039 | Me.SuspendLayout() | |
040 | ' | |
041 | 'Button1 | |
042 | ' | |
043 | Me.Button1.Location = New System.Drawing.Point(108, 108) | |
044 | Me.Button1.Name = "Button1" | |
045 | Me.Button1.Size = New System.Drawing.Size(84, 30) | |
046 | Me.Button1.TabIndex = 0 | |
047 | Me.Button1.Text = "MDBへの接続" | |
048 | ' | |
049 | 'Form1 | |
050 | ' | |
051 | Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12) | |
052 | Me.ClientSize = New System.Drawing.Size(292, 273) | |
053 | Me.Controls.Add(Me.Button1) | |
054 | Me.Name = "Form1" | |
055 | Me.Text = "Form1" | |
056 | Me.ResumeLayout(False) | |
057 | ||
058 | End Sub | |
059 | ||
060 | └ | #End Region |
061 | ||
062 | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click | |
063 | '■利用時の注意 | |
064 | 'ユーザー名、パスワードは平文でコードに埋め込まないでください! | |
065 | 'セキュリティ上の問題があります | |
066 | ||
067 | Dim DBPath As String | |
068 | DBPath = Path.Combine(Application.StartupPath, "sample.mdb") | |
069 | ||
070 | '■コネクション文字列の作成 | |
071 | Dim CnString As String | |
072 | 'コネクション文字列として必須の項目 | |
073 | CnString = "Provider=Microsoft.Jet.OLEDB.4.0;" 'AccessDBを使用するときの決り文句 | |
074 | CnString &= "Data Source=" & DBPath & ";" 'DBファイルのパス | |
075 | ||
076 | 'ワークグループファイル(mdw)を認証に用いた場合は、以下の項目を指定する | |
077 | Dim SysDBPath As String | |
078 | SysDBPath = Path.Combine(Application.StartupPath, "sample.mdw") | |
079 | ||
080 | CnString &= "Jet OLEDB:System Database=" & SysDBPath & ";" 'mdwファイルパス | |
081 | CnString &= "User ID=sa;" '利用ユーザー名 | |
082 | CnString &= "Password=sapassword;" 'ユーザーパスワード | |
083 | ||
084 | 'それ以外のオプション | |
085 | CnString &= "Persist Security Info=True;" 'DBが暗号化されていれば指定 | |
086 | CnString &= "Jet OLEDB:Database Password=dbpassword;" 'DB自体のパスワード | |
087 | ||
088 | '■コネクションオブジェクトの作成 | |
089 | Dim Cn As OleDbConnection | |
090 | Cn = New OleDbConnection(CnString) | |
091 | ||
092 | 'コネクションのオープン・クローズ | |
093 | Try | |
094 | Cn.Open() | |
095 | Cn.Close() | |
096 | Catch ex As Exception | |
097 | MsgBox(ex.Message) | |
098 | End Try | |
099 | ||
100 | End Sub | |
101 | End Class |