.net向另追加表信息

 private void DBind(string pid)
    {
        DataTable dt;
        string strsql = "select * from " + Session["outTable"].ToString() + " where " + Session["filed_id"].ToString() + "=" + pid;
        dt = new cc.DBHepler().RunSql(strsql, CommandType.Text, null, null).Tables[0];
        if (dt.Rows.Count > 0)
        {
           // strsql = "insert into " + Session["inTable"].ToString() + " (name,parent_id)values('" + dt.Rows[0]["name"].ToString() + "'," + Session["pNode"].ToString() + ")";

            string sqlfield = "";
            string sqlvalues = "";

            strsql = "insert into " + Session["inTable"].ToString() + " (";
            for (int i = 0; i < dt.Columns.Count; i++)
            {

                if (dt.Rows[0][i].ToString() != "" && dt.Columns[i].ColumnName.ToLower() != Session["filed_id"].ToString() && dt.Columns[i].ColumnName.ToLower() != Session["filed_pid"].ToString())
                {
                    sqlfield += dt.Columns[i].ColumnName + ",";
                    sqlvalues += "'" + dt.Rows[0][i].ToString() + "',";
                }
            }
            sqlfield += Session["filed_pid"].ToString();
            sqlvalues += Session["pNode"].ToString();
            strsql += sqlfield + ") values(" + sqlvalues + ")";

            new cc.DBHepler().RunSqlRetNull(strsql, CommandType.Text, null, null);//插入第一条数据


            strsql = "select max(" + Session["filed_id"].ToString() + ") from " + Session["inTable"].ToString();
            Session["num"] = null;
            Session["num"] = new cc.DBHepler().RunSqlRetObj(strsql, CommandType.Text, null, null).ToString();//得到被插入表最大ID

            strsql = "select * from " + Session["outTable"].ToString() + " where " + Session["filed_pid"].ToString() + "=" + pid;
            dt = new cc.DBHepler().RunSql(strsql, CommandType.Text, null, null).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
              //  strsql = "insert into " + Session["inTable"].ToString() + " (name," + Session["filed_pid"].ToString() + ")values('" + dt.Rows[i]["name"].ToString() + "'," + Session["num"].ToString() + ")";
              
                sqlfield = "";
                sqlvalues = "";
                strsql = "insert into " + Session["inTable"].ToString() + " (";
                for (int num = 0; num < dt.Columns.Count; num++)
                {

                    if (dt.Rows[i][num].ToString() != "" && dt.Columns[num].ColumnName.ToLower() != Session["filed_id"].ToString() && dt.Columns[num].ColumnName.ToLower() != Session["filed_pid"].ToString())
                    {
                        sqlfield += dt.Columns[num].ColumnName + ",";
                     
                    }
                 
                }
                sqlfield += Session["filed_pid"].ToString();
                   
                for (int num1 = 0; num1 < dt.Columns.Count; num1++)
                {
                    if (dt.Rows[i][num1].ToString() != "" && dt.Columns[num1].ColumnName.ToLower() != Session["filed_id"].ToString() && dt.Columns[num1].ColumnName.ToLower() != Session["filed_pid"].ToString())
                    {
                     
                        sqlvalues += "'" + dt.Rows[i][num1].ToString() + "',";
                    }               
                }
                sqlvalues += Session["num"].ToString();                  
                strsql += sqlfield + ") values(" + sqlvalues + ")";           
                new cc.DBHepler().RunSqlRetNull(strsql, CommandType.Text, null, null);//插入下级数据
            }

        }
    }

posted @ 2010-01-27 18:04  】Richard【  阅读(237)  评论(0编辑  收藏  举报