Okay, in my database, I have a column that has a 'date' datatype. That means it will only take dates in YYYY-MM-DD format.
No, it doesn't. It means that the values are just dates. The value in the database is a date, it's not "a date in a particular format". It's really important to understand the difference between the stored data and a textual representation which can be used for display or input.
My question is, how do I send the current date from C# to the database?
Use parameterized SQL, and set the value of the parameter to DateTime.Today
. You don't need a string representation at all. So something like:
// Work out the values beforehand
string name = getPlayerName(Tibia.Handle,
(BattleList_Start + Base + (BattleList_Step * playerIndex) + 4));
int level = ReadInt32(LvlAdr + Base, 4, Tibia.Handle);
int experience = ReadInt32(XpAdr + Base, 4, Tibia.Handle);
// Now do the database operations
string sql = @"INSERT INTO player (date, name, level, experience)
VALUES (@Date, @Name, @Level, @Experience)";
using (var conn = new MySqlConnection(...))
{
conn.Open();
using (var cmd = new MySqlCommand(sql, conn))
{
cmd.Parameters.Add("@Date", MySqlDbType.Date).Value = DateTime.Today;
cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = name;
cmd.Parameters.Add("@Level", MySqlDbType.Int32).Value = level;
cmd.Parameters.Add("@Experience", MySqlDbType.Int32).Value = experience;
int rows = cmd.ExecuteNonQuery();
// TODO: Validation of result (e.g. that 1 row was inserted)
}
}
Note that that will use the system default time zone to work out what date you mean by "now" - if you want a different time zone, please give more information.
Alternatively, if you use an ORM (e.g. Entity Framework, NHibernate etc) usually this would be simplified and you would never need to specify the SQL directly - the ORM should work in terms of the parameters automatically.