简体   繁体   中英

Changing dataset connectionstring at runtime vs2010

I found a solution that must be for a older version then vs2010. I would like to know how to do this for vs2010? Does anyone know?

http://www.csharpbydesign.com/2008/01/overriding-dataset-settings-co.html

Let me explain little more detail.

I have ac# generated dataset. How can I change the connection string so I can use the dataset with another (identically structured yet differently populated) database? This has to occur at runtime as I do not know the server or database name at compile time. i AM USING vs2010 and SQL Server 2008 R2 Express

I think there is no simple way and you cannot change the Connection-String programmatically for the entire DataSet since it's set for every TableAdapter .

You need to use/create the partial class of the TableAdapter to change the connection-string since the Connection property is internal (if your DAL is in a different assembly). Don't change the designer.cs file since it will be recreated automatically after the next change on the designer. To create it just right-click the DataSet and chose "show code".

For example (assuming the TableAdapter is named ProductTableAdapter ):

namespace WindowsFormsApplication1.DataSet1TableAdapters
{
    public partial class ProductTableAdapter 
    {
        public string ConnectionString {
            get { return Connection.ConnectionString; }
            set { Connection.ConnectionString = value; }
        }
    }
}

Now you can change it easily:

var productTableAdapter = new DataSet1TableAdapters.ProductTableAdapter();
productTableAdapter.ConnectionString = someOtherConnectionString;

Here's a screesnhot of my sample DataSet and the created file DataSet1.cs :

在此处输入图片说明

There's actually a much easier way to change the connection string. Go to the Settings screen, where the connection string is displayed as a connection string. First mark and copy the connection string that's displayed. Then change the type from connection string to string. The text for the string will change to include xml. Then paste the copied connection string over the xml text. Then change the scope from Application to User.

When I want to change the connection string, I use the following code.

// assign the path to use to the variable fullpath (or whatever)
string newConnection = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", fullpath);            
Properties.Settings.Default.HootConnectionString = newConnection;
Properties.Settings.Default.Save();

In my case, I have a global Dataset active, so I have to make that the data is reread by the tableadapter. And, of course, you'll have to add error control to make sure the database is still there.

You'll notice that this will not change what is displayed in Application Settings. Those are defaults.

This works for an Access database; so your mileage and requirements may vary.

EDIT: Caveat. As it works out, when installed, the connection string works well for opening and reading database content, but it complains about not having a connection string when trying to update a database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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