Tag: dbnull

无法将System.DBNull转换为int

我正在努力解决这个问题,我有一个Excel电子表格必须导入到我的SQL Server数据库。 我遍历它如下: Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.Range["A1", "D6867"]; int num = 4; // String test = ""; foreach (Microsoft.Office.Interop.Excel.Range row in xlRange.Rows) { if ((int)xlWorksheet.Cells[num, 1].Font.Size == 14) { ProductCategory category = new ProductCategory(); category.Category = xlWorksheet.Cells[num, 1].Value.ToString(); db.ProductCategories.Add(category); } num++; //System.Diagnostics.Debug.WriteLine(test); } db.SaveChanges(); xlWorkbook.Close(true, Missing.Value, Missing.Value); xlApp.Quit(); 我得到的错误是 无法将System.DBNull转换为int 在这一行: if ((int)xlWorksheet.Cells[num, 1].Font.Size == […]

通过OleDB读取Excel文件时,非空单元格中的DBNull

我使用OleDB来读取一个Excel文件。 其中一列具有“通用”格式,并且包含两个字母和数字组成的string。 string值检索没有问题,但纯数值检索为DBNull 。 怎么解决? 我使用下面的连接string打开Excel 2003文件 (xls): "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\file.xls; Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""