DataSet.Tables[0].Rows[i].Delete后怎么获取行的信息

本文介绍了在C#中如何正确地访问DataSet中被标记为删除状态的数据行。当使用GetChanges方法获取删除状态的行时,直接访问会引发异常。文章提供了正确的访问方式,并解释了DataRowVersion.Original的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#的DataSet.Tables[0].Rows的行有几种状态:

Added:表示行已添加到DataRowCollection,尚未调用AcceptChange()
Modified:表示行已被修改,尚未调用AcceptChange()
Deleted:表示行已被删除,
Unchanged:自上次调用AcceptChange()之后没有更改的行.
Detached:行虽然创建,但没有放到DataRowCollection中.
 
有时候,要获取到客户端表格的删除行,然后再提交到数据库更新.但是如果直接这样访问:
DataTable dtDeleted = DataSet.GetChanges(DataRowState.Deleted);
string sID=dtDeleted.Rows[0]["sID"].ToString();
会出现DeletedRowInaccessibleException异常:不能通过已删除的行访问该行的信息.
 
怎么办呢?
  其实可以Rows的另一个重载索引器来访问:
string sID=dtDeleted.Rows[0]["sID",DataRowVersion.Original].ToString();
 
DataRowVersion.Original表示的是该行的原始值
Public Function CreateExcel(ByVal ds As DataSet) As NPOI.SS.UserModel.IWorkbook '创建workbook Dim workbook As NPOI.SS.UserModel.IWorkbook = New NPOI.XSSF.UserModel.XSSFWorkbook() '创建sheet Dim sheet As NPOI.SS.UserModel.ISheet = workbook.CreateSheet("Sheet1") '寫列標題 Dim format1 As IDataFormat = workbook.CreateDataFormat() Dim dateCellStyle As ICellStyle = workbook.CreateCellStyle() dateCellStyle.DataFormat = format1.GetFormat("yyyy/MM/dd HH:mm:ss") Dim hlink_style As XSSFCellStyle = workbook.CreateCellStyle() Dim hlink_font As XSSFFont = workbook.CreateFont() hlink_font.Underline = FontUnderlineType.Single hlink_font.Color = HSSFColor.Blue.Index hlink_style.SetFont(hlink_font) Dim link As HSSFHyperlink '创建 Dim SQLSTR As String Dim row As NPOI.SS.UserModel.IRow Dim iscost As String = "" Dim typeGroupNum As Integer = 0 For i As Integer = 0 To ds.Tables(1).Rows.Count - 1 Dim typeId As String = ds.Tables(1).Rows(i).Item("分類編號").ToString() Dim strType As String = "select * from MITDEVIL.料號_參數 where 分類編號='" + typeId + "' order by CAST(項目 AS INT )" Dim ds1 As DataSet = db.FillDataSet(strType) '创建 row = sheet.CreateRow(i + typeGroupNum) row.CreateCell(0).SetCellValue(Convert.ToString("兄弟料號")) row.CreateCell(1).SetCellValue(Convert.ToString("公司編號")) row.CreateCell(2).SetCellValue(Convert.ToString("公司名稱")) row.CreateCell(3).SetCellValue(Convert.ToString("料號")) row.CreateCell(4).SetCellValue(Convert.ToString("階次1")) row.CreateCell(5).SetCellValue(Convert.ToString("階次2")) row.CreateCell(6).SetCellValue(Convert.ToString("階次3")) row.CreateCell(7).SetCellValue(Convert.ToString("階次4")) row.CreateCell(8).SetCellValue(Convert.ToString("階次5")) row.CreateCell(9).SetCellValue(Convert.ToString("階次6")) row.CreateCell(10).SetCellValue(Convert.ToString("階次7")) row.CreateCell(11).SetCellValue(Convert.ToString("階次8")) row.CreateCell(12).SetCellValue(Convert.ToString("階次9")) row.CreateCell(13).SetCellValue(Convert.ToString("階次10")) row.CreateCell(14).SetCellValue(Convert.ToString("類別編號")) row.CreateCell(15).SetCellValue(Convert.ToString("類別名稱")) row.CreateCell(16).SetCellValue(Convert.ToString("品名規格")) 'row.CreateCell(17).SetCellValue(Convert.ToString("單位")) row.CreateCell(18).SetCellValue(Convert.ToString("入庫幣別")) row.CreateCell(19).SetCellValue(Convert.ToString("入庫單價")) row.CreateCell(20).SetCellValue(Convert.ToString("價格冊幣別")) row.CreateCell(21).SetCellValue(Convert.ToString("價格冊價格")) row.CreateCell(22).SetCellValue(Convert.ToString("基準幣別")) row.CreateCell(23).SetCellValue(Convert.ToString("基準幣別單價")) row.CreateCell(24).SetCellValue(Convert.ToString("折算匯率")) row.CreateCell(25).SetCellValue(Convert.ToString("分類編號")) row.CreateCell(26).SetCellValue(Convert.ToString("分類名稱")) row.CreateCell(27).SetCellValue(Convert.ToString("單位單價")) row.CreateCell(28).SetCellValue(Convert.ToString("數量")) For k = 0 To ds1.Tables(0).Rows.Count - 1 Dim typeCount As Integer = ds1.Tables(0).Rows.Count iscost = ds1.Tables(0).Rows(k).Item("是否計算Cost單價").ToString row.CreateCell(29 + k).SetCellValue(ds1.Tables(0).Rows(k).Item("摘要").ToString()) 'oCells(1 + i + typeGroupNum, 18 + k) = ds1.Tables(0).Rows(k).Item("摘要").ToString() Next '參數類別分組區分 '循環關聯數據 For j = 0 To ds.Tables(0).Rows.Count - 1 If ds.Tables(0).Rows(j).Item("分類編號").ToString() = typeId Then Dim partId As String = ds.Tables(0).Rows(j).Item("料號").ToString() Dim companyNo As String = ds.Tables(0).Rows(j).Item("公司編號").ToString() Dim strPart As String = "select 參數,摘要 from MITDEVIL.料號_參數類別 where 分類編號='" + typeId + "' and 料號='" + partId + "' and 公司編號 = '" + companyNo + "' order by CAST(項目 AS INT )" Dim ds2 As DataSet = db.FillDataSet(strPart) '拼接前半部分數據 row = sheet.CreateRow(i + typeGroupNum + 1) row.CreateCell(0).SetCellValue(ds.Tables(0).Rows(j).Item("兄弟料號").ToString()) row.CreateCell(1).SetCellValue(ds.Tables(0).Rows(j).Item("公司編號").ToString()) row.CreateCell(2).SetCellValue(ds.Tables(0).Rows(j).Item("公司名稱").ToString()) row.CreateCell(3).SetCellValue(ds.Tables(0).Rows(j).Item("料號").ToString()) row.CreateCell(4).SetCellValue(ds.Tables(0).Rows(j).Item("階次1").ToString()) row.CreateCell(5).SetCellValue(ds.Tables(0).Rows(j).Item("階次2").ToString()) row.CreateCell(6).SetCellValue(ds.Tables(0).Rows(j).Item("階次3").ToString()) row.CreateCell(7).SetCellValue(ds.Tables(0).Rows(j).Item("階次4").ToString()) row.CreateCell(8).SetCellValue(ds.Tables(0).Rows(j).Item("階次5").ToString()) row.CreateCell(9).SetCellValue(ds.Tables(0).Rows(j).Item("階次6").ToString()) row.CreateCell(10).SetCellValue(ds.Tables(0).Rows(j).Item("階次7").ToString()) row.CreateCell(11).SetCellValue(ds.Tables(0).Rows(j).Item("階次8").ToString()) row.CreateCell(12).SetCellValue(ds.Tables(0).Rows(j).Item("階次9").ToString()) row.CreateCell(13).SetCellValue(ds.Tables(0).Rows(j).Item("階次10").ToString()) row.CreateCell(14).SetCellValue(ds.Tables(0).Rows(j).Item("類別編號").ToString()) row.CreateCell(15).SetCellValue(ds.Tables(0).Rows(j).Item("類別名稱").ToString()) row.CreateCell(16).SetCellValue(ds.Tables(0).Rows(j).Item("品名規格").ToString()) 'row.CreateCell(17).SetCellValue(ds.Tables(0).Rows(j).Item("單位").ToString()) row.CreateCell(18).SetCellValue(ds.Tables(0).Rows(j).Item("幣別").ToString()) row.CreateCell(19).SetCellValue(ds.Tables(0).Rows(j).Item("價格").ToString()) row.CreateCell(20).SetCellValue(ds.Tables(0).Rows(j).Item("Currency").ToString()) row.CreateCell(21).SetCellValue(ds.Tables(0).Rows(j).Item("Price_Book").ToString()) row.CreateCell(22).SetCellValue(ds.Tables(0).Rows(j).Item("基準幣別").ToString()) row.CreateCell(23).SetCellValue(ds.Tables(0).Rows(j).Item("基準單價").ToString()) row.CreateCell(24).SetCellValue(ds.Tables(0).Rows(j).Item("折算匯率").ToString()) row.CreateCell(25).SetCellValue(ds.Tables(0).Rows(j).Item("分類編號").ToString()) row.CreateCell(26).SetCellValue(ds.Tables(0).Rows(j).Item("分類名稱").ToString()) 'row.CreateCell(26).SetCellValue(ds.Tables(0).Rows(j).Item("實際價格").ToString()) row.CreateCell(28).SetCellValue(ds.Tables(0).Rows(j).Item("數量").ToString()) ''匯率計算 'SQLSTR = "SELECT " 'SQLSTR += "(SELECT 匯率 FROM [MITDEVIL].[匯率] A WHERE A.幣別=CASE WHEN '" + ds.Tables(0).Rows(j).Item("Currency").ToString() + "'='RMB' THEN 'CNY' ELSE '" + ds.Tables(0).Rows(j).Item("Currency").ToString() + "' END " 'SQLSTR += "AND NOT EXISTS (SELECT 1 FROM [MITDEVIL].[匯率] B WHERE A.幣別=B.幣別 AND B.匯率日期>A.匯率日期)) " 'SQLSTR += "/ " 'SQLSTR += "(SELECT 匯率 FROM [MITDEVIL].[匯率] A WHERE A.幣別=CASE WHEN '" + ds.Tables(0).Rows(j).Item("基準幣別").ToString() + "'='RMB' THEN 'CNY' ELSE '" + ds.Tables(0).Rows(j).Item("基準幣別").ToString() + "' END " 'SQLSTR += "AND NOT EXISTS (SELECT 1 FROM [MITDEVIL].[匯率] B WHERE A.幣別=B.幣別 AND B.匯率日期>A.匯率日期)) " 'row.CreateCell(24).SetCellValue(db.FillDataSet(SQLSTR).Tables(0).Rows(0)(0).ToString) '單位單價 --20220705,將EXP(SUM(LOG(參數)))替換為SUM(CAST(參數 AS FLOAT)) 先log 在exp 等於自身,沒有任何實際意義 '且計算過程中容易出現float溢出 cast預防出現 divide by zero 2.添加排除參數為NA的 AND A.參數 <>'NA' '202506130查询数据重复导致單價計算錯誤 SQLSTR = "SELECT '" + ds.Tables(0).Rows(j).Item("基準單價").ToString() + "'/ISNULL((CASE WHEN SUM(CAST(參數 AS FLOAT)) = 0 THEN 1 ELSE SUM(CAST(參數 AS FLOAT)) END),1) FROM ( " SQLSTR += "SELECT DISTINCT A.* FROM [MITDEVIL].[料號_階次類別] B, " SQLSTR += "[MITDEVIL].[料號_參數類別] A,[MITDEVIL].[料號_參數] C " SQLSTR += "WHERE A.兄弟料號 ='" + ds.Tables(0).Rows(j).Item("兄弟料號").ToString() + "' AND A.料號='" + ds.Tables(0).Rows(j).Item("料號").ToString() + "' AND A.公司編號 ='" + ds.Tables(0).Rows(j).Item("公司編號").ToString() + "' AND A.料號=B.料號 AND A.兄弟料號=B.兄弟料號 AND A.公司編號 = B.公司編號 AND A.分類編號=B.FMatClass AND A.分類名稱=B.FMatClassName AND B.FLAG='Y' " SQLSTR += "AND A.摘要=C.摘要 AND C.是否計算Cost單價='Y' AND A.參數<>'' AND A.參數 IS NOT NULL AND A.參數 <>'NA') A " row.CreateCell(27).SetCellValue(db.FillDataSet(SQLSTR).Tables(0).Rows(0)(0).ToString) For k = 0 To ds1.Tables(0).Rows.Count - 1 Dim typeCount As Integer = ds1.Tables(0).Rows.Count iscost = ds1.Tables(0).Rows(k).Item("是否計算Cost單價").ToString row.CreateCell(29 + k).SetCellValue(ds1.Tables(0).Rows(k).Item("摘要").ToString()) 'oCells(1 + i + typeGroupNum, 18 + k) = ds1.Tables(0).Rows(k).Item("摘要").ToString() Next For m = 0 To ds2.Tables(0).Rows.Count - 1 '拼接參數 row.CreateCell(29 + m).SetCellValue(ds2.Tables(0).Rows(m).Item("參數").ToString()) Next typeGroupNum = typeGroupNum + 1 End If Next Next Return workbook End Function Private Sub SaveDate(ByVal workbook As NPOI.SS.UserModel.IWorkbook, ByVal ExcelName As String) Dim ms As IO.MemoryStream Try '建立資料流 ms = New IO.MemoryStream() '寫入資料流 workbook.Write(ms) '在頁面輸出資料 Dim filename As String = "attachment;filename=" & HttpUtility.UrlEncode(ExcelName, Encoding.UTF8).ToString() Response.AddHeader("Content-Disposition", filename) Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312") Response.BinaryWrite(ms.ToArray()) Catch ex As Exception Finally '釋放資源 If Not ms Is Nothing Then ms.Dispose() ms.Close() End If End Try End Sub修改这段代码,新增导出过程中进度条显示
最新发布
06-14
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值