c# - 数据类型货币 C#

标签 c# sql sql-server-2008

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

当我将一个值(例如 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();

关于c# - 数据类型货币 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27172199/

相关文章:

c# - 正则表达式号码电话

sql - mysql 查询监听器

sql - "ORA-01733: virtual column not allowed here"插入 View 时

sql-server - 当时间戳(rowversion)溢出时会发生什么?

c# - 返回日期和时间值的日期类型列

c# - Membership.ValidateUser 方法如何访问数据库?

c# - 匹配包含嵌套 HTML 的术语

c# - Gmap.net 标记删除

sql - 如何在具有宽表的 SQL 查询中将一列与其他所有列进行比较?

sql - 加入日期范围查询