简体   繁体   English

C# WinForms:自动创建if语句连接到访问数据库

[英]C# WinForms: create automatically if statement connected to access database

I am new to C# and need some guidance!我是 C# 的新手,需要一些指导!

short summary:简短的摘要:

I have an application with few forms that is connected to the Access database.我有一个连接到 Access 数据库的 forms 应用程序。 Everything is working fine.一切正常。 The user can select the needed item from the combo box that is shown in the label on the next form.用户可以从下一个表格的 label 中显示的组合框中 select 所需的项目。 Additionally the elements of the table are shown in the datagridview2 on the same form: Every item from the combo box is connected to a different table in the Access database:此外,表格的元素显示在同一表格的 datagridview2 中:组合框中的每个项目都连接到 Access 数据库中的不同表格:

private void frmData_Load(object sender, EventArgs e)
{
    lblItem.Text = Item;

    string connectionString = null;
    OleDbConnection con;
    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\".\\Database_Example.accdb\"";
    con = new OleDbConnection(connectionString);

    if (lblItem.Text == "X")
    {
        OleDbCommand cmd2 = new OleDbCommand("SELECT ID, Column1, Column2 FROM X", con);
        OleDbDataAdapter sda = new OleDbDataAdapter(cmd2);
        DataTable td = new DataTable();
        sda.Fill(td);
        dataGridView2.DataSource = td;
    }

    if (lblItem.Text == "Y")
    {
        OleDbCommand cmd = new OleDbCommand("SELECT ID, Column1, Column2 FROM Y", con);
        OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
        DataTable td = new DataTable();
        sda.Fill(td);
        dataGridView2.DataSource = td;
    }
}

Now I want to use my application to add new tables to the database.现在我想使用我的应用程序将新表添加到数据库中。 Therefore there are bunch of different approaches on the web.因此在 web 上有很多不同的方法。

Problem: I´m looking for an option to automatically create new if-statements once I´ve added new tables in the database.问题:我正在寻找在数据库中添加新表后自动创建新 if 语句的选项。 For example for items "Z";例如项目“Z”; "A"; “一个”; "B"; “乙”; ... ...

Is there a way to do it?有没有办法做到这一点? Or do I need a different approach?还是我需要不同的方法?

Greetings:)问候:)

We get this question a few times a month and the answer is always the same:我们每个月都会收到几次这个问题,答案总是一样的:

You really need ONE table, with an additional column for your Z , A , B , etc. Make this the first field of the primary key.您确实需要一个表,并为您的ZAB等附加列。将此作为主键的第一个字段。

Now this:现在这个:

if (lblItem.Text == "X")
{
    OleDbCommand cmd2 = new OleDbCommand("SELECT ID, Column1, Column2 FROM X", con);
    OleDbDataAdapter sda = new OleDbDataAdapter(cmd2);
    DataTable td = new DataTable();
    sda.Fill(td);
    dataGridView2.DataSource = td;
}
// etc

Becomes this:变成这样:

var td = new DataTable();
using (var con = new OleDbConnection("connection string here"))
using (var cmd = new OleDbCommand("SELECT ID, Column1, Column2 FROM MyTable WHERE Key = ? ", con))
using (var sda = new OleDbDataAdapter(cmd2))
{
    cmd.Parameters.Add("?", OleDbType.NChar, 3).Value = lblItem.Text;
    sda.Fill(td);
}
 

No if statement required.不需要if语句。

Additionally, do NOT try to re-use your connection object throughout a form instance like that.此外,不要尝试在这样的表单实例中重复使用您的连接 object。 It really is more efficient to create a new connection for each call to the database.为每次调用数据库创建一个新连接确实更有效。

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

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