Change Database-Name/Server at runtime

ε祈祈猫儿з 提交于 2019-12-25 14:47:13

问题


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) .

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.


回答1:


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.

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


来源:https://stackoverflow.com/questions/43584439/change-database-name-server-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!