在运行时更改数据库名称/服务器 [英] Change Database-Name/Server at runtime

查看:98
本文介绍了在运行时更改数据库名称/服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio内置的DataSource函数来处理我的应用程序及其数据库。现在我面临一个小问题。如何在最终项目中更改数据库服务器?

I am using Visual Studios build-in DataSource functions, to work with my application and its database. Now I am facing one little problem; How do I change the database-server in the final project?

很显然,最终用户的服务器名称与我的名称不同。

Obviously the end-users server name will not be the same as mine.

我又如何在运行时更改它?我的应用程序具有查找数据库服务器本身的功能,因此它需要能够在运行时更改数据库服务器(仅在应用程序启动时)。

Also how can I change it at runtime? My application has functions to find the database-server itself, so it needs to be able to change the database server at runtime (Only at applcations start) .

更新1:

现在我正在更改 TableAdapter.Connection.ConnectionString .Replace( My Local Server Name, New Server Name)来更改服务器。但是我不认为这是应该这样做的方式。

Update 1:
Right now I am Changing my TableAdapter.Connection.ConnectionString with .Replace("My Local Server Name", "New Server Name") to change the Server. But I don't think that's the way it's supposed to be done.

推荐答案

如果要在部署后更改连接字符串那么您可以手动编辑配置文件,或者如果当前用户是管理员,则可以在代码中进行编辑。

If you want to change the connection string after deployment then you edit the config file by hand or you can do so in code if the current user is an administrator.

如果要更改表适配器的连接字符串在运行时,除了配置文件中的内容外,您确实需要设置 Connection.ConnectionString 属性。最好的方法是使用连接字符串构建器。对于Access数据库,可能看起来像这样:

If you want to change the connection string for a table adapter at run time to something other than what's in the config file then you do indeed need to set the Connection.ConnectionString property. The most advisable way to do that is to use a connection string builder. For an Access database, that might look like this:

Dim builder As New OleDbConnectionStringBuilder(myTableAdapter.Connection.ConnectionString)

builder.DataSource = dataSourceName
myTableAdapter.Connection.ConnectionString = builder.ConnectionString

这篇关于在运行时更改数据库名称/服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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