للتحقق من وجود جدول ما في قاعدة بيانات معينة نستخدم الكود التالي :
كود :
Dim Con As New OleDb.OleDbConnection
Dim TableName As String '= "LookForThisDBTable"
Dim DoesTheTableExist As Boolean = False
Private Sub DoesTableExist()
Try
Con.ConnectionString = ("Provider=Microsoft.Ace.Oledb.12.0; Data Source =" & Application.StartupPath &
"\Activities.accdb;Jet OLEDB:Database Password=")
Con.Open()
Dim restrictions(3) As String
restrictions(2) = TableName '
Dim dbTbl As DataTable = Con.GetSchema("Tables", restrictions)
If dbTbl.Rows.Count = 0 Then
'Table does not exist
DoesTheTableExist = False
MsgBox("Nooo")
Else
'Table exists
DoesTheTableExist = True
MsgBox("Yes")
End If
dbTbl.Dispose()
Con.Close()
Con.Dispose()
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TableName = "Activities" ' اسم الجدول المراد التحقق منه
DoesTableExist()
End Sub
للتحقق من وجود حقل ما في جدول معين نستخدم الكود التالي :
كود :
Public Function DoesFieldExist(ByVal cnnStr As String, ByVal tblName As String,
ByVal fldName As String) As Boolean
' For Access Connection String,
' use "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
' accessFilePathAndName
' Open connection to the database
Dim Con1 As New OleDb.OleDbConnection(cnnStr)
Con1.Open()
Dim dbTbl As New DataTable
' Get the table definition loaded in a table adapter
Dim strSql As String = "Select TOP 1 * from " & tblName
Dim dbAdapater As New OleDb.OleDbDataAdapter(strSql, Con1)
dbAdapater.Fill(dbTbl)
' Get the index of the field name
Dim i As Integer = dbTbl.Columns.IndexOf(fldName)
If i = -1 Then
'Field is missing
DoesFieldExist = False
Else
'Field is there
DoesFieldExist = True
End If
dbTbl.Dispose()
Con1.Close()
Con1.Dispose()
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim conStr As String
conStr = "Provider=Microsoft.Ace.Oledb.12.0; Data Source =" & Application.StartupPa
If DoesFieldExist(conStr, "Activities", "Activity") = True Then
MsgBox("Yes2")
Else
MsgBox("No2")
End If
End Sub
لا تنسى تعديل الكود بما يناسب(جملة الاتصال -- اسم الجدول -- اسم الحقل )..