怎么 输出datatable插入表
列是固定的  循环踢里怎么写插入语句啊
foreach(DataRow dr in ds.tabe[0].Rows)
{
     
}
 

解决方案 »

  1.   

    这是我最近写的一个gridview插入表,希望可以帮到你string callTel = GridView1.Rows[i].Cells[0].Text;                   
                string countTel =GridView1.Rows[i].Cells[1].Text;                   
                string countTime = GridView1.Rows[i].Cells[2].Text;                 
                string avgTime = GridView1.Rows[i].Cells[3].Text;                   
                string inCount = GridView1.Rows[i].Cells[4].Text;                   
                string inCountTime = GridView1.Rows[i].Cells[5].Text;               
                string inAvgTime = GridView1.Rows[i].Cells[6].Text;                 
                string outCount = GridView1.Rows[i].Cells[7].Text;                  
                string outCountTime = GridView1.Rows[i].Cells[8].Text;              
                string outAvgTime = GridView1.Rows[i].Cells[9].Text;                            string sql = string.Format("insert into data_source_9 values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", callTel, countTel, countTime, avgTime, inCount, inCountTime, inAvgTime, outCount, outCountTime, outAvgTime);
                using (SqlConnection conn = DBHelper.Connection)
                {
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                }
      

  2.   

    function addTr(){
    var table = document.getElementById("tableId");
    var tabRows = table.rows.length;
    var tabTime = new Date().getTime();
    var newRow = table.insertRow(tabRows);
    newRow.className="toptr_mouse";
    var id = "root"+tabTime;
    var tdId = "id"+tabTime;
    newRow.id=id;
    var c0,c1,c2,c3,c4,c5;
    c0 = newRow.insertCell(0);
    c0.className = "td_2";
    c0.innerHTML = "<input name='wpName' id='wpName' maxlength='25' style='width:150px;' value='${wpName}'/>";
    c1 = newRow.insertCell(1);
    c1.className = "td_2";
    c1.innerHTML = "<input name='wpNums' maxlength='7' style='width:150px;' value='${wpNums}'/>";
    c2 = newRow.insertCell(2);
    c2.className = "td_2";
    c2.innerHTML = "<input name='wpGuige' maxlength='20' style='width:150px;' value='${wpGuige}'/>";
    c3 = newRow.insertCell(3);
    c3.className = "td_2";
    c3.innerHTML = "<input name='wpTezheng' maxlength='25' style='width:150px;' value='${wpTezheng}'/>";
    c4 = newRow.insertCell(4);
    c4.className = "td_2";
    c4.innerHTML = "<input name='wpBeizhu' maxlength='25' style='width:150px;' value='${wpBeizhu}'/>";
    c5 = newRow.insertCell(5);
    c5.className = "td_2";
    c5.innerHTML = "<img src='<%=path%>/images/jian.jpg' title='删除' style='cursor: pointer;' onclick=deleteTr('"+id+"'); /><input type='hidden' name='btnSys'/>";
    }
    给你一个类似的你自己看看,现在在上班也没太多时间帮你重新写个
      

  3.   

    列是固定的,那你肯定知道那几列的名称了。现在假设你有2列,第一列是字符型,第二列是double型的,给你构造语句如下,根据你实际进行修改即可。
    foreach(DataRowView drv in ds.Tables[0].DefaultView)
    {
         string strSQL = "insert into 表名 values('"+drv["列名一"].ToString()+"',"+double.Parse(drv["列名二"].ToString())+")";   
          //执行SQL语句,插入数据
          .....
    }
      

  4.   

    4楼  有错 提示 列不属于table
      

  5.   

    我改了啊 
    表是cjl  列是
    id int
    name varchar
    camount float
    cdate datetime
    就这4中类型
      

  6.   

    表是cjl 列是
    id int
    name varchar
    camount float
    cdate datetime这里的列名一二三四,是你的ds中取出来的对应你表中要要插入的字段,不是cjl中的id、name、camount、cdate,你明白木有???
    string sql = "select 列名一,列名二,列名三,列名四 from 其他表";
    DataSet ds = XXX(sql);
    //你下面利用的ds是类似上面的字段
    foreach(DataRowView drv in ds.Tables[0].DefaultView) 

          string strSQL = "insert into cjl values("+drv["列名一"].ToString()+",'"+double.Parse(drv["列名二"].ToString())+"',"+float.Parse(drv["列名三"].ToString())+",'"+DateTime.Parse(drv["列名四"].ToString())+"')"; 
            ....

      

  7.   

    那你的
    foreach(DataRowView drv in ds.Tables[0].DefaultView) 
    中的ds是什么???
      

  8.   

    excel表啊  查询excel导入的
      

  9.   

    string str;
    foreach(DataRow dr in ds.tabe[0].Rows)
    {
        str = "insert into cjl values('"+dr[0].ToString()+"','"+dr[1].ToString()+"','"+dr[2].ToString()+"','"+dr[3].ToString()+"'";
    ..执行
    }
      

  10.   


    哦,原来是从Excel中取得的,那就这样foreach(DataRowView drv in (DataView)ds) 

          string strSQL = "insert into cjl values("+int.Parse(drv.Row[0].ToString())+",'"+double.Parse(drv.Row[1].ToString())+"',"+float.Parse(drv.Row[2].ToString())+",'"+DateTime.Parse(drv.Row[3].ToString())+"')"; 
            ....

      

  11.   

    没有值的话,那就插入默认值吧,如字符型的话,插入空字符,数字的话,插入0 ,时间的话,插入个当前时间啥的,自己定。
    foreach(DataRowView drv in (DataView)ds) 

      string strSQL = "insert into cjl values("+int.Parse(string.IsNullOrEmpty(drv.Row[0].ToString())?"0":drv.Row[0].ToString())+",'"+double.Parse(string.IsNullOrEmpty(drv.Row[1].ToString())?"0":drv.Row[1].ToString())+"',"+float.Parse(string.IsNullOrEmpty(drv.Row[2].ToString())?"0":drv.Row[2].ToString())+",'"+DateTime.Parse(string.IsNullOrEmpty(drv.Row[3].ToString())?DateTime.Now.ToShortDateString():drv.Row[3].ToString())+"')"; 
      ....
    }  
      

  12.   


    还是有错 datetime类型的不行
      

  13.   


    主要是利用三元运算符进行空处理判断,还有一点就是你Excel中的数据要符合格式,否则会报数据类型不匹配的,比如说你Excel中时间字段中有不符合要求的话,那么插入时会报错的,所以前提是你Excel数据是正确的。
      

  14.   

     
    excel表是导出的 而且  我从sql里可以插入
     代码里 datetime类型 就出错了
      

  15.   


    就知道时间会出问题,你自己检查一下是不是Excel时间中有问题???如果是在检查不出来的话,一个不是办法的办法,将你表cjl 中cdate字段类型改为varchar(20),先倒入数据,然后看看哪个时间数据是错的。这个也是不得已的办法了。
      

  16.   


    现在能插入数据了嘛??表是cjl 列是
    id int
    name varchar
    camount float
    cdate datetime //这个字段你设置成可空 NULL,你原来设置成非空了吧???