2010-08-12 66 views
4

我有一个只有一列和一行的数据集。它给了我一个整数。我必须保存在另一个整数。我是一名学习者。请帮助我!谢谢!将数据集值保存为整数

Dataset pds; 

if (pds.Tables[0].Rows.Count > 0) 
{ 
     int Islocationcount = pds.Tables[0].Columns[0].ColumnName[1]; 
} 

回答

3

你想

int Islocationcount = Convert.ToInt32(pds.Tables[0].Rows[1][0]); 

假设它不会是DBNull.Value,否则会抛出异常

0

.NET有一些功能,可以帮助。退房:

System.Convert

int.TryParse

此外,还要确保你正确地检查出来的列的值等于DBNull.Value,这是运行时错误的新的.NET公共源开发人员这样做。

0

int value =(int)pds.Tables [0] .Rows [0] [“ColumnName”]; //或者可以使用列索引0

+0

裹,在一些错误检查,当然:) – David 2010-08-12 14:07:30

+0

谁需要错误检查?只是在开玩笑:-) – 2010-08-12 16:00:46

0

你可能想用这种东西来检查一些东西。我通常检查以确保Dataset不为空,然后确保它至少有一个表和一行。如果它包含所有这些内容,请仔细检查您要查找的值是否为空,并且是实际的整数。下面是一个例子(VB.NET,因为我很熟悉):

Dim IsLocationCounter As Integer 

If (Not pds Is Nothing AndAlso pds.Tables.Count > 0 AndAlso pds.Tables[0].Rows.Count > 0) Then 

    /* I can't remember here if you can use <> or if you have to use Is */ 
    If (pds.Tables[0].Columns[0].ColumnName[1] <> DBValue.Null) Then 

    /* Because you pass an integer by reference to TryParse, you don't have to set anything in an else statement */ 
    If (Not Integer.TryParse(pds.Tables[0].Columns[0].ColumnName[1].ToString(), IsLocationcounter)) Then 
     Throw New Exception ("Do some error handling here because there is no int coming back in your dataset") 
    End If 

    End If 
End If 

只是作为例子记下您在你的问题有,你将无法使用该IsLocationCount变量是外If声明如果您在If声明中声明它。如果你在If声明之外需要它,你应该在声明之外声明它。

0

昏暗值作为整数= ds.Tables(0).Rows(0)(0)