Checking if table already exists, but it's not working: receiving error “Value cannot be null. Parameter name: dataTable”.
here is the code
Sub TABLES_CHK()
Try
tbl_lbl.Text = "TABLES EXISTS: " & DoesTABLEExist("server=localhost; username=root; password=*****; database=*****;")
If DoesTABLEExist("server=localhost; username=root; password=*****; database=*****;") = False Then
Exit Sub
Using CON As New MySqlConnection("server=localhost; username=*****; password=*****; database=*******;")
Using adapter As New MySqlDataAdapter("CREATE TABLE `airtech_db`.`employees`(" &
"`Employee_ID` INT Not NULL AUTO_INCREMENT,`Name` VARCHAR(45) NULL,FATHER_NAME VARCHAR(45) NULL," &
"DOB VARCHAR(45) NULL,DOJ VARCHAR(45) NULL,POSITION VARCHAR(45) NULL,EMP_STATUS VARCHAR(45) NULL," &
"SALARY VARCHAR(45) NULL,EOS VARCHAR(45) NULL,REMARKS VARCHAR(45) NULL," &
"PRIMARY KEY(`Employee_ID`))", CON)
CON.Open()
Dim dt As New DataTable
adapter.Fill(dt)
CON.Close()
End Using
End Using
End If
tbl_lbl.Text += " -TABLES CREATED: " & DoesDBExist("server=localhost; username=root; password=*****;")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Function DoesTABLEExist(DBConnectionString As String) As Boolean
Try
Dim conn As MySqlConnection = New MySqlConnection(DBConnectionString)
Dim ADAPTER As New MySqlDataAdapter("Select * From information_schema.tables" &
" Where table_schema = 'airtech_db' And table_name = 'employees' LIMIT 1;", conn)
Dim DT As DataTable, TABLECHK As Boolean
conn.Open()
ADAPTER.Fill(DT)
MsgBox(DT.Rows.Count)
If DT.Rows.Count > 0 Then
TABLECHK = True
Else
TABLECHK = False
End If
conn.Close()
Return TABLECHK
Catch ex As Exception
MsgBox(ex.Message)
End If
End Try
End Function
Temporairily remove the Try
so you can see what line is causing the error.
Database objects that expose a .Dispose
method expect that this will be called because they may be using unmanaged resources that need to be released. This happens in their .Dispose
method. A Using...End Using
block will handle this for you even if there is an error.
There is no need to create a DataAdapter
or a DataTable
. Do not pull data from the server that you don't need. You only need the count. Use .ExecuteScalar
which returns the the first column of the first row of the result set as an object.
Function DoesTABLEExist(DBConnectionString As String) As Boolean
Dim RetVal As Integer
Using conn As New MySqlConnection(DBConnectionString),
cmd As New MySqlCommand("Select Count(*) From information_schema.tables
Where table_schema = 'airtech_db'
And table_name = 'employees';", conn)
conn.Open()
RetVal = CInt(cmd.ExecuteScalar())
End Using
Dim TABLECHK As Boolean
If RetVal > 0 Then
TABLECHK = True
Else
TABLECHK = False
End If
Return TABLECHK
End Function
to check if table already exists, this code worked perfectly
Sub TABLES_CHK()
Dim CON_STRING As String
CON_STRING = "server=localhost; username=root; password=****; database=****;"
Try
tbl_lbl.Text = "TABLES EXISTS: " & DoesTABLEExist(CON_STRING)
If DoesTABLEExist(CON_STRING) = False Then
Using CON As New MySqlConnection(CON_STRING)
Using adapter As New MySqlDataAdapter("CREATE TABLE `airtech_db`.`employees`(" &
"`Employee_ID` INT Not NULL AUTO_INCREMENT,`Name` VARCHAR(45) NULL,FATHER_NAME VARCHAR(45) NULL," &
"DOB VARCHAR(45) NULL,DOJ VARCHAR(45) NULL,POSITION VARCHAR(45) NULL,EMP_STATUS VARCHAR(45) NULL," &
"SALARY VARCHAR(45) NULL,EOS VARCHAR(45) NULL,REMARKS VARCHAR(45) NULL," &
"PRIMARY KEY(`Employee_ID`))", CON)
CON.Open()
Dim dt As New DataTable
adapter.Fill(dt)
CON.Close()
End Using
End Using
tbl_lbl.Text += " -TABLES CREATED: " & DoesDBExist(CON_STRING)
Else
tbl_lbl.Text += " -TABLES CREATED: False"
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Function DoesTABLEExist(DBConnectionString As String) As Boolean
Try
Dim conn As MySqlConnection = New MySqlConnection(DBConnectionString)
' Dim ADAPTER As New MySqlDataAdapter("Select * From information_schema.tables" &
' " Where table_schema = 'airtech_db' And table_name = 'employees' LIMIT 1;", conn)
Dim cmd As New MySqlCommand("Select * From information_schema.tables Where table_schema = 'airtech_db' And table_name = 'employees' LIMIT 1;", conn)
conn.Open()
Dim bRet As Boolean = False
Dim reader As MySqlDataReader = cmd.ExecuteReader
bRet = reader.HasRows
If (bRet = True) Then
'MsgBox("Table Exists")
Else
' MsgBox("No more records")
End If
conn.Close()
Return bRet
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.