使用相同的应用程序C#从不同的数据库中选择值 [英] Select values from diferent db with same app C#

查看:95
本文介绍了使用相同的应用程序C#从不同的数据库中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的应用



两个数据库:

1)2017(表名合作伙伴(id,name))

2)2018(表名合作伙伴(id,name))



表格1上有用户名,密码,按钮登录和组合框。

在Form 2上空的partnerdatagrid



我需要在combobox中选择2017年2017年数据库显示数据(表名合作伙伴(id,name)) ),

如果选择值2018来显示来自

I have some simple app

Two database:
1) 2017 (table name partners(id, name))
2) 2018 (table name partners(id, name))

On Form 1 have username, password, button login and combo box.
On Form 2 empty partnerdatagrid

I need when in combobox chose value 2017 show data from database 2017 (table name partners(id, name)),
if choose value 2018 to show data from

2018 (table name partners(id, name))





一些帮助吗?



我尝试过:



Combobox值在下一个方向找到





Some help?

What I have tried:

Combobox value are find on next way

InitializeComponent();
     comboBox1.DataSource = GetDatabaseList();













public List<string> GetDatabaseList()
        {
            List<string> list = new List<string>();

            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();

                // Set up a command with the given query and associate
                // this with the current connection.
                using (SqlCommand cmd = new SqlCommand("SELECT name from sys.databases where name like '20%'", con))
                {
                    using (IDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            list.Add(dr[0].ToString());
                        }
                    }
                }
            }
            return list;

        }

推荐答案

一般说明:

停止猴子编程并开始思考!



首先......

一个想法将合作伙伴(保持合作伙伴的详细信息)存储到两个不同的数据库是个坏主意!,因为......

a你因为一套理由而失去了对数据库的控制权(新伙伴身份等),



b)(如果不是'a`那么......)你需要每年在新数据库中复制合作伙伴详细信息。



其次...

如果你需要在同一服务器上放置的两个不同数据库之间复制数据,您不需要更改连接字符串,因为您已经连接到此服务器。您需要更改 SELECT 语句:

General note:
Stop "monkey-programming" and start thinking!

First of all...
An idea to store partners (keep partners details) into two different databases is bad idea!, because of...
a) you loose control over your database for set of reason (new partner id, etc.),
or
b) (if not `a` then...) you need to duplicate partners details every year in new database.

Second of all...
If you need to copy data between two different databases placed on the same server, you don't need to change connection string, because you already are connected to this server. You need to change SELECT statement:
SELECT <<ListOfFields>>
FROM Database2.Partners;



注意:你必须有权这样做!

如果需要在两个不同服务器上放置的两个不同数据库之间复制数据,可以使用链接服务器(数据库引擎) [ ^ ]能够从外部sql server访问数据。



最后......

请阅读 DAL [ ^ ]和 BLL [ ^ ]。


Note: you have to have permission to do that!
If you need to copy data between two different databases placed on two different servers, you can use Linked Servers (Database Engine)[^] to be able to access data from outside sql server.

Finally...
Please, read about DAL[^] and BLL[^].


只需连接2即可离子;每个都有自己的连接字符串。



连接需要时间;在启动时打开;无论何时通过/拿走你需要的那个。



当不再需要连接时,在关键点/间隔添加关闭/处置方法。



SqlConnection _con1 ...

SqlConnection _con2 ...



列表< sqlconnection> ; _connections = new List< ...>(){_ con1,_con2};



等。
Just make "2" connections; each with their own connection string.

It takes time to "connect"; open both at startup; pass / take the one you need whenever.

Add a "close / dispose" method at a key point / interval when the connections are no longer needed.

SqlConnection _con1 ...
SqlConnection _con2 ...

List<sqlconnection> _connections = new List<...>() { _con1, _con2 };

etc.


不肯定你的问题在这里。首先,我会做一些假设... 1)你使用的是WinForm 2),例如,提供2017和2018选项的组合框被命名为 comboBox2



在这种情况下,登录完成后,我会执行以下操作。根据 comboBox2.SelectedIndex 的值选择连接字符串。例如:

Not positive on your question here. First, I'll make a few assumptions...1) you are using WinForm 2) for the sake of example, the combo box that presents the choices 2017 and 2018 is named comboBox2.

In that case, after your login is complete, I'd do the following. Select a connection string based on the value of comboBox2.SelectedIndex. For example:
string cs = comoBox.SelectedIndex == 1 ? connectionString2018 : connectionString2017;



然后,我会为你的另一个组合框设置数据源,如下所示(传递cs作为参数):


Then, I would set the data source for your other combo box as follows (passing cs as a parameter):

comboBox1.DataSource = GetDatabaseList(cs);



或者,如果2017/2018组合框出现在其他地方,您也可以订阅 SelectedIndexChanged 事件并在该事件中初始化 DataSource (如前所述)。



更新:

我现在注意到我误读了你的问题,你的 GetDatabaseList 函数实际上是获取了数据库名称,用于填充数据库选择列表。



如果您真的只有2017和2018年的可能性,请在组合框中对其进行硬编码并选择连接字符串如上所述。



如果您需要支持随机数量的数据库,您可以像当前一样填充数据库列表执行并订阅 SelectedIndexChanged 事件以(以某种方式)获取该数据库的连接字符串。



我会认为这是一个糟糕的设计。如果您有更多灵活性,请考虑转移到不是特定年份的单个数据库。然后,添加一个新表 PartnersByYear ,其中包含列和 partnerId 专栏。



然后,使用相当基本的 INNER JOIN partnersByYear 和合作伙伴您可以选择他们参与的年度合作伙伴,使用组合框获取<$ c $中的年份c> WHERE 子句。这比维护一堆独立的数据库要麻烦得多。


Alternatively, if the 2017/2018 combo box appears elsewhere, you could also subscribe to the SelectedIndexChanged event and initialize the DataSource (as described previously) in that event.

UPDATE:
I notice now that I have misread your question and that your GetDatabaseList function is actually getting the list of database names, to populate your database selection list.

If you really only have the possibility of 2017 and 2018, hard code them in the combo box and select a connection string as described above.

If, instead, you need to support a random number of databases, you can populate the database list as you currently do and subscribe to the SelectedIndexChanged event to (somehow) get a connection string for that database.

I would consider this a poor design. If you have a bit more flexibility, consider moving to a single database that is not year-specific. Then, add a new table PartnersByYear that has a year column and a partnerId column.

Then, with a fairly basic INNER JOIN, between partnersByYear and partners you can select the partners for the year that they participate, using a combo box to get the year in the WHERE clause. This would be a lot less cumbersome than maintaining a bunch of separate databases.


这篇关于使用相同的应用程序C#从不同的数据库中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆