2010-08-12 65 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)