【发布时间】:2013-07-23 07:59:07
【问题描述】:
我在 C# 中做了一些计算,我想将该值插入 MySql 数据库。示例 totalPrice= Price1+Price2;我想将 totalPrice 传递到我的表中。该怎么做?
【问题讨论】:
-
在 INSERT 语句中将其作为参数传递。
-
是的...我想将它作为十进制传递。有可能吗?
我在 C# 中做了一些计算,我想将该值插入 MySql 数据库。示例 totalPrice= Price1+Price2;我想将 totalPrice 传递到我的表中。该怎么做?
【问题讨论】:
您需要使用INSERT 语句。最好使用参数化查询,而不仅仅是 INSERT 命令。
MySqlCommand command = new MySqlCommand();
string sql = "INSERT INTO YourTable (TotalPrice) VALUES (@TotalPrice)";
command.Parameters.AddWithValue("@TotalPrice", totalPrice);
然后记得执行您的查询。 command.ExecuteNonQuery();
【讨论】:
如果您使用的是 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..");
【讨论】: