按顺序查看DataTable中的每一行,如果是新行,就设置并发布SQL Insert命令将其添加至表中,如果是修改后的数据,就只有SQL UPDATE命令。代码段如下;//将DataSet中的Emplist表中的数据更新至数据源//

try
    {
     // examine each of rows in the table
     foreach(EmpInfo.EmpListRow row in empInfo.EmpList.Rows)
     {
      switch(row.RowState)
      {
        // If the row is a new row, try add it to the datatabase
       case DataRowState.Added :
       cmd.CommandText =
         "insert into emplist(empid,last,dept,salary) values (" +
         "'" + row.EmpID.ToString() + "'," +
         "'" + row.Last.ToString() + "'," +
         "'" + row.Dept.ToString() + "'," +
         row.Salary.ToString() + ")";
        cmd.ExecuteNonQuery();
        break;
        // If the row is an update row, try to update it in the db.
      case DataRowState.Modified :
        cmd.CommandText = "update emplist set " +
         "last='" + row.Last + "'," +
         "dept='" + row.Dept + "'," +
         "salary=" + row.Salary + " " +
         " where empid = '" + row.EmpID + "'";
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
        break;
       default :
        break;
      }
      // Update the progress bar
      pbStatus.Value += 1;
     }
     // process the stack of deletes
    while(deletes.Count > 0)
     {
      cmd.CommandText = "delete from emplist where EmpID = '" +
       deletes.Pop() + "'";
      cmd.ExecuteNonQuery();
      pbStatus.Value += 1;
     }

         //注意DeleteRow的实现方式很特别,须定义数组或集合记录删除列的键值
     // All done, so commit these changes
     trns.Commit();    
    }


类别:ado.net 查看评论
posted on 2008-05-30 06:16  GIS飞行者  阅读(422)  评论(0)    收藏  举报