如何连接两个表并在一个列表视图中显示它们?

时间:2013-11-28 04:47:08

标签: sql winforms sqlite listview

我正在使用列表视图,它有两个不同的数据库表格。这是我正在尝试的代码:

try
{
   String Query = "select id,Code,Description,Rate,Bottles,Supply,Empty,Amount,Received,
        Customer_New.Opening_Date,Customer_New.Clients_Title,Customer_New.Cust_Id 
         from (Items INNER JOIN Customer_New on Customer_New.Cust_Id=Items.Cust_Id)
                ,Customer_New";
    SQLiteDataAdapter  dba = new SQLiteDataAdapter(Query, GlobalVars.conn);
    DataSet testDs = new DataSet();
    dba.Fill(testDs, "Items");    //error
    dba.Fill(testDs, "Customer_New");   //error
    DataTable dt = testDs.Tables[0];
    this.lvcmodify.DataContext = testDs.Tables[0].DefaultView;
    lvcmodify.ItemsSource = dt.DefaultView;
    testDs.Dispose();
    dba.Dispose();
    dt.Dispose();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

有谁知道如何在列表视图中完成此功能? 。

在sqlite中是否有可能在运行时将两个表合并到逻辑表中并对db进行任何更改,以便我可以在命令中使用该单个表:dba.Fill(testDs," Items") ;? 请帮我纠正这段代码。谢谢

1 个答案:

答案 0 :(得分:0)

在查找源代码之后,我认为问题是你的sql包括Customer_New两次

只需删除上一个Customer_New表。

try
{
    Query = "select id,Code,Description,Rate,Bottles,Supply,Empty,Amount,Received,
       Customer_New.Opening_Date,Customer_New.Clients_Title,Customer_New.Cust_Id 
         from Items INNER JOIN Customer_New on Customer_New.Cust_Id=Items.Cust_Id";
    dba = new SQLiteDataAdapter(Query, GlobalVars.conn);
    testDs = new DataSet();
    dba.Fill(testDs, "Items");
...
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}