如何从winform C#每1小时后访问或执行数据库操作? [英] How to access or perform database operations after each 1 hour from winform C# ?

查看:209
本文介绍了如何从winform C#每1小时后访问或执行数据库操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i有winform c#visual studio 2012

i需要你的帮助

其实我想执行数据库操作(每1小时后从我的winform中选择,更新,删除或插入。在1小时之前,数据库中应该没有任何动作。



让我采取一种方案。

实际上在我的winform中,我执行select查询和从DB(数据库)获取的数据保存在我的数据表中。

通过datagridview我允许用户执行一些修改,如添加新记录,删除等..

现在当用户点击保存按钮时,所有这些修改首先保存在我的数据表中,然后仅在1小时后在我的数据库中更新。下一个选择的数据库查询也应该在1小时后出现



我被卡住了,我怎么能这样做?



我尝试过:



public string strcon =server = xxxx; database = xxx; username = xxx ; password = xx;;

MySqlConnection conn = new MySqlConnection();

DataTable dt = new DataTable();

conn.ConnectionString = strcon;

if(conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)

conn.Open();

MySqlCommand cmd = new MySqlCommand(select * from enugro_service_requests_info,conn);



MySqlDataReader rd = cmd.ExecuteReader();

dt.Load(rd);

this.dataGridView1.DataSource = dt;

解决方案

有几种方法可以做到了 - 但最简单的可能是设置你pa等待小时过去的BackgroundWorker线程,然后执行更新。

您的主应用程序要么通过队列或类似方式将更新提供给线程,要么共享数据表并实现锁定以防止出现问题。



但是......如果我是用户,那可能会让我感到烦恼,因为我永远都不知道我的修改是否已经完成永久与否。如果断电会怎么样?我今天的哪些更改存储了?哪个没有?

如果我关闭申请会怎样?如果我使用任务管理器关闭应用程序会发生什么?



我强烈建议你不要这样做,除非你有一个非常非常好的理由...

hi all,
i have winform c# visual studio 2012
i need your help
in fact, i would like to perform database operation (select,update,delete or insert) from my winform after each 1 hour. Before 1 hour there should have no action in the database.

let me take one scenario.
actually in my winform, i performed select query and the data fetched from the DB(database) are saved in my datatable.
through a datagridview i allow user to perform some modifications,like add new record, delete etc..
now when user click on save button, all these modifications are saved first in my datatable and then updated in my DB only after 1 hour. And the next select query towards database should come also after 1 hour

I'm stuck,How can i do it?

What I have tried:

public string strcon = "server=x.x.x.x; database=xxx; username=xxx; password=xx;";
MySqlConnection conn = new MySqlConnection();
DataTable dt = new DataTable();
conn.ConnectionString = strcon;
if (conn.State==ConnectionState.Closed || conn.State== ConnectionState.Broken)
conn.Open();
MySqlCommand cmd = new MySqlCommand("select * from enugro_service_requests_info", conn);

MySqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
this.dataGridView1.DataSource =dt;

解决方案

There are a couple of ways to do it - but the simplest is probably to set up a BackgroundWorker thread that waits for the hour to elapse, and then does the updates.
Your main application either feeds the updates to the thread via a queue or similar, or they share the data table and you implement locking to prevent problems.

But...if I was the user, that would probably really annoy me, because I'd never know if my changes had been made permanent or not. What happens if the power fails? Which of my changes today are stored? Which haven't been?
What happens if I close the application? What happens if I use Task Manager to close the app?

I'd strongly recommend that you don't do this unless you have a really, really, good reason...


这篇关于如何从winform C#每1小时后访问或执行数据库操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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