如何在VS2022中设置ODBC连接字符串?

问题描述 投票:0回答:1

我有一个带有组合框的应用程序,用户可以在其中选择他们想要连接的数据库。从 app.config 填充列表工作正常。在 FetchData 函数上,我尝试根据用户选择的数据库设置 ODBC 连接字符串。

            int selectedDB = cbxDatabase.SelectedIndex;
            string connectionstring = dbconnections[selectedDB].ConnectionString;
            /*string connectionstring = Properties.Settings.Default.connString;*/
            MessageBox.Show(selectedDB.ToString());
            MessageBox.Show(connectionstring);

出于某种原因,无论我选择哪个数据库,它都会一直连接到第一个连接字符串。所选索引正确,但连接字符串仍默认为索引 0 中的任何内容。我正在调试器中测试代码。

odbc connection-string visual-studio-2022
1个回答
0
投票

我想通了...

将连接字符串行更改为:

string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings[cbxDatabase.Text].ConnectionString.ToString();
© www.soinside.com 2019 - 2024. All rights reserved.