VB.Net中动态创建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

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2Me.Button1 = New System.Windows.Forms.Button()   
  3Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()   
  4Me.SaveFileDialog2 = New System.Windows.Forms.SaveFileDialog()   
  5Me.Button2 = New System.Windows.Forms.Button()   
  6Me.SuspendLayout()   
  7'   
  8'Button1   
  9'   
 10Me.Button1.Location = New System.Drawing.Point(160, 104)   
 11Me.Button1.Name = "Button1"   
 12Me.Button1.Size = New System.Drawing.Size(232, 48)   
 13Me.Button1.TabIndex = 0   
 14Me.Button1.Text = "创建access数据库"   
 15'   
 16'SaveFileDialog1   
 17'   
 18Me.SaveFileDialog1.FileName = "doc1"   
 19'   
 20'SaveFileDialog2   
 21'   
 22Me.SaveFileDialog2.FileName = "doc1"   
 23'   
 24'Button2   
 25'   
 26Me.Button2.Location = New System.Drawing.Point(160, 192)   
 27Me.Button2.Name = "Button2"   
 28Me.Button2.Size = New System.Drawing.Size(224, 56)   
 29Me.Button2.TabIndex = 1   
 30Me.Button2.Text = "build dataase"   
 31'   
 32'Form1   
 33'   
 34Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
 35Me.ClientSize = New System.Drawing.Size(592, 286)   
 36Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1})   
 37Me.Name = "Form1"   
 38Me.Text = "Form1"   
 39Me.ResumeLayout(False) 
 40
 41End Sub 
 42
 43#End Region 
 44
 45Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
 46Dim str1 As String = "d:\mrfu\newaccess"   
 47Dim str2 As String = "newAccess" 
 48
 49If File.Exists("d:\mrfu\newmdb.mdb") Then   
 50MsgBox("the file have exists !")   
 51Exit Sub 
 52
 53End If   
 54If CreateAccessDB(str1) Then   
 55MsgBox("sucess!")   
 56Else   
 57MsgBox("failed!") 
 58
 59End If   
 60'me.SaveFileDialog1 
 61
 62End Sub 
 63
 64Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
 65
 66With Me.SaveFileDialog1   
 67.AddExtension = True 
 68
 69.DefaultExt = "mdb"   
 70.CheckPathExists = True   
 71.Filter = "(Access数据库)*.mdb|*.mdb"   
 72.OverwritePrompt = True 
 73
 74.FilterIndex = 1   
 75End With 
 76
 77  
 78If Me.SaveFileDialog1.ShowDialog() = DialogResult.OK Then 
 79
 80  
 81filename = Me.SaveFileDialog1.FileName   
 82If filename &lt;&gt; "" Then   
 83MsgBox(filename)   
 84Else   
 85Exit Sub 
 86
 87End If   
 88End If 
 89
 90If File.Exists(filename) Then   
 91MsgBox("the file have exists !")   
 92File.Delete(filename)   
 93MsgBox("had delete!") 
 94
 95End If 
 96
 97filename = filename &amp; ";"   
 98MsgBox(filename) 
 99
100If CreateAccessDB(filename) Then   
101MsgBox("sucess!")   
102Else   
103MsgBox("failed!") 
104
105End If   
106End Sub   
107End Class</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus