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

原创 2005年03月02日 13:19:00

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

VB.NET 使用 OleDb 操作 Access 数据库(来自 MSDN)

VB.NET 使用 OleDb 操作 Access 数据库 这里采用 OleDb 方式对 Access 数据库进行访问,主要内容都来自 MSDN 帮助文档。建议新手注意 MSDN 的用途。 首先导入命...

vb.net 操作Access数据库帮助类

VB.net 操作Access数据库的一些常用方法,代码如下 Imports System Imports System.Data.SqlClient Imports System.Config...

vb.net 操作Access数据库帮助类

VB.net 操作Access数据库的一些常用方法,代码如下:Imports System Imports System.Data.SqlClient Imports System.Configura...

VB.NET操作ACCESS数据库读取存入图片

一、准备工作     在ACCESS数据库中,将字段声明为 OLE链接 类型 二、将图片框中图片转化成二进制数组的方法         Dim MyStream As New System.IO....

vb.net操作数据库之ACCESS(一)

转载自:http://blog.csdn.net/machong8183/article/details/5488191 一直想写下数据库操作方面的,只是没什么时间(好像是借口,应该是懒或者没心情吧...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)