VB.Net中動態建立Access資料庫

來源:互聯網
上載者:User
access|建立|動態|資料|資料庫 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





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。