2013-07-06 32 views
0

我有這樣的代碼:SqlDataReader的數值數據類型

public void nactiData() 
{ 
     SqlCommand cm = new SqlCommand("SELECT * FROM zajezd WHERE akce="+nc_zajezd_vyber, con); 
     con.Open(); 
     SqlDataReader reader = cm.ExecuteReader(); 

     if (reader.Read()) 
     { 
      zpocdnu.Text = reader.GetInt32(31).ToString(); 
      zcena3.Text = reader.GetDecimal(6).ToString(); 
     } 

     con.Close(); 
    } 

問題是,它不讀zcena3因爲在表中的數據類型是numeric

在微軟的網站上寫道,我應該用GetDecimal來閱讀它,但它也不起作用。

有沒有解決方法?

+6

對列位置使用'*'會引起麻煩。寫出列名,例如'GetString(「col1」)' – Andomar

+0

我試過了,應該用什麼替換*? – Marek

+1

什麼** exact **數據類型是SQL Server中的那些列?如果他們是'int','smallint'或'bigint';你需要使用'.GetInt16/.GetInt32/.GetInt64'(而不是'.GetDecimal')。你說使用'GetDecimal' *不起作用* - ***如何***不起作用?你有錯誤嗎?如果是這樣:***什麼錯誤***? –

回答

2

(你自己解決你的問題,但讓我提出以下幾點。)

既然你正在處理一個SQL Server的數據讀取,使用GetSqlDecimal代替GetDecimal

  • 它應該表現更好一點(因爲它不會做任何不必要的轉換)。
  • 它也可以處理NULL(通過IsNull屬性返回SqlDecimal值)。