简体   繁体   English

VB.Net是否应将数据库连接字符串与应用程序或用户范围一起存储?

[英]VB.Net Should I Store Database Connection String with Application or User Scope?

All of the examples I've seen set the connection string in app.config at the application level, but application settings are read-only. 我见过的所有示例都在应用程序级别在app.config中设置了连接字符串,但是应用程序设置是只读的。 However, I want to allow the user to update the connection string at run time as a user preference. 但是,我想允许用户在运行时根据用户喜好更新连接字符串。 I've found a workaround to update the connection string as an application-level setting, but there are probably reasons why it's read-only and I feel like I'm implementing a bad practice. 我找到了一种解决方法来将连接字符串更新为应用程序级设置,但是可能由于某些原因,它是只读的,并且我觉得我正在实施一种不好的做法。 Should I set this setting at the user level instead? 我应该在用户级别设置此设置吗?

Could you simply add the variations as separate connectionstring and present the user with a combo to choose from? 您能否简单地将变体添加为单独的连接字符串,并向用户展示可供选择的组合?

If you really need to build everything dinamically then take a look at SqlConnectionBuilder class 如果您真的需要构建所有东西,那么请看SqlConnectionBuilder

This example is the same one presented in the link above 此示例与上面的链接中显示的示例相同

Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder
builder("Data Source") = "(local)"
builder("Integrated Security") = True
builder("Initial Catalog") = "AdventureWorks"
Console.WriteLine(builder.ConnectionString)

You kind of have a little problem. 你有点问题。 If you want all users to have the same list of connection strings, then you need a storage common to all which is usualy the database. 如果希望所有用户具有相同的连接字符串列表,则需要所有数据库的公用存储。

If you want to store it per user, then Isolated Storage , local .xml config or the My.Settings . 如果要按用户存储它,则使用隔离存储 ,本地.xml配置或My.Settings

If you want all user to have the same list, then you might want to think of a web service. 如果希望所有用户具有相同的列表,则可能需要考虑Web服务。 Or you could have a "main" connection string, that never changes that will contain the list of the other connection strings. 或者,您可以有一个“主”连接字符串,该字符串永远不会更改,它将包含其他连接字符串的列表。

Sometime, you'll see the .msi (the setup) ask for the connection string. 有时,您会看到.msi(安装程序)要求输入连接字符串。 You grab it and save it at installation. 您抓住它并在安装时将其保存。

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

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