使用 app.config 文件作为连接字符串,并将其从用户那里抽象出来

发布于 2024-11-17 08:14:14 字数 423 浏览 1 评论 0原文

我在我的 winform 应用程序中使用了 app.config 文件,该文件用于存储连接字符串,我的应用程序中的所有表单都使用该连接字符串来连接到远程 MySQL 数据库。

但是当我在客户的电脑上安装该应用程序时,我希望他们看不到 app.config 文件。是否可以?如何?请帮忙写代码。

另外,是否还有其他方法来创建所有表单都可以访问的连接字符串。比如,我可以创建一个类connection.cs,然后在每个表单中继承它吗? 但如何做到这一点呢?我的意思是如何实现代码

我的主要目标是只创建一个用于连接的字符串,这样,如果我一次又一次地更改它,那么我不需要去每个表单中更改它,相反,我只会在一个 File ,它会被所有表单使用。

使用 app.config 文件是一个好的选择还是制作一个 connection.cs 文件更好?

I have used app.config file for my winform application , the file is used to store the connection string which is used by all the Forms in my application to connect to a remote MySQL database.

But when I install the application on my customer's PCs, then I want that they could not see the app.config file. Is it possible? How? Please help with code.

Also, is there any other way, to create a connection string which is accessible by all the Forms. Like, can I make a class connection.cs and then inherit it in every Form.
But how to do this? I mean how to implement the code

My main objective is to create just one string for connection, so that , if i change it again and again, then i need not go and change it every Form, instead, i would just change it only in one File , and it would be used by all the Forms

Is using app.config file a good option or making a connection.cs file's a better one?

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

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

发布评论

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

评论(5

俯瞰星空 2024-11-24 08:14:14

您不需要在每个表单中使用连接字符串,您需要一个数据访问层,然后从任何地方使用它,理论上只能从称为业务逻辑的另一层使用......

需要将数据加载到网格中的表单、下拉列表或其他一些控件应该消耗应用程序堆栈中较低层加载的数据。

阅读有关 3 层架构的内容。

You don't need to use a connection string from every form, you need a data access layer and then you use it from everywhere, in theory only from another layer called business logic...

A form which needs to load data into a grid, a drop down or some other controls should consume data loaded by lower layers in your application stack.

Read something about 3 tier architecture.

丑疤怪 2024-11-24 08:14:14

app.config 在用户计算机上始终可见,因此您不应将其中存储的任何信息视为机密。

您实际上有两个选择:

  • 继续将连接字符串存储在 app.config 中,但对其进行加密。如果它是一个内部应用程序并且安全性不是太大问题,那么这将工作得很好。但由于加密密钥必须存储在应用程序中,专门的黑客可以检索它。

  • 按照建议使用三层架构。这样,连接字符串存储在中间层,而您的应用程序不再直接连接到数据库,而是通过中间层连接。然后可以使用每个用户的用户名/密码或使用 Windows 身份验证来完成身份验证。连接字符串存储在服务器上,只有有权访问该服务器的人才能查看它并查看数据库连接字符串。

The app.config is always visible on the user machine, so you should not treat any information stored in it as secret.

You really have two options:

  • Continue to store the connection string in the app.config but encrypt it. This will work fine if its an internal app and security is not to much of an issue. But since the encryption key has to be stored in the app a dedicated hacker could retrieve it.

  • use a three tier architecture as suggested already. With this the connection string is stored in the middle tier, while your application no longer connects directly to the database but rather through the middle tier. Authentication can then be done with a user name/password per user or by making use of windows authentication. The connection string is stored on a server and only people with acces to this server can look at it and see the DB connection string.

廻憶裏菂餘溫 2024-11-24 08:14:14

如果您只想要一个简单的解决方案,为什么不在文件 connection.cs 中创建一个名为“Connection”的类,并让它具有一个名为“ConString”的静态属性或属性,它保存连接字符串:

public class Connection
{
  public static ConString = "your connection string here";
}

然后您可以访问它无处不在:

OdbcConnection conn = new OdbcConnection(Connection.ConString);

但这只是一种快速而肮脏的方法(尽管它有效)。创建自己的数据库层会更好 - 但也会做更多的工作。

If you just want a simple solutions why not create a class named for example "Connection" in a file connection.cs and let it have a static attribute or property named for example "ConString" which holds the connection string:

public class Connection
{
  public static ConString = "your connection string here";
}

Then you can access it everywhere:

OdbcConnection conn = new OdbcConnection(Connection.ConString);

BUT that would only be the quick and dirty way of doing it (although it works). It would be much nicer to create an own Database-Layer - but also much more work.

伴我老 2024-11-24 08:14:14

App.config 无法隐藏在用户计算机上,这是您可以做的。
您可以加密连接字符串并将其存储在 app.config 中。看看这篇文章,它向您展示了如何做到这一点。

App.config can't be hidden on users machine, this is what you can do.
You can encrypt the connection string and store it in the app.config. have a look on this article, it shows you how to do that.

风吹雪碎 2024-11-24 08:14:14

尝试在 [statThread] 之前的 program.cs 中定义连接字符串,方法是将其存储在公共静态字符串变量(如 constr 等)中。然后您可以在任何引用的地方使用该 var:
程序构造

Try to define your connection string in program.cs before [statThread] by storing it in a public static string variable like constr etc. Then u can use that var anywhere referencing:
program.constr

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