c# - 无法连接到vb.net中的数据库

标签 c# mysql vb.net

不断抛出异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll

这是代码:

 Public Sub showWorkerFullnames()

    getQuery = "SELECT worker.worker_fullname FROM worker"
    getCommand = New MySqlCommand(getQuery, MySQLConnection)
    getReader = getCommand.ExecuteReader

    cbWorkerFullnames.Items.Clear()

    While getReader.Read

        cbWorkerFullnames.Items.Add(getReader.Item("worker_fullname").ToString)

    End While

最佳答案

This page显示了一个工作示例。

创建连接:

string connStr = 
    "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);

创建命令时使用该连接对象:

MySqlCommand cmd = new MySqlCommand(sql, conn);

使用前打开连接。链接的示例在创建连接后立即打开连接。这是令人吹毛求疵的,但我会在使用它之前将打开连接作为最后一件事。

conn.Open();

完成后不要忘记关闭连接。

关于c# - 无法连接到vb.net中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37233449/

相关文章:

c# - C# 有扩展属性吗?

c# - 为应用程序池 'xxx' 提供服务的进程与 Windows 进程激活服务发生致命通信错误

c# - 对log4net功能的困惑,请帮忙澄清一下?

javascript - window.open 在怪异模式下也会将后续页面更改为怪异模式

vb.net - Excel VBA 和 VB.NET 一样吗

c# - 比较某些 Prop 具有不同格式的对象

c# - 使用 C# 解析 SVG "path"元素 - 是否有库可以执行此操作?

mysql:平均与总和/计数

mysql - 将所有大写值更新为大写

mysql查询需要优化