关闭

今天用笨方法合并结构不同的库,郁闷

589人阅读 评论(0) 收藏 举报

 SqlConnection conn = DB.createConn();
        string exec1 = "select book_id,isbn,book_name,author,0 as notepad from t_book";
        string exec2 = "select * from t_book_merge";
        DataSet ds = new DataSet();
        DataSet ds2 = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(exec1, conn);
        SqlDataAdapter sda2 = new SqlDataAdapter(exec2, conn);
        sda.Fill(ds, "t_book");
        sda2.Fill(ds2, "t_book_merge");
        //新定义一表
        DataTable dt = new DataTable("wt");
        //加column
        dc = new DataColumn();
        dc.ColumnName = "isbn";
        dc.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "book_name";
        dc.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "notepad";
        dc.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(dc);

        DataRow dr;
       
        //ds.Merge(ds2);
        for (int i = 0; i < ds.Tables["t_book"].Rows.Count; i++)
        {
            dr = dt.NewRow();
            string strIsbn = ds.Tables["t_book"].Rows[i][1].ToString();
            dr["isbn"] = strIsbn;
            dr["book_name"] = ds.Tables["t_book"].Rows[i][2].ToString();

            for(int j=0;j<ds2.Tables["t_book_merge"].Rows.Count;j++)
            {
               
                if(Convert.ToInt32(strIsbn)==Convert.ToInt32(ds2.Tables["t_book_merge"].Rows[j][1].ToString()))
                {
                   dr["notepad"] = ds2.Tables["t_book_merge"].Rows[j][2].ToString();
                   // dr["notepad"] = "wt";
                }
            }
            dt.Rows.Add(dr);
        }
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
        for (int k = 0; k < dt.Rows.Count; k++)
        {
            for (int t = 0; t < 3; t++)
                Response.Write(dt.Rows[k][t].ToString());
        }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:100387次
    • 积分:1483
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:20篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论