DataTable的中的数据来自截取的字符串 现在想把它存入到SQL中 :
        dt.Columns.Add("卡号", typeof(string));
        dt.Columns.Add("设备类型", typeof(string));
        dt.Columns.Add("名称", typeof(string));
  这是行 和SQL表的3个字段数据类型一样!
 该如如何存入到SQL?
      

解决方案 »

  1.   

    for(int i=0;i<ds.Table[0].Rows.Count;i++)
    {
         insert语句
    }
      

  2.   

    Method1.自己拼字符串循環插入
    Method2.放到一個DataSet中,用DataSet.Update()插入
      

  3.   

    Method1.自己拼字符串循環插入
    Method2.放到一個DataSet中,用DataSet.Update()插入
      

  4.   

    Method1.自己拼字符串循環插入
    Method2.放到一個DataSet中,用DataSet.Update()插入
      

  5.   

      DataTable  objtable;
      dt.Columns.Add("卡号", typeof(string));
      dt.Columns.Add("设备类型", typeof(string));
      dt.Columns.Add("名称", typeof(string));
      
      在objtable中确定有数据。
      for(int i=0;i<objtable.row.count;i++)
    {
        sql="insert into table(字段,字段) values (objtable[i]["字段"],objtable[i]["字段"]  )";
    }
      

  6.   

     SqlConnection con = new SqlConnection("Server=.;database=RiskData;uid=sa;pwd=sa123;");
            con.Open();
         
           for(int i=0;i<dt.Rows.Count;i++)
    {
        string strSQL = "insert into  GPS(ID,Type,BelongDepartment)  values (dt[i][卡号],dt[i][类型],dt[i][部门名称)";
        SqlCommand com = new SqlCommand(strSQL, con);
        com.ExecuteNonQuery();
    }        con.Close();
    报错 在此上下文中不允许使用名称 "dt"。有效表达式包括常量、常量表达式和变量(在某些上下文中)。不允许使用列名。前面已经命名 dt 了 里面有数据了
      

  7.   

    你的数据本来就是存在datatable里面的,你可以把这些数据取出来作为sql语句的参数然后往数据库里面插入就和正常的界面和后台的交互一样啊
      

  8.   

    DbDataAdapter.Update,设置好 InsertCommand 属性
      

  9.   

    两种方法:
    1、DataSet.Update()整表提交更新数据库;
    2、循环DataTable的行,一行一行的插入。
      

  10.   

    参考
    [code=C#][
            DataTable dt = new DataTable();
            dt.Columns.Add("卡号", typeof(string));
            dt.Columns.Add("设备类型", typeof(string));
            dt.Columns.Add("名称", typeof(string));
            StringBuilder sql = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                if (sql.ToString().Length > 0)
                {
                    sql.Append(";");
                }
                sql.Append
                    (String.Format("insert into 表名 (卡号,设备类型,名称) values ('{0}','{1}','{2}')",
                    dr[0].ToString(),
                    dr[1].ToString(),
                    dr[2].ToString()));
            }
            //sql.ToString()
    /code]
      

  11.   

    [Quote=引用 13 楼 jjkf5522 的回复:]
    楼主肯定是 dt变量冲突了。
      

  12.   

    为什么世界上还有这么多人用这么老的技术呢,使用Linq to Sql这些东西增删改查都太简单了啊。
    都什么年代了啊。奉劝小妹以后做东西还是考虑新技术吧。把数据库映射成实体对象,VS自动给你添加增删改查等方法,你直接“.”出来就用了,而且效率刚刚地~
      

  13.   

    19楼,人家楼主都说是新手啦,linq这样的新技术肯定玩不转的,直接告诉代码对她帮助还要大些~~for(int i=0;dt.Rows.count;i++)
    {
      insert into 表名 values (dt.Row[i]["卡号"].Tostring(),dt.Row[i]["设备类型"].Tostring(),dt.Row[i]["名字"].Tostring(),);
    }搞定~
      

  14.   

    额,写掉了一个 for(int i=0;i<dt.Rows.count;i++)
      

  15.   


    linq to sql 不是万能的 谢谢。本质还是SQL。