GetChanges方法本身就获得所更新的行。

解决方案 »

  1.   

    ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count这句应该提到for外,比如:
    DataSet tempSet=ds.GetChanges(DataRowState.Modified);
    DataTable table=tempSet.Tables[0];
    int count=table.Count;
    for(int i=0;i<count;i++)
    {
    DataRow= table.Rows[0];
    .........
    }代码估计就优化到这,再慢,把Update语句中的Where条件在数据库中设为表的索引
      

  2.   

    谢谢,你这种方法确实比我现在的写法优化很多,用你这个应该会快很多 再慢估计也没办法了,下面我把我现在写的贴出来,现在是一行一行执行更新,上次我也试过FOR循环里组织更新语句,最后一次性执行,不过感觉提升效果不明显,大家觉得呢
                    int iRows = ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count;
                    for (int i = 0; i < ds.GetChanges(DataRowState.Modified).Tables[0].Rows.Count; i++)
                    {
                        FInterID = (int)ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["FInterID"];
                        FEntryID = (int)ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["FEntryID"];
                        FDate_FL = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["发料日期"].ToString();
                        FName_JX = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["机型"].ToString();
                        FDate_CG = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购回复时间"].ToString();
                        FNote_CG = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购备注"].ToString();
                        FNote_Plan = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["计划备注"].ToString();
                        FName_CGY = ds.GetChanges(DataRowState.Modified).Tables[0].Rows[i]["采购员(手录)"].ToString(); ;
                        strSQL = "exec pro_bj_InsertData '" + FInterID.ToString() + "','" + FEntryID.ToString() + "','" + FDate_FL + "','" + FName_JX + "','" + FDate_CG + "','" + FNote_CG + "','" + FNote_Plan + "','" + FName_CGY + "'";
                        conn = new SqlConnection(constr);
                        conn.Open();
                        SqlCommand sc = new SqlCommand(strSQL, conn);
                        sc.ExecuteNonQuery();

                    }
                    ds.AcceptChanges();
      

  3.   

    假如我要将table里的所有记录写到数据库的表中,能否不用for循环一条条写,可以一次性写入吗?
      

  4.   

    Insert 可以,Update和DELETE不可以System.Data.SqlClient.SqlBulkCopy这个类可以实现最快速的批量插入另外 conn = new SqlConnection(constr);
                        conn.Open();
    这两句移到for循环以外.
      

  5.   

    Insert 可以,一次性写入怎么写啊?另外这存在一个问题:既然要用一次性写入,那原报表里的数据就需要先删除,否则就造成重复了 
    其实用你昨天所说的方法我修改程序后感觉执行效率明显提升,应该可以达到要求了 我只是想做得更完善
      

  6.   

    string strCon = "Server=WRET-MOSY688YVW\\MRGLL;User Id=sa;Pwd=;DataBase=db_17";//定义数据库连接字符串
            SqlConnection sqlcon;//声明数据库连接对象
            SqlDataAdapter sqlda;//声明数据库桥接器对象
            DataSet myds;//声明数据集对象
            private void Form1_Load(object sender, EventArgs e)
            {
                sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
                sqlda = new SqlDataAdapter("select * from tb_PDic", sqlcon);//实例化数据库桥接器对象
                myds = new DataSet();//实例化数据集
                sqlda.Fill(myds);//填充数据集CodeGo.net
                dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定
                //设置DataGridView控件中的数据以隔行换色的形式显示
                foreach (DataGridViewRow dgvRow in dataGridView1.Rows)//遍历所有行
                {
                    if (dgvRow.Index % 2 == 0)//判断是否是偶数行
                    {
                        //设置偶数行颜色
                        dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightGoldenrodYellow;
                    }
                    else//奇数行
                    {
                        //设置奇数行颜色
                        dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightSeaGreen;
                    }
                }        }
            //执行批量更新操作
            private void button1_Click(object sender, EventArgs e)
            {
                myds.Tables.Clear();//清空数据集
                sqlcon = new SqlConnection(strCon);//实例化数据库连接对象
                sqlda = new SqlDataAdapter("select * from tb_PDic", sqlcon);//实例化数据库桥接器对象
                //给SqlDataAdapter的UpdateCommand属性指定执行更新操作的SQL语句
                sqlda.UpdateCommand = new SqlCommand("update tb_PDic set Name=@name,Money=@money where ID=@id", sqlcon);
                //添加参数并赋值
                sqlda.UpdateCommand.Parameters.Add("@name", SqlDbType.VarChar, 20, "Name");
                sqlda.UpdateCommand.Parameters.Add("@money", SqlDbType.VarChar,9, "Money");
                SqlParameter prams_ID = sqlda.UpdateCommand.Parameters.Add("@id", SqlDbType.Int);
                prams_ID.SourceColumn = "id";//设置@id参数的原始列
                prams_ID.SourceVersion = DataRowVersion.Original;//设置@id参数的原始值
                sqlda.Fill(myds);//填充数据集
                //使用一个for循环更改数据集myds中的表中的值
                for (int i = 0; i < myds.Tables[0].Rows.Count; i++)
                {
                    myds.Tables[0].Rows[i]["Name"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    myds.Tables[0].Rows[i]["Money"] = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
                }
                //调用Update方法提交更新后的数据集myds,并同步更新数据库数据
                sqlda.Update(myds);
                dataGridView1.DataSource = myds.Tables[0];//对DataGridView控件进行数据绑定
            }