使用数据集合并两个表并在gridview中显示

时间:2013-01-15 08:17:58

标签: c# gridview dataset

我有这两个表

表一 代码标题

KANT-015如何建立品牌

KANT-016避免劳动力危机

KANT-017创造成功的社交媒体策略

KANT-028准备领导

KANT-029基于价值观的领导者

表二

CODE

KANT-015

KANT-016

KANT-017

KANT-028

KANT-029

如何将它们合并到一个数据集中以在一个网格视图中显示它们

这是我到目前为止所做的事情

conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;
                                Data Source =" + Server.MapPath("App_Data\\LR Product Database 2000.mdb"));
        conn.Open();

        setOleDb = new DataSet();
        OleDbDataAdapter dbaOle = new OleDbDataAdapter("SELECT * FROM tblProducts", conn);

        dbaOle.Fill(setOleDb);
        sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["LRVWebsite"].ToString());
        sqlCon.Open();
        dsSql = new DataSet();

        SqlDataAdapter dba = new SqlDataAdapter(@"SELECT C.CustomerFirstName,C.CustomerLastName, C.CustomerCompany,C.CustomerPosition,C.CustomerCountry,C.CustomerProvince,C.CustomerContact,CP.ActionDate,CP.ProductCode,CP.CustomerEmail FROM tblCustomers C INNER JOIN tblCustomerProducts CP ON C.CustomerEmail = CP.CustomerEmail ORDER BY ActionDate DESC", connString);
        //@"SELECT C.CustomerFirstName,C.CustomerLastName,C.CustomerCompany,C.CustomerPosition,C.CustomerCountry,C.CustomerProvince,C.CustomerContact,CP.ActionDate,CP.ProductCode,CP.CustomerEmail FROM tblCustomers C INNER JOIN tblCustomerProducts CP ON C.CustomerEmail = CP.CustomerEmail ORDER BY ActionDate DESC", connString);
        dba.Fill(dsSql);

        dsSql.Merge(setOleDb);

        GridView1.DataSource = dsSql;
        GridView1.DataBind();


        sqlCon.Close();

1 个答案:

答案 0 :(得分:1)

你错过了PK,合并发生在PK上。

在两个表上设置一个:

setOleDb.Tables[0].PrimaryKey = setOleDb.Tables[0].Columns["CODE"];