VB.Net中动态创建Access数据库

类别:.NET开发 点击:0 评论:0 推荐:

Imports ADOX

Module Module1

    Public Function CreateAccessDB(ByVal NewDBPathName As String) As Integer
        Dim i As Integer = 1
        Dim cat As Catalog = New Catalog()
        Try
            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                      "Data Source=" & NewDBPathName & _
                      "Jet OLEDB:Engine Type=5")

            'Console.WriteLine("Database Created Successfully")
        Catch
            i = 0
            MsgBox("错误号 : " & Err.Number.ToString & " 错误描述:" & Err.Description.ToString)

        End Try


        cat = Nothing
        Return i
    End Function

End Module

Imports System.IO
Imports ADOX

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim filename As String = ""

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents SaveFileDialog2 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
        Me.SaveFileDialog2 = New System.Windows.Forms.SaveFileDialog()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(160, 104)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(232, 48)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "创建access数据库"
        '
        'SaveFileDialog1
        '
        Me.SaveFileDialog1.FileName = "doc1"
        '
        'SaveFileDialog2
        '
        Me.SaveFileDialog2.FileName = "doc1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(160, 192)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(224, 56)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "build dataase"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(592, 286)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str1 As String = "d:\mrfu\newaccess"
        Dim str2 As String = "newAccess"

        If File.Exists("d:\mrfu\newmdb.mdb") Then
            MsgBox("the file have exists !")
            Exit Sub

        End If
        If CreateAccessDB(str1) Then
            MsgBox("sucess!")
        Else
            MsgBox("failed!")

        End If
        'me.SaveFileDialog1

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        With Me.SaveFileDialog1
            .AddExtension = True            

            .DefaultExt = "mdb"  
            .CheckPathExists = True
            .Filter = "(Access数据库)*.mdb|*.mdb"
            .OverwritePrompt = True   

            .FilterIndex = 1
        End With


        If Me.SaveFileDialog1.ShowDialog() = DialogResult.OK Then


            filename = Me.SaveFileDialog1.FileName
            If filename <> "" Then
                MsgBox(filename)
            Else
                Exit Sub

            End If
        End If

        If File.Exists(filename) Then
            MsgBox("the file have exists !")
            File.Delete(filename)
            MsgBox("had delete!")

        End If

        filename = filename & ";"
        MsgBox(filename)

        If CreateAccessDB(filename) Then
            MsgBox("sucess!")
        Else
            MsgBox("failed!")

        End If
    End Sub
End Class

本文地址:http://com.8s8s.com/it/it41664.htm