连接数据源之间的选择 C#

发布于 2024-10-26 07:15:58 字数 537 浏览 6 评论 0原文

我的表单上有一个 dataAdapter、dataSet 和 BindingSource,用于连接到 SQL 数据库以显示表单中的数据,一切正常。但是,我需要有两个连接服务器的选项。 例如 ISSP\SQLEXPRESS 和 MY-WEB。 我不知道该怎么做,想知道是否有人可以给我一些帮助,从哪里开始?谢谢 :) 但是当使用变量时它说它无法连接。

我正在使用下面的代码,但它说它无法连接,所以我想知道我是否引用了错误的变量?

sqlConnectionNW.ConnectionString = "Data Source=@server;Initial Catalog=Northwind;Integrated Security=True";

当我将代码更改为以下内容时,它可以完美运行。

sqlConnectionNW.ConnectionString = "Data Source=ISSP\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";

I've got a dataAdapter, dataSet and BindingSource on my forms to connect to an SQL database to show the data in the form, and it all works fine. However, I need to have 2 options of what server to connect to.
e.g ISSP\SQLEXPRESS and MY-WEB.
I don't know how to do this and wondered whether anyone could give me some help on where to start? Thanks :)
But when using the variables it says that it can't connect.

I'm using the code below but it says that it can't connect, so i'm wondering whether i'm refering to the variable wrong?

sqlConnectionNW.ConnectionString = "Data Source=@server;Initial Catalog=Northwind;Integrated Security=True";

When I change the code to the following, it works perfectly.

sqlConnectionNW.ConnectionString = "Data Source=ISSP\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

复古式 2024-11-02 07:15:58

首先将两个连接设置添加到您的应用程序配置文件中:

<connectionStrings>
    <add name="Test"
     connectionString="Data Source=ISSP\SQLEXPRESS;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
     providerName="System.Data.SqlClient" />
    <add name="Production"
     connectionString="Data Source=MY-WEB;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
     providerName="System.Data.SqlClient" />
</connectionStrings>

在您的应用程序中,将一个组合框添加到表单中(在本例中称为 uiConnection),然后添加以下代码来填充它:

uiConnection.ValueMember = "Name";
foreach (ConnectionStringSettings con in ConfigurationManager.ConnectionStrings)
    {
        uiConnection.Items.Add(con);
    }

您现在可以使用下拉列表来指定要连接的数据库到。当您获取数据时,请执行以下操作以获得正确的连接字符串:

ConnectionStringSettings connection = uiConnection.SelectedItem as ConnectionStringSettings
string queryString = "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");

Firstly add both connection settings to your app config file:

<connectionStrings>
    <add name="Test"
     connectionString="Data Source=ISSP\SQLEXPRESS;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
     providerName="System.Data.SqlClient" />
    <add name="Production"
     connectionString="Data Source=MY-WEB;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
     providerName="System.Data.SqlClient" />
</connectionStrings>

In your application add a ComboBox to the form and (called uiConnection in this example) and add the following code to populate it:

uiConnection.ValueMember = "Name";
foreach (ConnectionStringSettings con in ConfigurationManager.ConnectionStrings)
    {
        uiConnection.Items.Add(con);
    }

You can now use the drop down to specify which database to connect to. When you fetch your data do the following to get the correct connection string:

ConnectionStringSettings connection = uiConnection.SelectedItem as ConnectionStringSettings
string queryString = "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");
﹏雨一样淡蓝的深情 2024-11-02 07:15:58

如果两者的数据相同,那么我认为最简单的方法是仅更改连接字符串并在选择其他选项时切换它。

例子:
数据源=“服务器\DBInstance”;初始目录=“数据库名称”;用户ID=“用户”;密码=“密码”;

If the data is the same on both then I think the easiest way is to just change the connection string and switch it when the other option is selected.

Example:
Data Source="Server\DBInstance";Initial Catalog="DatabaseName";User ID="user";Password="password";

没有伤那来痛 2024-11-02 07:15:58

采用 SqlConnection 的 DataAdapter 应该存在重载。您可以手动创建它,并在连接时将其传递到数据适配器以选择不同的连接。

如果您查看 dataset.designer.cs 并找到构造函数,您将看到这是默认构造函数在应用程序设置中执行的操作。

There should be an overload on the DataAdapter which takes an SqlConnection. You can create this manually and pass it into your data-adapter when you connect to choose different connections.

If you look into your dataset.designer.cs and find the constructor, you will see that this is what the default constructor is doing from your appsettings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文