C#修改库中存在的连接字符串 [英] C# Modify Connection String that exists inside a library

查看:172
本文介绍了C#修改库中存在的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库,里面只有一个DataSet(MySQL连接器)和一个Connector类。

I have a Class Library, which inside just has a DataSet (MySQL connector) and a Connector class.

我在多个项目中使用这个类连接到数据库,我总是有密码嵌入连接字符串,但现在我需要能够修改这个字符串(出于安全目的),所以我可以让用户使用自己的帐户连接。

I use this class in multiple projects to connect to the database, and I always had the password embedded in the connection string but now I need to be able to modify this string(for security purposes) so I can have the user connect using their own account.

如何修改此连接字符串。

How can I modify this connection string.

我试过下面的

Properties.Settings.Default.DataBaseConnectionString = "String";

但是看来连接字符串是readonly,因为它没有一个setter值。

But it seems that the connection string is readonly becase it doesn't appear to have a setter value.

我也试过下面没有运气

Properties.Settings.Default.DatabaseConnectionString.Insert(
Properties.Settings.Default.DatabaseConnectionConnectionString.Length - 1,
            "Password=dbpassword;");


推荐答案

您可以这样修改:

Properties.Settings.Default["MyConnectionString"] = newCnnStr;

对于还将新值保存到文件的完整解决方案,您需要执行这样的操作:

For a full solution that also saves the new value to the file, you need to do something like this:

    private static void ModifyConnectionStrings()
    {
        // Change the value in the config file first
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        const string newCnnStr = "server=(local);database=MyDb;user id=user;password=secret";
        config.ConnectionStrings.ConnectionStrings["MyProject.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr;
        config.Save(ConfigurationSaveMode.Modified, true);

        // Now edit the in-memory values to match
        Properties.Settings.Default["MyConnectionString"] = newCnnStr;
    }

如果您的数据集在另一个程序集中,该程序集的公共设置。要执行此操作:

If your dataset is in another assembly, you can still do this providing you make the settings for that assembly public. To do this:


  1. 在解决方案资源管理器中右键单击项目,然后点击属性

  2. 点击设置标签。

  3. 访问限制条件下拉菜单更改为公开,保存并关闭。

  1. Right-click the project in the solution explorer and click Properties
  2. Click the Settings tab.
  3. Change the Access Modifier dropdown to "Public", save, and close.

然后你可以这样做(假设另一个项目叫做MyProject.DataLayer):

Then you can do this (assuming the other project is called "MyProject.DataLayer"):

    private static void ModifyConnectionStrings()
    {
        // Change the value in the config file first
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        const string newCnnStr = "server=(local);database=MyDb;user id=user;password=secret";
        config.ConnectionStrings.ConnectionStrings["MyProject.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr;
        config.ConnectionStrings.ConnectionStrings["MyProject.DataLayer.Properties.Settings.MyConnectionString"].ConnectionString = newCnnStr;
        config.Save(ConfigurationSaveMode.Modified, true);

        // Now edit the in-memory values to match
        Properties.Settings.Default["MyConnectionString"] = newCnnStr;
        MyProject.DataLayer.Properties.Settings.Default["MyConnectionString"] = newCnnStr;
    }

这篇关于C#修改库中存在的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆