我如何动态地更改连接字符串 [英] how can i dynamically change the connection string

查看:170
本文介绍了我如何动态地更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用vb.net和SQL Server 2005

I am using vb.net and SQL server 2005

我有哪些可以从数据库对象生成数据库类的项目。这是我一个数据集添加到项目,并拖放一个数据库对象到它。这样做的问题是,我必须给其存储在code进行的ConnectionString。这是确定的,而开发应用程序。但运行时我想用另一种ConnectionString中,无论是出于安全考虑,而且由于应用程序的每个用户对应到数据库中的用户(SQL),并须有相应的ConnectionString。有没有一种简单的方法来做到这一点,而不必恢复到普通的所有数据集,并尽一切自己?

I have a project with database classes which are generated from database objects. That is I add a dataset to the project and drag and drop a database object onto it. The problem with this is that I have to give a connectionstring which is stored in the code. This is ok while developing the application. But runtime I want to use another connectionstring, both for security reasons and because each user of the application will correspond to a database user (SQL) and shall have a corresponding connectionstring. Is there an easy way to do this without having to restore to plain all datasets and do everything myself?

推荐答案

存储在app.settings连接字符串

Store your connection string in the app.settings

http://msdn.microsoft.com/en -us /库/ a65txexh(VS.80)的.aspx

您可以将文件添加到您的应用程序调用app.settings。在这里,你可以存储你的连接字符串(它们可以被加密)。要访问他们,你只需要调用以下

You can add a file to your application call app.settings. In here you can store you connection strings (they can be encrypted). To access them you just call the following

昏暗CON的String = ConfigurationManager.ConnectionStrings(MYDB)。ConnectionString的

如果您需要登录明确地用不同的用户名和passwordsjust捕获在应用程序登录的用户名和密码。并建立串起来编程。

If you have to log in explicitly with different user names and passwordsjust capture the username and password at application login. And build the string up programatically.

这方面的一个例子可以是

An example of this may be

  <connectionStrings>
    <add name="myDb" connectionString="MYSERVER;Initial Catalog=MYDB;Persist Security  
Info=True;User ID={username};Password={pwd}"/>
  </connectionStrings>

Dim con As String = ConfigurationManager.ConnectionStrings("myDb").ConnectionString
con = con.Replace("{username}",txtUserName.Text)
con = con.Replace("{pwd}",txtPassword.Text)

'now you can do something with con like hook it up to a dataset

这篇关于我如何动态地更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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