将两个/几个MS Access Table加载到C#Windows窗体中

时间:2015-10-15 08:54:05

标签: c# winforms ms-access connection-string

我问如何将MS Access表中的两个或多个表连接到c #Windows表单?。

我收到错误,你无法找到第二张表:

public partial class Form1 : Form
{
    private OleDbConnection connection = new OleDbConnection();
    private OleDbConnection connection2 = new OleDbConnection();
    public Form1()
    {
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\be\Documents\MitarbeiterDaten2.accdb;
        Persist Security Info=False;";

        connection2.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\be\Documents\DatenbankAbteilung.accdb;
        Persist Security Info=False;";
        InitializeComponent();
    }

private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select ABTEILUNG from combo";
            command.CommandText = query;

            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Abteilung.Items.Add(reader["ABTEILUNG"].ToString());
            }
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }
        finally
        {
            connection.Close();
        }

有人有解决方案吗? 它就是如何将两个或多个MS Access表连接成C#Windows表单。

2 个答案:

答案 0 :(得分:2)

您可以重复使用这些对象,并可以从各种表或数据库中获取数据,如下所示:

private void Form1_Load(object sender, EventArgs e)
{
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select ABTEILUNG from combo";
            command.CommandText = query;

            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Abteilung.Items.Add(reader("ABTEILUNG").ToString());
            }
            reader.Close(); //' Always Close ther Reader. Don't left it open

            connection2.Open();
            command.Connection = connection2; //' Reusing Same Command Over New Connection
            command.CommandText = "Select Field2 from Table2";
            while (reader.Read)
            {
                if (!(Convert.IsDBNull(reader("Field2")))) //' Checking If Null Value is there
                {
                    Abteilung.Items.Add(reader("Field2").ToString());
                }
            }
            reader.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }
        finally
        {
            connection.Close();
            connection2.Close();
        }
    }

答案 1 :(得分:0)

如何使用using块来处理命令和连接,然后使用DataAdapter轻松完成工作。我使用这样的代码。

DataSet ds = new DataSet();
using (OleDbConnection con = new OleDbConnection(MDBConnection.ConnectionString))
{
    con.Open();
    using (OleDbCommand cmd = new OleDbCommand("SELECT Column FROM Table1", con))
    {
        using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
        {
            da.Fill(ds);
        }
    }

    using (OleDbCommand cmd = new OleDbCommand("SELECT AnotherColumn FROM Table2", con))
    {
        using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
        {
            da.Fill(ds);
        }
    }
}
return ds;