按日期删除数据库中的数据
Delete the data in the Database by date
我做了一个简单的程序,这个程序会添加数据库中的数据,提交时间,以及过期时间(提交后添加2天)。但是,当数据库中已有数据并且我将计算机中的日期更改为与过期时间相同时,然后当我 运行 程序时,数据库中应该删除的数据却没有。
还有Timer
with Interval 1000
(1 second) 即使我们没有运行程序也会被执行?
基本上,我想在达到过期时间时自动删除数据库中的数据。例如:数据库中有一条数据的过期日期和时间是2015年1月31日,当到达该过期日期和时间时,该数据将自动从数据库中删除。
这是我正在使用的代码:
public partial class Form1 : Form
{
public string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=../db1.accdb; Persist Security Info=False;";
Timer _timer = new Timer();
public Form1()
{
InitializeComponent();
_timer.Interval = 1000;
_timer.Tick += delegate
{
RealTimeTimer();
};
}
private void Form1_Load(object sender, EventArgs e)
{
_timer.Start();
}
private void button1_Click(object sender, EventArgs e)
{
Add();
this.label2.Text = this.textBox1.Text;
this.label3.Text = Convert.ToString(UserInformation.Submitted);
this.label4.Text = Convert.ToString(UserInformation.Expired);
}
private void RealTimeTimer()
{
UserInformation.Submitted = DateTime.Now;
if (DateTime.Now >= DateTime.Now.AddDays(2))
{
Delete();
}
}
private void Add()
{
try
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "INSERT INTO [Data] ([Description], [SubmittedOn], [ExpiredOn]) VALUES (@Description, @SubmittedOn, @ExpiredOn)";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
{
UserInformation.Expired = DateTime.Now.AddDays(2);
command.Parameters.Add("@Description", OleDbType.VarChar);
command.Parameters["@Description"].Value = this.textBox1.Text;
command.Parameters.Add("@SubmittedOn", OleDbType.VarChar);
command.Parameters["@SubmittedOn"].Value = Convert.ToString(UserInformation.Submitted);
command.Parameters.Add("@ExpiredOn", OleDbType.VarChar);
command.Parameters["@ExpiredOn"].Value = Convert.ToString(UserInformation.Expired);
command.ExecuteNonQuery();
}
conn.Close();
}
}
catch(Exception ex)
{
MessageBox.Show("There is an exception: " + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Delete()
{
try
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Data] WHERE [ExpiredOn] = @ExpiredOn";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
{
command.Parameters.Add("@ExpiredOn", OleDbType.VarChar);
command.Parameters["@ExpiredOn"].Value = Convert.ToString(UserInformation.Expired);
command.ExecuteNonQuery();
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("There is an exception: " + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
class UserInformation
{
public static DateTime Submitted
{
get;
set;
}
public static DateTime Expired
{
get;
set;
}
感谢对这个问题感兴趣的人。
非常感谢您的回答!
非常感谢。
WHERE [ExpiredOn] = @ExpiredOn
您知道这只有在两个日期完全匹配时才会成立,对吗?而且这个查询在准确的那一秒运行的可能性极小,对吧?
尝试 >=
试试这个:
private void Delete()
{
try
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Data] WHERE [ExpiredOn] <= @ExpiredOn";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
{
command.Parameters.AddWithValue("@ExpiredOn", UserInformation.Expired);
command.ExecuteNonQuery();
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("There is an exception: " + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
看看这个:
if (DateTime.Now >= DateTime.Now.AddDays(2))
{
// :-( I Will never be called...
}
它位于您的 timer_tick 方法中,不会被调用。
DateTime.Now 怎么会大于两天后..?
;
让我给你一个简单的方法来完成你的工作。即删除恰好两天前提交的记录。您需要做的就是让计时器 运行.
public Form1()
{
InitializeComponent();
_timer.Interval = 1000;
_timer.Tick += delegate
{
RealTimeTimer();
};
}
.
private void RealTimeTimer()
{
Delete();
}
.
private void Delete()
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Data] WHERE DateAdd('d', 2, [SubmittedOn]) <= Now";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
command.ExecuteNonQuery();
conn.Close();
}
}
通过让计时器 运行,数据库会检查自己是否有恰好在 2 天之前提交的记录并删除它们。
我做了一个简单的程序,这个程序会添加数据库中的数据,提交时间,以及过期时间(提交后添加2天)。但是,当数据库中已有数据并且我将计算机中的日期更改为与过期时间相同时,然后当我 运行 程序时,数据库中应该删除的数据却没有。
还有Timer
with Interval 1000
(1 second) 即使我们没有运行程序也会被执行?
基本上,我想在达到过期时间时自动删除数据库中的数据。例如:数据库中有一条数据的过期日期和时间是2015年1月31日,当到达该过期日期和时间时,该数据将自动从数据库中删除。
这是我正在使用的代码:
public partial class Form1 : Form
{
public string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=../db1.accdb; Persist Security Info=False;";
Timer _timer = new Timer();
public Form1()
{
InitializeComponent();
_timer.Interval = 1000;
_timer.Tick += delegate
{
RealTimeTimer();
};
}
private void Form1_Load(object sender, EventArgs e)
{
_timer.Start();
}
private void button1_Click(object sender, EventArgs e)
{
Add();
this.label2.Text = this.textBox1.Text;
this.label3.Text = Convert.ToString(UserInformation.Submitted);
this.label4.Text = Convert.ToString(UserInformation.Expired);
}
private void RealTimeTimer()
{
UserInformation.Submitted = DateTime.Now;
if (DateTime.Now >= DateTime.Now.AddDays(2))
{
Delete();
}
}
private void Add()
{
try
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "INSERT INTO [Data] ([Description], [SubmittedOn], [ExpiredOn]) VALUES (@Description, @SubmittedOn, @ExpiredOn)";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
{
UserInformation.Expired = DateTime.Now.AddDays(2);
command.Parameters.Add("@Description", OleDbType.VarChar);
command.Parameters["@Description"].Value = this.textBox1.Text;
command.Parameters.Add("@SubmittedOn", OleDbType.VarChar);
command.Parameters["@SubmittedOn"].Value = Convert.ToString(UserInformation.Submitted);
command.Parameters.Add("@ExpiredOn", OleDbType.VarChar);
command.Parameters["@ExpiredOn"].Value = Convert.ToString(UserInformation.Expired);
command.ExecuteNonQuery();
}
conn.Close();
}
}
catch(Exception ex)
{
MessageBox.Show("There is an exception: " + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Delete()
{
try
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Data] WHERE [ExpiredOn] = @ExpiredOn";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
{
command.Parameters.Add("@ExpiredOn", OleDbType.VarChar);
command.Parameters["@ExpiredOn"].Value = Convert.ToString(UserInformation.Expired);
command.ExecuteNonQuery();
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("There is an exception: " + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
class UserInformation
{
public static DateTime Submitted
{
get;
set;
}
public static DateTime Expired
{
get;
set;
}
感谢对这个问题感兴趣的人。
非常感谢您的回答!
非常感谢。
WHERE [ExpiredOn] = @ExpiredOn
您知道这只有在两个日期完全匹配时才会成立,对吗?而且这个查询在准确的那一秒运行的可能性极小,对吧?
尝试 >=
试试这个:
private void Delete()
{
try
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Data] WHERE [ExpiredOn] <= @ExpiredOn";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
{
command.Parameters.AddWithValue("@ExpiredOn", UserInformation.Expired);
command.ExecuteNonQuery();
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("There is an exception: " + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
看看这个:
if (DateTime.Now >= DateTime.Now.AddDays(2))
{
// :-( I Will never be called...
}
它位于您的 timer_tick 方法中,不会被调用。 DateTime.Now 怎么会大于两天后..?
;
让我给你一个简单的方法来完成你的工作。即删除恰好两天前提交的记录。您需要做的就是让计时器 运行.
public Form1()
{
InitializeComponent();
_timer.Interval = 1000;
_timer.Tick += delegate
{
RealTimeTimer();
};
}
.
private void RealTimeTimer()
{
Delete();
}
.
private void Delete()
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Data] WHERE DateAdd('d', 2, [SubmittedOn]) <= Now";
conn.Open();
using (OleDbCommand command = new OleDbCommand(query, conn))
command.ExecuteNonQuery();
conn.Close();
}
}
通过让计时器 运行,数据库会检查自己是否有恰好在 2 天之前提交的记录并删除它们。