將sql中的money轉化為c#中的資料型別

superdont發表於2007-07-17
在購買物品時,需要對客戶賬戶上的餘額與物品的實際價格進行比較,因此需要把sql中的money讀取出來,並進行轉化。
 
找了好久。
結果如下

 string st1 = ConfigurationManager.ConnectionStrings["yikawangluoConnectionString"].ConnectionString;

        SqlConnection Conn1 
= new SqlConnection(st1);
        Conn1.Open();
        SqlCommand mycomm1 
= new SqlCommand("select account from userinfo where name='"+name2+"'", Conn1);
        
try
        
{
            SqlDataReader r 
= mycomm1.ExecuteReader();
            r.Read();
        
//  TextBox1.Text = ((DbType)r[0]).ToString();
            decimal money = Convert.ToDecimal(r[0]);
            TextBox1.Text 
= money.ToString();
        }

        
catch (SqlException eeee)
        
{
          
//  Response.Redirect("error1.aspx");
        }




在檢索的過程中,有如下答案,採用了第一種。

資料庫中的money型別對應C#的decimal型別,要解決這個問題起碼有兩種辦法轉換資料型別:

1:
使用Convert類提供的靜態方法ToDecimal
decimal money = Convert.ToDecimal(this.lable1.Text.Trim());

2:
使用decimal類提供的靜態方法Parse
decimal money = decimal.Parse(this.lable1.Text.Trim());

相關文章