类型为"MySqlConnection"的值无法转换为"字符串"



当我尝试运行我的代码时,它出现了类型'MySqlConnection'的值不能转换为'String'。

我的代码如下所示:

Imports MySql.Data.MySqlClient
Module ConnectionDatabase
Public Function strconnection() As MySqlConnection
Return New MySqlConnection("server=localhost; user id=root; password=; database=value power meter")
End Function
Public strcon As MySqlConnection = strconnection()
Public result As String
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public dt As New DataTable
Public Sub create(ByVal sql As String)
Try
strcon.Open()
With cmd
.Connection = strcon
result = cmd.ExecuteNonQuery
End With
Catch ex As Exception
End Try
End Sub
Public Sub reload(ByVal sql As String, ByVal DTG As Object)
Try
dt = New DataTable
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql
End With
da.SelectCommand = cmd
da.Fill(dt)
DTG.datasource = dt
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
da.Dispose()
End Try
End Sub
Public Sub updates(ByVal sql As String)
Try
strcon.Open()
With cmd
.CommandText = strcon
.CommandText = sql
result = cmd.ExecuteNonQuery
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub
Public Sub delete(ByVal sql As String)
Try
strcon.Open()
With cmd
.CommandText = strcon
.CommandText = sql
result = cmd.ExecuteNonQuery
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub
End Module

你需要修改你的代码:

.CommandText = strcon
.CommandText = sql

改变

.Connection = strcon
.CommandText = sql

完成修正

Public Sub updates(ByVal sql As String)
Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql
result = cmd.ExecuteNonQuery
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub
Public Sub delete(ByVal sql As String)
Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql
result = cmd.ExecuteNonQuery
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub

最新更新