为什么sql命令无法在vb.net中转换为mysql命令

时间:2015-01-29 02:20:08

标签: mysql vb.net

我目前正在将数据网格视图连接到我的数据库,但问题是我无法连接此错误:MySql.Data.MySqlClient.MySqlCommand'无法转换为'System.Data.SqlClient.SqlCommand

这是我的代码:

Private Sub populateGird()
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim bs As New BindingSource
        Try
            sqlC = New MySqlCommand("SELECT c_id, CONCAT(c_firstname, c_lastname), rank_name, c_bir_status, rank_basic_pay, rank_positional_allowance," &
                                    "rank_meal_allowance, ca_worth FROM tbl_crew, tbl_cash_advance, tbl_rank", conn)
            da.SelectCommand = sqlC '--> error is that sqlC
            da.Fill(ds)
            bs.DataSource = ds
            PayrollGrid.DataSource = bs
            da.Update(ds)
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Dispose()
        End Try

这是模块中的连接代码:

Imports MySql.Data.MySqlClient

Module Module1
Public conn As MySqlConnection

Public Sub connect()
    conn = New MySqlConnection
    conn.ConnectionString = "server=localhost;user=root;database=fat2x_payroll;pwd=;Convert Zero Datetime=True"

    Try
        conn.Open()
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub

End Module

2 个答案:

答案 0 :(得分:0)

将SqlDataAdapter的声明更改为适当的MySQL类型(我猜测MySqlDataAdapter?)

Dim da As New SqlDataAdapter

答案 1 :(得分:0)

错误信息非常明确 - 您正尝试从不兼容的类型进行分配! SelectCommand的类型为System.Data.SqlClient.SqlCommand,因为您已将da定义为System.Data.SqlClient.SqlDataAdapter类型的变量。另一方面,您正在创建类型为MySql.Data.MySqlClient.MySqlCommand的变量,这是一种不同的类型,当您尝试进行分配时,无法将其转换为SqlCommand

解决此问题的方法是更改​​代码以使用MySql.Data.MySqlClient中的相应类,如下所示:

 Dim da As New MySqlDataAdapter