简体   繁体   中英

Wrong date after update C# & Sql server 2008

I'm developing a C# windows mobile app :

In a form, I read (since mi mobile) records on a server database (my pc) - SQL server 2008 - , these records will be stored in my mobile db - .sdf file- after it I'll do some calcs with them, and after the calcs, my records will return to my server database (my pc again) - SQL Server 2008 -

This is my code to read records and save them on my mobile:

sql0 = new SqlCommand("Select * From ACCT WHERE FASM = '" + uss+ "'  ", conn); //"uss" is the user
//code lines
//...
//more code lines
String NUMIA, NUMIPO, NUMCTIVO;

SqlCommand sqlA = new SqlCommand("SELECT * FROM ACMCT WHERE FRASM = '" + usuario + "' ", conn);
qlA.ExecuteNonQuery();
SqlDataAdapter cbA = new SqlDataAdapter(sqlA);
DataTable dtA = new DataTable();
cbA.Fill(dtA);
foreach (DataRow drA in dtA.Rows)
{
    NUMIA = drA["NUMIA"].ToString();
    NUMIPO = drA["NUMIPO"].ToString();
    NUMCTIVO = drA["NUMCTIVO"].ToString();
    //and more code lines
    FDQIS = drA["FDQIS"].ToString(); //<<<< Here starts my problem: FDQIS gets "02/09/00 0:00:00"
    StringBuilder sbQuery0 = new StringBuilder();
    SqlCeCommand cmd00 = new SqlCeCommand();
    sbQuery0.Append("INSERT INTO ACMCT (NUMIA, NUMIPO, NUMCTIVO... )VALUES('" + NUMIA + "','" + NUMIPO + "','" + NUMCTIVO + "'... )");
    cmd00.CommandText = sbQuery0.ToString();
    cmd00.Connection = connd;
    cmd00.ExecuteNonQuery();
    cmd00.Dispose();
    if (FDQIS == "")
    {
    //code lines
    }
    else
    {
    dateTimePicker1.Value = DateTime.Parse(drA["FDQIS"].ToString());
    StringBuilder sbQuery1 = new StringBuilder();
    SqlCeCommand cmd1k = new SqlCeCommand();

    sbQuery1.Append("UPDATE ACMCT SET FDQIS = " + dateTimePicker1.Value.ToString("yyyy/MM/dd") + " WHERE NUMIA = '" + NUMIA + "' ... ... ");
    cmd1k.CommandText = sbQuery1.ToString();
    cmd1k.Connection = connd;
    cmd1k.ExecuteNonQuery();
    cmd1k.Dispose();
    }
}

I thought It was woring right, but the problem is: I read from my server database the value of my date "02/09/00 0:00:00" , after I do my update it is saved as "09/02/00 0:00:00" and when I'm trying to show it on my screen, it shows: "22/04/00 0:00:00" . Being sincere, I really don't understand why it happens.

This is my code to show my data on the screen:

conn.ConnectionString = ("Data Source =" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\database.sdf;Password=****;Persist Security Info=False;");
            conn.Open();
            SqlCeCommand sql = new SqlCeCommand("SELECT * FROM ACMCT Where ACMCT.NUMCTIVO = '" + oc.Text + "' AND ACMCT.NUMIA = '" + CIA.Text + "' AND ACMCT.NUMIPO = '" + TIPO.Text + "' AND ACMCT.SUNUMCT = '" + SUBNUM.Text + "'", conn);
            sql.ExecuteNonQuery();
            SqlCeDataAdapter cb = new SqlCeDataAdapter(sql);
            DataTable dt = new DataTable();
            cb.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                Manntt ma = new Manntt(); 
                ma.txtCia.Text = dr["NUMIA"].ToString();
                ma.txtipo.Text = dr["NUMIPO"].ToString();
                ma.txtSubu.Text = dr["SUBUMCT"].ToString();
                ma.txtFadqFis.Text = dr["FDQIS"].ToString();//HERE the Value of txtFadqFis is "22/04/00 0:00:00"
                ma.Show();
            }

Am I doing my INSERT/UPDATE/DELETE wrong?

Any idea about what could I do?

I had a similar problem weeks ago! I believed that my INSERTs and UPDATEs , even DELETEs were wrong! But they weren't...

The solution is so simple:

Try this:

else
    {
    dateTimePicker1.Value = DateTime.Parse(drA["FDQIS"].ToString());
    StringBuilder sbQuery1 = new StringBuilder();
    SqlCeCommand cmd1k = new SqlCeCommand();

sbQuery1.Append("UPDATE ACMCT SET FDQIS = '" + dateTimePicker1.Value.ToString("yyyy/MM/dd") + "' WHERE NUMIA = '" + NUMIA + "' ... ... "); 
//////////// NOTE: I USE '' at the beginning and at the end of your date 
cmd1k.CommandText = sbQuery1.ToString();
cmd1k.Connection = connd;
cmd1k.ExecuteNonQuery();
cmd1k.Dispose();
}

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