简体   繁体   English

C#Visual Studio使用settings.settings进行mysql连接

[英]c# visual studio using settings.settings for mysql connection

I'm trying to connect to a MySQL database using settings.settings in visual studio 2017. I checked settings.settings and my connection string is there and is set properly (as a connection string and is complete). 我正在尝试使用Visual Studio 2017中的settings.settings连接到MySQL数据库。我检查了settings.settings,我的连接字符串在那里并且设置正确(作为连接字符串并且完整)。 I looked everywhere and tried a bunch of different code to get it to work but it's simply not working. 我到处看,并尝试了许多不同的代码来使其正常工作,但它根本无法正常工作。 I've been at this for way too many hours so your help would be greatly appreciated. 我在这里已经呆了很多小时,因此非常感谢您的帮助。 If you could post the code you would use it would be great. 如果您可以发布代码,则可以使用它。 I am using the MySql.Data.MySqlClient and System.Configuration. 我正在使用MySql.Data.MySqlClient和System.Configuration。 I can connect by inserting the connection string manually but I really want to use the settings since I will need to change this info once the system is installed on location and don't want to change the code on all the forms once there. 我可以通过手动插入连接字符串来进行连接,但是我真的想使用这些设置,因为一旦系统安装到位后就需要更改此信息,并且不想在那里更改所有表单上的代码。

Here is my code: 这是我的代码:

public dbadmin()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
            MySqlConnection con = new MySqlConnection(conSettings.ToString());
            con.Open();
            MessageBox.Show("Connection ok");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }




    }

The connectionstring requested via ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"] gets retrieved from the App.config file instead of Settings.settings . 通过ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"]请求的连接ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"]将从App.config文件而不是Settings.settings检索到。

If you want to keep using ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"] you have to add the connectionstring to App.config as shown below. 如果要继续使用ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"] ,则必须将连接ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"]添加到App.config ,如下所示。
(Provide your values for connectionString and providerName .) (为connectionStringproviderName提供值。)

<configuration>
    <connectionStrings>
        <add name="shopmanagerConnectionString1" connectionString="..." providerName="..." />
    </connectionStrings>
</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM