English 中文(简体)
B. 联系数据C#
原标题:Choice Between Connection DataSource C#

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

我用下面的法典说,它能够连接起来,这样一来就不问一米是否指变错?

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

当我把法典修改到以下时,它就完美地运作。

sqlConnectionNW.ConnectionString = "Data Source=ISSPSQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";
最佳回答

首先,在您的议事录中增加两个链接环境:

<connectionStrings>
    <add name="Test"
     connectionString="Data Source=ISSPSQLEXPRESS;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>

在你的申请中,在表格和(例如所谓的“ui”)中添加一个ComboBox,并添加以下法典以充实:

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");
问题回答

如果两者的数据相同,那么我认为最容易的办法就是在选择其他选择时,只改变连接线的设置和开关。

Example: Data Source="ServerDBInstance";Initial Catalog="DatabaseName";User ID="user";Password="password";

应在数据Adapter上卸载,而后者需要SqlConnection。 如果你能够选择不同的联系,你可以人工制作,并把它输入你的数据。

如果看你的数据集,即设计者、设计师和施工者,你就会看到,这是违约建筑者在你的评估中做的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签