-1

我正在使用 c# 开发一个表单应用程序,其中有一个表,我在我的数据库中定期更新相同的数据有一个名为 customers 的表具有以下列

客户 ID | 余额 | Last_Bill_Id | Last_Bill_Date | Last_Bill_Total | Last_Deposited | Last_Deposited_Date |

现在我正在使用这段代码来更新我的数据库

 try
        {

            OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = G:\\my Documents\\Visual Studio 2008\\Projects\\xml_and_db_test\\xml_and_db_test\\bin\\Debug\\database_for_kissan_Pashu_AhaR_Bills.mdb");
         string queryText = "UPDATE Customers SET Balance = ? ,Last_Bill_Id = ?,Last_Bill_Date = ? ,Last_Bill_Total = ?,Last_Deposited = ? , Last_Deposited_Date =?  where CustomerId = ?;";


            OleDbCommand cmd = new OleDbCommand(queryText, con);
            cmd.CommandType = CommandType.Text;







            cmd.Parameters.Add("Balance", OleDbType.Integer).Value = Convert.ToInt32(textBox2.Text);

            cmd.Parameters.Add("Last_Bill_Id", OleDbType.Integer).Value = 15;
            cmd.Parameters.Add("Last_Bill_Date", OleDbType.Date).Value = DateTime.Now;
            cmd.Parameters.Add("Last_Bill_Total", OleDbType.Integer).Value = 1560;

            cmd.Parameters.Add("Last_Deposited", OleDbType.Integer).Value = 500;

            cmd.Parameters.Add("Last_Deposited_Date", OleDbType.Date).Value = DateTime.Now;



            cmd.Parameters.Add("CustomerId", OleDbType.Integer).Value = Convert.ToInt32(textBox1.Text);





            con.Open(); // open the connection
            //OleDbDataReader dr = cmd.ExecuteNonQuery();

            int yy = cmd.ExecuteNonQuery();

            textBox3.Text = "update Done";
            con.Close();

        }
        catch (Exception ex)
        {
            string c = ex.ToString();
            MessageBox.Show(c);

        }

问题 有时我不想更新Last_Deposited 和 Last_Deposited_Date 字段然后我尝试了这段代码

  try
        {

            OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = G:\\my Documents\\Visual Studio 2008\\Projects\\xml_and_db_test\\xml_and_db_test\\bin\\Debug\\database_for_kissan_Pashu_AhaR_Bills.mdb");


            string queryText = "UPDATE Customers SET Balance = ? ,Last_Bill_Id = ?,Last_Bill_Date = ? ,Last_Bill_Total = ?  where CustomerId = ?;";

            OleDbCommand cmd = new OleDbCommand(queryText, con);
            cmd.CommandType = CommandType.Text;







            cmd.Parameters.Add("Balance", OleDbType.Integer).Value = Convert.ToInt32(textBox2.Text);

            cmd.Parameters.Add("Last_Bill_Id", OleDbType.Integer).Value = 15;
            cmd.Parameters.Add("Last_Bill_Date", OleDbType.Date).Value = DateTime.Now;
            cmd.Parameters.Add("Last_Bill_Total", OleDbType.Integer).Value = 1560;

        //    cmd.Parameters.Add("Last_Deposited", OleDbType.Integer).Value = 500;

       //     cmd.Parameters.Add("Last_Deposited_Date", OleDbType.Date).Value = DateTime.Now;



            cmd.Parameters.Add("CustomerId", OleDbType.Integer).Value = Convert.ToInt32(textBox1.Text);





            con.Open(); // open the connection
            //OleDbDataReader dr = cmd.ExecuteNonQuery();

            int yy = cmd.ExecuteNonQuery();

            textBox3.Text = "update Done";
            con.Close();

        }
        catch (Exception ex)
        {
            string c = ex.ToString();
            MessageBox.Show(c);

        }

现在,每当我用第二个代码更新我的 dB 时,保存在 Last_Deposited 和 Last_Deposited_Date 中的值都会被删除,我不想删除这些值,只想在这些字段有新值可用时更新。

4

1 回答 1

1

如果您不想更新您的 2 个字段,您可以不在更新查询中分配它们。

于 2013-07-28T11:42:18.867 回答