DataTable OptimalTable = tbl_Optimized_Schedule1.Tables["tbl_Optimized_Schedule"];     
DataRow myNewDataRow = OptimalTable.NewRow(); OptimalTable.Rows.Add(myNewDataRow);
    我用OptimalTable.Rows.Add(myNewDataRow);将数据插入OptimalTable表中,,然后用下面两句将OptimalTable更新,
 OptimalTable.AcceptChanges();
 adapter1.Update(OptimalTable);我想问得是,,怎么把OptimalTable 中的数据插入到tbl_Optimized_Schedule的数据库表中。  请指点,,谢谢!

解决方案 »

  1.   

    可是去掉Acceptchange后,程序报错System.InvalidOperationException: Update requires a valid InsertCommand when passed DataRow collection with new rows. at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
      

  2.   

    System.InvalidOperationException: Update requires a valid InsertCommand when passed DataRow collection with new rows.
    ===========================================
    当有一个新的数据行时  DbDataAdapter需要提供正确的insertcommand
      

  3.   

    ms-help://MS.MSDNQTR.2003FEB.2052/cpqstart/html/cpsmpnetsamples-howtodataadonet.htm#cpsmpAdorstodatasetSample
      

  4.   

    你这个是在DataTable中添加数据后导入到数据库中。那么你的adapter必须要有insertcommand
      

  5.   

    有谁知道这个insertcommand 怎么写吗?
      

  6.   

    可以使用SqlCommandBuilder自动生成更新command,在Update前加入以下代码://生成更新
    SqlCommandBuilder cbUpdate = new SqlCommandBuilder(adapter1);//更新数据库
    adapter1.Update(OptimalTable);
      

  7.   

    那么自己写吧。SqlCommand cmdInsert=New SqlCommand(yourInsertSQL);
    //Add your other codeadapter1.InsertCommand=cmdInsert;
    adapter1.Update(OptimalTable);
      

  8.   

    有谁知道这个insertcommand 怎么写吗?
    ======================
    adapter1.insertCommand = new SqlCommand("insert 语句" ,new SqlConnection (strcon ));
      

  9.   

    this.OleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
    this.adapter1 = new System.Data.OleDb.OleDbDataAdapter();
    this.conn = new System.Data.OleDb.OleDbConnection();
    this.adapter1.InsertCommand = this.OleDbInsertCommand1;
    this.OleDbInsertCommand1.CommandText = "INSERT INTO nqry_Optimized_Schedule(PART_NO, SHOP_NO, OP_NO, MACH_NO, LABOR, STAR" +
    "T, STOP, DUE_DATE, QTY, DEPT_NO) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    this.OleDbInsertCommand1.Connection = this.conn;
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("PART_NO", System.Data.OleDb.OleDbType.VarChar, 50, "PART_NO"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("SHOP_NO", System.Data.OleDb.OleDbType.VarChar, 50, "SHOP_NO"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("OP_NO", System.Data.OleDb.OleDbType.VarChar, 50, "OP_NO"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("MACH_NO", System.Data.OleDb.OleDbType.VarChar, 50, "MACH_NO"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("LABOR", System.Data.OleDb.OleDbType.VarChar, 50, "LABOR"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("START", System.Data.OleDb.OleDbType.Date, 0, "START"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("STOP", System.Data.OleDb.OleDbType.Date, 0, "STOP"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("DUE_DATE", System.Data.OleDb.OleDbType.Date, 0, "DUE_DATE"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("QTY", System.Data.OleDb.OleDbType.Integer, 0, "QTY"));
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("DEPT_NO", System.Data.OleDb.OleDbType.SmallInt, 0, "DEPT_NO"));
    }
         catch (Exception e)
            {
          Response.Write(e.ToString());
          }
      

  10.   

    有了上面这些应该就可以了吧,,怎么还插不进出,,还报错,invalid   insertcommond
      

  11.   

    你的参数全部没有赋值://这个语句有问题,并没有定义参数值
    this.OleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("PART_NO", System.Data.OleDb.OleDbType.VarChar, 50, "PART_NO"));
      

  12.   

    this.OleDbInsertCommand1.CommandText = "INSERT INTO nqry_Optimized_Schedule(PART_NO, SHOP_NO, OP_NO, MACH_NO, LABOR, STAR" +
    "T, STOP, DUE_DATE, QTY, DEPT_NO) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";语法对嘛,values后边是参数名,用“?”不行
    这样子后边的this.OleDbInsertCommand1.Parameters.Add添加的是那个参数,程序不知道,肯定要错
      

  13.   

    可以尝试这样
    DataSet myDataSet;
       myDataSet = new DataSet();   // Not shown: methods to fill the DataSet with data.
       DataTable t;
       t = myDataSet.Tables["Suppliers"];   // Add a DataRow to a table.
       DataRow myRow;
       myRow = t.NewRow();
       myRow["CompanyID"] = "NWTRADECO";
       myRow["CompanyName"] = "NortWest Trade Company";   // Add the row.
       t.Rows.Add( myRow );   // Calling AcceptChanges on the DataSet causes AcceptChanges to be
       // called on all subordinate objects.
       myDataSet.AcceptChanges();