简体   繁体   English

在运行时vs2010更改数据集连接字符串

[英]Changing dataset connectionstring at runtime vs2010

I found a solution that must be for a older version then vs2010. 我找到了一个解决方案,该解决方案必须适用于vs2010的旧版本。 I would like to know how to do this for vs2010? 我想知道vs2010怎么做? Does anyone know? 有人知道吗?

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

Let me explain little more detail. 让我解释更多细节。

I have ac# generated dataset. 我有ac#生成的数据集。 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 我正在使用vs2010和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 . 我认为没有简单的方法,并且您不能以编程方式更改整个DataSet的Connection-String,因为它是为每个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). 由于Connection属性是internal (如果您的DAL在不同的程序集中),因此您需要使用/创建TableAdapter的局部类来更改连接字符串。 Don't change the designer.cs file since it will be recreated automatically after the next change on the designer. 不要更改designer.cs文件,因为它将在设计器上进行下一次更改后自动重新创建。 To create it just right-click the DataSet and chose "show code". 要创建它,只需右键单击数据集并选择“显示代码”。

For example (assuming the TableAdapter is named ProductTableAdapter ): 例如(假设TableAdapter名为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 : 这是我的示例数据集和创建的文件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. 字符串的文本将更改为包括xml。 Then paste the copied connection string over the xml text. 然后将复制的连接字符串粘贴到xml文本上。 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. 就我而言,我有一个活动的全局数据集,因此我必须使数据由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; 这适用于Access数据库。 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. 事实证明,安装后,连接字符串可很好地用于打开和读取数据库内容,但它抱怨在尝试更新数据库时没有连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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