简体   繁体   中英

How to store date as 1 and month as 1 to mysql database by using c#

如何使用c#将日期存储为“ 1”,将月份存储为“ 1”(例如1-1-1987),我可以从变量frdt中获取年份,所以我需要存储例如1-1 -frdt。

 string frdt = drow1["release_year"].ToString();

If you're storing a date then it should be stored as a binary date, not as text. It's hard to tell whether you're doing that or not but you absolutely should. The fact that you're storing a year, which is a number, in a string variable is not encouraging. You should be storing the year in an int and then creating a DateTime like so:

var myDate = new DateTime(year, 1, 1);

where year is the int containing the year. You then save that to your database as you would any other data, using a parameter one would hope, eg

var command = new MySqlCommand("UPDATE MyTable SET MyDate = @MyDate WHERE ID = @ID", connection);

command.Parameters.AddWithValue("@MyDate", myDate);

i would pass it into a DateTime object and insert that one into your database

string frdt = drow1["release_year"].ToString();
DateTime Result = new DateTime(int.Parse(frdt), 1, 1);
string Command = "INSERT INTO TABLENAME (COLUMNNAME) VALUES (@DATEVALUE);"; // TODO
using (MySqlConnection mConnection = new MySqlConnection(ConnectionString))
{
    mConnection.Open();
    using (MySqlCommand myCmd = new MySqlCommand(Command, mConnection))
    {
        myCmd.Parameters.AddWithValue("@DATEVALUE", Result);
        int RowsAffected = myCmd.ExecuteNonQuery();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM