不断抛出异常:
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/