仅每5分钟写入一次数据库?

时间:2018-07-18 04:25:31

标签: c# database timer

我尝试使用计时器每5分钟将数据写入数据库一次,但是它不起作用。基本上,数据每秒从我的API中流式传输,但是我只需要我的程序每5分钟写入一行数据即可。

这是我尝试输入的代码:

                    /* print out the info from every person in te frame*/
                    // foreach ( Person person in people )
                    for (int i = 0; i < people.size(); i++) {
                        System.Console.Write("Person id" + people.get(i).id + " , face x coordinate: " + people.get(i).face.x + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , face y coordinate: " + people.get(i).face.x + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , face width coordinate: " + people.get(i).face.width + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , face height coordinate: " + people.get(i).face.height + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , Emotion - Joy: " + people.get(i).impression.emotion_response.joy_score + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , Emotion - Surprise: " + people.get(i).impression.emotion_response.surprise_score + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , Emotion - Anger: " + people.get(i).impression.emotion_response.anger_score + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , Emotion - Fear: " + people.get(i).impression.emotion_response.fear_score + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , Emotion - Sadness: " + people.get(i).impression.emotion_response.sadness_score + "\n");
                        System.Console.Write("Person id" + people.get(i).id + " , Emotion - Disgust: " + people.get(i).impression.emotion_response.disgust_score + "\n");



                         //this is where I attempted to write data into database at only 5 minute intervals
                         var timer = new System.Timers.Timer()
                         timer.Interval = 300000;
                         timer.Elapsed += (_s, _e) =>{
                         using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\sit\Desktop\(NEW) LEA WINFORMS\28June_LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonEffectivenessAnalysis\LessonAnalysis.mdf;Integrated Security=True")) {
                                conn.Open();

                                using (SqlCommand cmd1 = new SqlCommand("INSERT INTO TBL_VIDEO(JOY_SCORE,SURPRISE_SCORE,ANGER_SCORE,FEAR_SCORE,SADNESS_SCORE,DISGUST_SCORE) VALUES('" + people.get(i).impression.emotion_response.joy_score.ToString() + "','" + people.get(i).impression.emotion_response.surprise_score.ToString() + "','" + people.get(i).impression.emotion_response.anger_score.ToString() + "','" + people.get(i).impression.emotion_response.fear_score.ToString() + "','" + people.get(i).impression.emotion_response.sadness_score.ToString() + "','" + people.get(i).impression.emotion_response.disgust_score.ToString() + "')", conn)) {

                                    using (SqlDataReader dr = cmd1.ExecuteReader()) {

                                    }
                                }
                            }
                    }; timer.Enabled = true;

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试通过将timer放入具有people.size()大小的数组(循环之前),并使用i作为其索引。

编辑: 像这样...

List<System.Timers.Timer> timers = new List<System.Timers.Timer>();
int interval = 0;      // set this to -300000 if you want the first one to be 0 seconds.
for (int i = 0; i < people.size(); i++) {
    // Outputs.....

    interval += 300000;           // adds up 5 minutes each loop.
    var timer = new System.Timers.Timer()
    timer.Interval = interval;
    timer.Elapsed += (_s, _e) =>{
    using (SqlConnection conn = new SqlConnection(@"Data Source=...")) {
        conn.Open();

        using (SqlCommand cmd1 = new SqlCommand("INSERT INTO...", conn)) {
            using (SqlDataReader dr = cmd1.ExecuteReader()) {}
        }
    }
    timers.add(timer);
}; 

foreach (var t in timers) {
    t.Enabled = true;
}

这只是我的逻辑,我在Java中做了类似的事情。 请注意,这未经测试。