Public Sub AddNewAccess()
Dim cat As Catalog = New Catalog()
Dim apppath As String = Application.StartupPath
apppath = apppath & "\源文件\"
Dim cn As ADODB.Connection = New ADODB.Connection '定义连接
Dim objTable As ADOX.Table = New ADOX.Table '定义新表
cn.Open("Provider=Microsoft.ace.OLEDB.12.0;Data Source=" & "源文件\" & "NewMDB.accdb") '链接到数据库
Dim rs As New ADODB.Recordset
rs = cn.Execute("测试项目表")
If Err.Number = 0 Then
If MsgBox("此表已经存在,是否删除?", vbYesNo , "提示") = vbYes Then
cn.Execute("DROP TABLE 测试项目表") '---------此处不行
Else
End If
cat = Nothing
cn.Close()
cn = Nothing
End If
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cn As New OleDbConnection cn.ConnectionString = "Provider=Microsoft.ace.oledb.12.0;Data Source=你的数据库路径+文件名" Try '如果不出错,就删除表 cn.Open() Dim cmd As OleDbCommand cmd = New OleDbCommand("测试项目表", cn)
= CommandType.TableDirect Dim myread As OleDbDataReader = cmd.ExecuteReader() MsgBox("数据库中已经有这个表,即将删除。") cmd.Dispose() '释放相关资源 myread.Close() '一定要关闭已打开的对象 = CommandType.Text cmd = New OleDbCommand("Drop TABLE 测试项目表", cn) myread = cmd.ExecuteReader() Catch ex As Exception '如果出错,说明没有这个表 MsgBox(Err.Number & Err.Description)'这里弹出对话框告诉你什么原因出错 MsgBox("数据库中可能没有这个表") '这里可以创建表 End Try If cn.State = ConnectionState.Open Then cn.Close() End Sub