Change Database-Name/Server at runtime

2019-09-14 16:32发布

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条回答
够拽才男人
2楼-- · 2019-09-14 17:07

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
查看更多
登录 后发表回答