這是MySQL表如何通過c#代碼更新mysql表?
reminder_id date show_day before_day status
1 2016-06-05 1999-01-01 2 p
2 2016-06-05 1999-01-01 1 p
3 2016-06-14 1999-01-01 2 p
4 2016-06-25 1999-01-01 0 p
5 2016-06-26 1999-01-01 4 p
我要更新的show_day列作爲(日期before_day)在ID 1 的show_day將( '2016-06-05'-2)=' 2016-06-03' in id 2 it will('2016-06-05'-1)='2016-06-01'等等......
我寫下面的代碼,但它將整列更新爲第一個id的相同值。 plz幫助我。
void init2()
{
DateTime date = default(DateTime);
int before_day = 0;
DateTime sToday = DateTime.Now;
string myConnection = "datasource= localhost;port=3306;username=root;password=root";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand SelectCommand = new MySqlCommand("SELECT* FROM bs.reminder ", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
while (myReader.Read())
{
date = myReader.GetDateTime(1);
before_day = myReader.GetInt32(4);
string show_day = date.AddDays(-before_day).ToString("yyyy/MM/dd");
string constring = "datasource= localhost;port=3306;username=root;password=root";
string Query = "update bs.reminder set status='s',show_day='" + show_day + "' where status= 'p';";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader1;
try
{
conDataBase.Open();
myReader1 = cmdDataBase.ExecuteReader();
while (myReader1.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
究竟是什麼問題? –
你爲什麼在代碼開始時打開MySQL連接,然後在循環中反覆打開一個?只需要第一個,我認爲 – vogomatix
它將整列更新爲第一個id的相同值 – user3399062