You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何将shipping_total转为数值存入数据库及设置列属性

解决方案:将运费以数值类型存入数据库

一、数据库列属性设置

根据你使用的数据库类型,把Z_Orders表的shipping列修改为以下合适的数值类型:

  • SQL Server/Access:decimal(18,2),18代表总位数,2代表小数位数,能覆盖绝大多数运费的精度需求
  • MySQL/MariaDB:DECIMAL(18,2),逻辑和SQL Server一致
  • PostgreSQL:numeric(18,2)

修改后该列可直接存储带小数的数值,避免字符串存储带来的精度丢失、无法直接计算等问题。

二、C#代码修改步骤

  1. 调整变量类型:把shippingCosts从字符串类型改为decimal(金额类数据优先用decimal,避免浮点数精度误差)
  2. 替换字符串拼接查询为参数化查询:原字符串拼接方式不仅会导致数值类型存储出错,还存在SQL注入风险,必须改用参数化方式传递数值参数

三、完整修改后的代码

//Order
string refId = order.id.ToString();
ApplicationLogger.Write("order.date_created : " + order.date_created.ToString());
var dateTime = Convert.ToDateTime(order.date_created.ToString()).ToString("MM/dd/yyyy HH:mm:ss");
string weight = order.cart_hash;
// 建议同步把总价也改成数值类型存库,处理逻辑和运费一致
decimal totalPrice = decimal.Parse(order.total);
string paymentMethod = order.payment_method;
// 直接获取数值类型的运费;如果order.shipping_total是字符串类型,改用decimal.Parse(order.shipping_total)
decimal shippingCosts = order.shipping_total; 
string insertOrderQuery = string.Empty;
try
{
    string invoice = "ΛΙΑ";
    if (order.billing != null)
    {
        if (!string.IsNullOrEmpty(order.billing.company))
            invoice = "TIM";
    }
   
    // 检查订单是否已存在,同样用参数化避免注入风险
    DataTable orderDT = BaseDAL.ExecCommand("select * from Z_Orders where refId=@refId", 
        new Dictionary<string, object> { {"@refId", refId} }, 
        connectionString);
    if (orderDT != null && orderDT.Rows.Count <= 0)
    {
         // 使用参数化插入语句
         insertOrderQuery = @"Insert into Z_Orders 
             ([refId],[date_time],[invoice],[order_weight],[total_price],[payment_method],[shipping]) 
             values (@refId,@dateTime,@invoice,@weight,@totalPrice,@paymentMethod,@shippingCosts)";
         // 构造参数集合
         var parameters = new Dictionary<string, object>
         {
             {"@refId", refId},
             {"@dateTime", dateTime},
             {"@invoice", invoice},
             {"@weight", weight},
             {"@totalPrice", totalPrice},
             {"@paymentMethod", paymentMethod},
             {"@shippingCosts", shippingCosts}
         };
         BaseDAL.ExecNonQueryCommand(insertOrderQuery, parameters, connectionString);
    }
}
catch(Exception ex)
{
    // 建议添加异常日志便于排查问题
    ApplicationLogger.Write("订单入库异常: " + ex.Message);
    throw;
}

额外说明

  • 如果你的BaseDAL不支持字典类型参数,也可以改用对应数据库的参数数组(以SQL Server为例):
var parameters = new SqlParameter[]
{
    new SqlParameter("@refId", refId),
    new SqlParameter("@dateTime", dateTime),
    new SqlParameter("@invoice", invoice),
    new SqlParameter("@weight", weight),
    new SqlParameter("@totalPrice", totalPrice),
    new SqlParameter("@paymentMethod", paymentMethod),
    new SqlParameter("@shippingCosts", shippingCosts)
};
  • 原代码中totalPrice也是以字符串存库,建议同步改成数值类型,后续做订单统计、金额计算时会更方便。

内容的提问来源于stack exchange,提问作者ΙΩΑΝΝΗΣ ΡΙΣΒΑΣ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 07:05:05