2013-07-23 42 views
0

我已經在C#中做了一些計算,並且希望將該值插入MySql數據庫。示例totalPrice = Price1 + Price2;我想將totalPrice傳入我的表格。怎麼做?如何向MySQL C插入值#

+0

你把它作爲你的INSERT語句中的參數。 –

+0

雅...我想通過它作爲十進制。可能嗎? – user2601433

回答

5

您需要使用INSERT語句。可能最好使用參數化查詢,而不是僅使用INSERT命令。

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

然後記得執行您的查詢。 command.ExecuteNonQuery();

+5

用於參數化查詢。不能太強調使用他們! – Corak

0

如果您正在使用的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..");