给定的键在 vb.net 的字典中不存在 [英] The given key was not present in the dictionary in vb.net
问题描述
尝试连接到 mysql 数据库时,我在 vb.net 中收到此错误:
I'm getting this error in vb.net, when trying to connect to mysql database:
An error occurred creating the form. See Exception.InnerException for details. The error is: The given key was not present in the dictionary.
我正在从文本文件中读取数据库信息(用户、密码、主机、数据库).
I'm reading the database information (user, password, host, database) from a textfile.
Dim FILE_NAME As String = "D:\connection.txt"
Dim objReader As New StreamReader(FILE_NAME)
Public Sub textfileopener()
host = objReader.ReadLine
user = objReader.ReadLine
password = objReader.ReadLine
database = objReader.ReadLine
objReader.Close()
End Sub
然后将其分配给这些变量:
Then assigning it to these variables:
Public host
Public database
Public user
Public password
最后我连接到数据库:
Dim con As New MySqlConnection("Server=" + host + ";Database=" + database + ";Uid=" + user + ";Pwd=" + password + ";")
我认为它没有得到任何值.因为如果我提前分配值,我可以毫无问题地连接:
I think its not getting any values. Because if I assign values right ahead, I can connect without problems:
Public host = "localhost"
Public database = "db"
Public user = "root"
Public password = "pw
但如果我这样做,那么一切都会保持不变.而我根本无法改变任何东西.我的问题是在连接字符串中获取正确的值.我该怎么做?
But if I do it this way then it will all be constant. And I cannot change anything at all. My problem is getting the correct values into the connection string. How do I do that?
更新
System.InvalidOperationException was unhandled
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: The given key was not present in the dictionary."
Source="QueryGenerator"
StackTrace:
at QueryGenerator.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at QueryGenerator.My.MyProject.MyForms.get_Form1()
at QueryGenerator.My.MyApplication.OnCreateMainForm() in C:\Users\Nrew\Documents\Visual Studio 2008\Projects\QueryGenerator\QueryGenerator\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at QueryGenerator.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Collections.Generic.KeyNotFoundException
Message="The given key was not present in the dictionary."
Source="mscorlib"
StackTrace:
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.get_UserID()
at MySql.Data.MySqlClient.NativeDriver.Authenticate()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at QueryGenerator.connection..ctor() in C:\Users\Nrew\Documents\Visual Studio 2008\Projects\QueryGenerator\QueryGenerator\connection.vb:line 22
at QueryGenerator.Form1..ctor() in C:\Users\Nrew\Documents\Visual Studio 2008\Projects\QueryGenerator\QueryGenerator\Form1.vb:line 5
InnerException:
推荐答案
错误来自 MySQL 客户端.它似乎希望您在连接字符串中提供一个您没有提供的特定参数.虽然我对托管 MySQL 驱动程序不够熟悉,无法告诉您缺少什么,但缺少的事实非常清楚.
The error is coming from the MySQL client. It appears that it's expecting you to provide a particular parameter in the connection string that you are not. While I'm not familiar enough with the managed MySQL driver to tell you what's missing, the fact that something is missing is pretty clear.
您可能想尝试在表示一个参数结束的分号和下一个参数的名称之间添加一个空格.像这样:
You may want to try adding a space between the semicolon that signals the end of one parameter and the name of the next parameter. Like this:
Dim con As New MySqlConnection("Server=" + host + "; Database=" + database + "; Uid=" + user + "; Pwd=" + password + ";")
这篇关于给定的键在 vb.net 的字典中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!