Mysql 检查表是否已存在,但它';“s不工作接收错误”;值不能为null。参数名称:dataTable";
检查表是否已经存在,但它不工作:接收错误“值不能为null。参数名称:dataTable” 这是密码Mysql 检查表是否已存在,但它';“s不工作接收错误”;值不能为null。参数名称:dataTable";,mysql,vb.net,Mysql,Vb.net,检查表是否已经存在,但它不工作:接收错误“值不能为null。参数名称:dataTable” 这是密码 Sub TABLES_CHK() Try tbl_lbl.Text = "TABLES EXISTS: " & DoesTABLEExist("server=localhost; username=root; password=*****; database=*****;") If DoesTABLEExist("server=localhost;
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
暂时删除
Try
,以便查看导致错误的行
公开.Dispose
方法的数据库对象希望调用该方法,因为它们可能正在使用需要释放的非托管资源。这发生在他们的.Dispose
方法中。即使出现错误,Using…End Using
块也会为您处理此问题
无需创建数据适配器
或数据表
。不要从服务器中提取不需要的数据。你只需要数一数。使用.ExecuteScalar
,将结果集第一行的第一列作为对象返回
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
暂时删除
Try
,以便查看导致错误的行
公开.Dispose
方法的数据库对象希望调用该方法,因为它们可能正在使用需要释放的非托管资源。这发生在他们的.Dispose
方法中。即使出现错误,Using…End Using
块也会为您处理此问题
无需创建数据适配器
或数据表
。不要从服务器中提取不需要的数据。你只需要数一数。使用.ExecuteScalar
,将结果集第一行的第一列作为对象返回
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
为了检查表是否已经存在,这段代码工作得很好
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
为了检查表是否已经存在,这段代码工作得很好
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
这个网站提供了一个问题和答案的预览。如果您在问题看起来像狗的早餐时单击“提交”按钮,您只是不想帮助我们帮助您。错误发生在哪一行?您将需要使用MySqlCommand和ExecuteNonQuery来执行表创建代码。对于某些SQL操作,使用DataAdapter是个坏主意;)请将
“server=localhost;username=root;password=****;”
放入某种可全局访问的字符串中,这样,如果它发生更改,您只需在一个地方更改它,而不需要在很多地方更改。本网站提供了问题和答案的预览,这是有原因的。如果您在问题看起来像狗的早餐时单击“提交”按钮,您只是不想帮助我们帮助您。错误发生在哪一行?您将需要使用MySqlCommand和ExecuteNonQuery来执行表创建代码。对于某些SQL操作,使用DataAdapter是个坏主意;)请将“server=localhost;username=root;password=****;”
放入某种全局可访问的字符串中,这样,如果它发生更改,您只需要在一个位置更改它,而不需要在很多位置更改它。