将数据集值保存为整数

时间:2010-08-12 13:57:48

标签: c# asp.net

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

Dataset pds;

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

5 个答案:

答案 0 :(得分:3)

你想要

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

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

答案 1 :(得分:0)

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

System.Convert

int.TryParse

另外,请确保正确检查列中的值是否等于DBNull.Value,这是新.NET开发人员执行此操作时的常见运行时错误来源。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

你可能想用这种东西来检查一些事情。我经常检查以确保Dataset不为空,然后确保它至少有一个表和一行。如果它确实包含所有这些内容,那么请仔细检查您要查找的值是否为null并且是实际整数。这是一个例子(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

正如您在问题中提供的示例的说明一样,如果您在{{1}内声明它,那么您将无法在该IsLocationCount语句之外使用该If变量声明。如果你需要在If语句之外,你应该在它之外声明它。

答案 4 :(得分:0)

Dim value As integer = ds.Tables(0).Rows(0)(0)