【问题标题】:How to insert a value to MySQL C#如何向 MySQL C# 中插入一个值
【发布时间】:2013-07-23 07:59:07
【问题描述】:

我在 C# 中做了一些计算,我想将该值插入 MySql 数据库。示例 totalPrice= Price1+Price2;我想将 totalPrice 传递到我的表中。该怎么做?

【问题讨论】:

  • 在 INSERT 语句中将其作为参数传递。
  • 是的...我想将它作为十进制传递。有可能吗?

标签: c# mysql .net sql text


【解决方案1】:

您需要使用INSERT 语句。最好使用参数化查询,而不仅仅是 INSERT 命令。

MySqlCommand command = new MySqlCommand();
string sql = "INSERT INTO YourTable (TotalPrice) VALUES (@TotalPrice)";
command.Parameters.AddWithValue("@TotalPrice", totalPrice);

然后记得执行您的查询。 command.ExecuteNonQuery();

【讨论】:

  • +1 用于参数化查询。使用它们不会有太大压力!
【解决方案2】:

如果您使用的是 EntityFramework...

yourTable obj = new yourTable();
obj.name = "name";
obj.created = DateTime.Now;
etc.......
ctx.yourTable.Add(obj);
ctx.SaveChanges();

ctx.Database.ExecuteSqlCommand("Insert intoy YourTable values etc..");

【讨论】:

    猜你喜欢
    • 2011-10-30
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2016-03-27
    • 1970-01-01
    相关资源
    最近更新 更多