0

I have do some calculation in C# and i want insert that value to MySql database. Example totalPrice= Price1+Price2; I want pass the totalPrice into my table. How to do that?

2
  • You pass it as a parameter in your INSERT statement. Commented Jul 23, 2013 at 8:00
  • ya...i want pass it as decimal. Is it possible? Commented Jul 23, 2013 at 8:02

2 Answers 2

5

You need to use an INSERT statement. It's probably best to use parameterized queries rather than just an INSERT command.

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

Then remember to execute your query. command.ExecuteNonQuery();

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for parameterized queries. Can't be stressed too much to use 'em!
0

If you are using EntityFramework...

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

or

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.