为什么sql命令不能在vb.net中转换为mysql命令 [英] why sql command cannot be converted to mysql command in vb.net

查看:48
本文介绍了为什么sql命令不能在vb.net中转换为mysql命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently working on connecting my data grid view to my database, but the problem is I can't connect with this error : MySql.Data.MySqlClient.MySqlCommand' cannot be converted to '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

这是我在模块中的连接代码:

this is my connection code in a module:

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

推荐答案

错误信息很清楚 - 您正试图从不兼容的类型分配!SelectCommand 属于 System.Data.SqlClient.SqlCommand 类型,因为您已将 da 定义为 System.Data.SqlClient 类型的变量.SqlDataAdapter.另一方面,您正在创建一个 MySql.Data.MySqlClient.MySqlCommand 类型的变量,这是一种不同的类型,当您尝试执行时无法将其转换为 SqlCommand做作业.

The error message is quite clear - you are trying to assign from an incompatible type! SelectCommand is of type System.Data.SqlClient.SqlCommand because you have defined da as a variable of type System.Data.SqlClient.SqlDataAdapter. On the other hand, you are creating a variable of type MySql.Data.MySqlClient.MySqlCommand, which is a different type and which cannot be converted to SqlCommand when you try to do the assignment.

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

The way to fix this is to change your code to use the corresponding classes from MySql.Data.MySqlClient, like so:

 Dim da As New MySqlDataAdapter

这篇关于为什么sql命令不能在vb.net中转换为mysql命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆