Skip to main content
 首页 » 编程设计

c#之将数字存储在 sql 数据库中的整数中

2024年02月24日30jackei

我正在使用带有表 RESUME 和列 PageIndex 的数据库,其中类型是数据库中的数字,但是当我想将此 PageIndex 值存储为整数时,出现异常错误

指定的转换无效。

这是代码

string sql; 
string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Deliverable4.accdb"; 
protected OleDbConnection rMSConnection; 
protected OleDbDataAdapter rMSDataAdapter; 
protected DataSet dataSet; 
protected DataTable dataTable; 
protected DataRow dataRow; 

点击按钮

sql = "select PageIndex from RESUME"; 
rMSConnection = new OleDbConnection(conString); 
rMSDataAdapter = new OleDbDataAdapter(sql, rMSConnection); 
dataSet = new DataSet("pInDex"); 
rMSDataAdapter.Fill(dataSet, "RESUME"); 
 
dataTable = dataSet.Tables["RESUME"]; 

int pIndex = (int)dataTable.Rows[0][0];

rMSConnection.Close(); 
 
if (pIndex == 0) 
{             
    Response.Redirect("Create Resume-1.aspx"); 
} 
else if (pIndex == 1) 
{             
    Response.Redirect("Create Resume-2.aspx"); 
} 
else if (pIndex == 2) 
{           
        Response.Redirect("Create Resume-3.aspx"); 
    } 
} 

我在这一行中遇到错误

int pIndex = (int)dataTable.Rows[0][0]; 

请您参考如下方法:

如果无法将其转换为整数,则不会从数据库中将其作为整数检索。该字段的数据类型是什么?

尝试检查 dataTable.Rows[0][0].GetType().ToString() 的结果以查看对象实际是什么。