C# 修改库中存在的连接字符串

发布于 2024-08-28 04:11:07 字数 520 浏览 3 评论 0原文

我有一个类库,里面只有一个数据集(MySQL 连接器)和一个连接器类。

我在多个项目中使用此类来连接到数据库,并且我始终将密码嵌入在连接字符串中,但现在我需要能够修改此字符串(出于安全目的),以便我可以让用户使用自己的连接进行连接帐户。

我怎样才能修改这个连接字符串。

我已尝试以下操作

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

,但似乎连接字符串是只读的,因为它似乎没有设置器值。

我也尝试了以下但没有运气

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

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.

I have tried the following

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

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

I also tried the following with no luck

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何必那么矫情 2024-09-04 04:11:07

您可以这样修改它们:

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

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

    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;
    }

如果您的数据集位于另一个程序集中,则只要您为该程序集进行设置,您仍然可以执行此操作民众。为此,请执行以下操作:

  1. 在解决方案资源管理器中右键单击该项目,然后单击属性
  2. 单击设置选项卡。
  3. 访问修饰符下拉列表更改为“公共”,保存并关闭。

然后你可以这样做(假设另一个项目名为“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;
    }

You can modify them like this:

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. 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.

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;
    }
毁虫ゝ 2024-09-04 04:11:07

你没有这个类的源代码吗?

另外,但更复杂一点的方法是使用 修补程序集 ReflectorReflexil 插件

Don't you have the source code of that class?

Also, but a little more complicated method, would be to patch the assembly using Reflector with the Reflexil AddIn.

携余温的黄昏 2024-09-04 04:11:07

我认为您是在问如何在运行时根据使用该应用程序的用户更改连接字符串属性。希望这有帮助。

过去,我通过使连接字符串包含可以使用 string.Format 提供的参数来完成此操作。

    <connectionStrings>
        <add name="SomeDB" connectionString="("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password={1}"" />
    </connectionStrings>

string connectionString = string.Format(ConfigurationManager.ConnectionStrings["SomeDB"].ConnectionString, location, password);

I think you're asking how you can change connectionstring properties at runtime depending on who is using the application. Hope this helps.

In the past I have done this by making my connection string contain parameters that I can provide using string.Format.

    <connectionStrings>
        <add name="SomeDB" connectionString="("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password={1}"" />
    </connectionStrings>

string connectionString = string.Format(ConfigurationManager.ConnectionStrings["SomeDB"].ConnectionString, location, password);
放肆 2024-09-04 04:11:07

看起来您正在从配置文件加载连接字符串,您应该能够从那里更改它。构建后,它将是一个与编译后的表单名称相同的文件加上 .config。 (例如 application.exe.config)

It looks like you are loading the connection string from the config file, you should be able to change it from there. Once built it will be a file named the same as your compiled form plus .config. (For example application.exe.config)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文