C# 修改库中存在的连接字符串
我有一个类库,里面只有一个数据集(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以这样修改它们:
对于还将新值保存到文件的完整解决方案,您需要执行以下操作:
如果您的数据集位于另一个程序集中,则只要您为该程序集进行设置,您仍然可以执行此操作民众。为此,请执行以下操作:
然后你可以这样做(假设另一个项目名为“MyProject.DataLayer”):
You can modify them like this:
For a full solution that also saves the new value to the file, you need to do something like this:
If your dataset is in another assembly, you can still do this providing you make the settings for that assembly public. To do this:
Then you can do this (assuming the other project is called "MyProject.DataLayer"):
你没有这个类的源代码吗?
另外,但更复杂一点的方法是使用 修补程序集 Reflector 与 Reflexil 插件。
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.
我认为您是在问如何在运行时根据使用该应用程序的用户更改连接字符串属性。希望这有帮助。
过去,我通过使连接字符串包含可以使用 string.Format 提供的参数来完成此操作。
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.
看起来您正在从配置文件加载连接字符串,您应该能够从那里更改它。构建后,它将是一个与编译后的表单名称相同的文件加上 .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)