如何在日期之间更新列 [英] How to update column between date

查看:52
本文介绍了如何在日期之间更新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

请帮我.我试图在两个日期之间更新列.系统提交方法.但这并不需要在数据库中进行更改.

这是代码.


Hello

Please help me. I tried to update column between two dates. The system commit the method. But it does not take change in the database.

This is the code.


public string AlterValid(DateTime startDate, DateTime endDate)
 {  
 OleDbConnection conn = new OleDbConnection("My Db setup");
 string status = null;
 bool setToFalse = false;  

 string updateSql = "UPDATE Watchreceipt SET valid = @valid WHERE Dato BETWEEN @startDate AND @endDate AND DriverIDFK = @DriverIDFK";  
 
conn.Open();
 OleDbTransaction sqltrans = conn.BeginTransaction();
  
 try
 {
 OleDbCommand updateCmd = conn.CreateCommand();
 updateCmd.CommandText = updateSql;
  
 updateCmd.Parameters.AddWithValue("@startDate", startDate);
 updateCmd.Parameters.AddWithValue("@endDate", endDate);
 updateCmd.Parameters.AddWithValue("@DriverIDFK", UserMapper.driverNumber);
  
 updateCmd.Transaction = sqltrans;
  
 updateCmd.Parameters.Add("@valid", OleDbType.Boolean);
 updateCmd.Parameters["@valid"].Value = setToFalse;
  
 updateCmd.ExecuteNonQuery();
 sqltrans.Commit();  
 
status = "Ok";
 }
 catch (OleDbException odbe)
 {
 sqltrans.Rollback();
 System.Windows.Forms.MessageBox.Show("Rolled back\n" + odbe.Message);
 }
 catch (Exception ex)
 {
 System.Windows.Forms.MessageBox.Show("Systemfejl\n" + ex.Message);
 }
 finally
 {
 conn.Close();
 }  
 return status; 
}



请帮助



Please help

推荐答案

正如我对这个问题的另一个实例所说-您需要使用在try块外创建的命令,而不是在try块内创建新命令-参数不会自行传输.

请不要重新发布问题,因为您觉得自己的回复速度不够快-我们所有人都有其他工作要做,这很不礼貌.请记住,我们不会为此获得报酬! :laugh:
As I said to your other instance of this question - you need to use the command you created outside the try block, not create a new one inside it - the parameters do not transfer themselves.

Please do not re-post questions because you don''t feel you got a fast enough reply - we all have other jobs to do, and it is rude. Remember we don''t get paid for this! :laugh:


这是解决方法

this is the solution

public string UpdateValid(DateTime startDate, DateTime endDate)
        {
          
            OleDbConnection conn = new OleDbConnection("my db path"));
            string status = null;
            
            string updateSql = "UPDATE Watchreceipt SET valid = false WHERE Dato BETWEEN @startDate AND @endDate AND DriverIDFK = @DriverIDFK";

            conn.Open();
            OleDbTransaction sqltrans = conn.BeginTransaction();
            OleDbCommand updateCmd = conn.CreateCommand();
            updateCmd.CommandText = updateSql;
            
            updateCmd.Parameters.AddWithValue("@startDate", startDate);
            updateCmd.Parameters.AddWithValue("@endDate", endDate);
            updateCmd.Parameters.AddWithValue("@DriverIDFK", UserMapper.driverNumber);

            updateCmd.Transaction = sqltrans;

            try
            {
                
                updateCmd.ExecuteNonQuery();
                sqltrans.Commit();
                
                status = "Ok";
            }
            catch (OleDbException odbe)
            {
                sqltrans.Rollback();
                System.Windows.Forms.MessageBox.Show("Rolled back\n" + odbe.Message);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Systemfejl\n" + ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return status;
        }



我更改了SQL语句并删除了try块中的一些代码.
谢谢!!



I changed the SQL statement and deleted some code inside the try block.
Thanks!!


这篇关于如何在日期之间更新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆