user2601433
user2601433

Reputation: 21

How to insert a value to MySQL C#

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?

Upvotes: 0

Views: 110

Answers (2)

Alex Zheludov
Alex Zheludov

Reputation: 171

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..");

Upvotes: 0

Darren
Darren

Reputation: 70728

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();

Upvotes: 5

Related Questions