Skip to main content
 首页 » 编程设计

c#之数据类型货币 C#

2024年02月06日10findumars

有人请帮助我,我不知道为什么!

当我将一个值(例如 3469,2)插入到我的 SQL Server 数据库中时,我得到 34692,0000

列的类型是Money,值是double类型

// code 
public void updateligne_facture(String Ref, int Qte,String Reffacture,float prixvente,int tva) 
{ 
        SqlConnection con = new SqlConnection(); 
        con.ConnectionString = @"Data Source=AKRAM-PC\SQLEXPRESS;Initial Catalog=MM_DataBase;Integrated Security=True"; 
 
        con.Open(); 
 
        double prix = Qte * prixvente; 
 
        double prix_ttc = prix * (1 + (tva/ 100D)); 
 
        String requete = "update lc SET lc.Quantite='" + Qte + "',lc.Prix_HT='"+prix+"',lc.Prix_TTC='"+prix_ttc+"' FROM LIGNES_FACTURE as lc  JOIN  MM_ARTICLE as art ON lc.ID_Article=art.ID  JOIN MM_Facture as f ON lc.ID_Facture=f.ID   WHERE art.AR_Ref='" + Ref + "' AND f.Ref='" + Reffacture + "'"; 
        SqlCommand command = new SqlCommand(requete, con); 
        command.ExecuteNonQuery(); 
        con.Close(); 
    } 

请您参考如下方法:

根据微软type mapping guide , SQL Server 的 Money 类型映射到 C# 中的 decimal。

注意:进行查询的正确方法是使用参数,如下所示:

decimal prix = (decimal)(Qte * prixvente); 
decimal prix_ttc = prix * (1 + (tva/ 100M)); 
 
String requete = @"UPDATE lc  
                   SET lc.Quantite = @qte,  
                       lc.Prix_HT = @prix, 
                       lc.Prix_TTC = @prix_ttc 
                   FROM LIGNES_FACTURE as lc 
                   JOIN  MM_ARTICLE as art ON lc.ID_Article=art.ID 
                   JOIN MM_Facture as f ON lc.ID_Facture=f.ID 
                   WHERE art.AR_Ref = @ref  
                     AND f.Ref = @reffacture"; 
SqlCommand command = new SqlCommand(requete, con); 
command.Parameters.Add("@qte", qte); 
command.Parameters.Add("@prix", prix); 
... // Set other parameters here 
command.ExecuteNonQuery();