如何节省数据库时间 [英] How to save time elapsed to database

查看:140
本文介绍了如何节省数据库时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是vb新手
我只想问一下如何将经过的时间存储到数据库中?

我有2种表格
在形式一中,我有一个计时器和一个按钮

hi guys im new in vb
i just want to ask if how can i store the elapsed time to my database?

i have 2 forms
in form one i have a timer and a button

Public Class Form1
    Public _elapseTimerRunning As Boolean = False
    Public _elapseStartTime As DateTime
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        _elapseStartTime = DateTime.Now
        _elapseTimerRunning = True
      
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        End

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub

    Public Sub Timer1_Tick(sender As Object, e As EventArgs)
        ''Form_Cert.Label5.Text = Now.ToString("h:mm:ss tt")
        If _elapseTimerRunning = True Then
            Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
            Form2.Label5.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
        End If
    End Sub
End Class


现在,我要在表格2的标签5中显示所花费的时间
我想将数据保存到数据库后吗?

我该怎么办?


THAKS IN ADVANCE


NOW I WANT TO DISPLAY THE ELAPSED TIME IN LABEL 5 OF FORM 2
AFTER THAT I WANT TO SAVE TIT TO MY DB?

HOW CAN I DO THAN?


THAKS IN ADVANCE

推荐答案

关于在另一种形式上显示经过的时间:我建议单击按钮时您正在执行的操作将被分开,非UI类.此类中的方法将进行操作,例如,使用事件将通知操作正在进行以及经过的时间量.

为了使UI实际显示结果,该操作应在另一个线程中运行.两种形式都可以关联事件,以便在引发类中的事件时,两种形式都可以使用所需的信息.

所以类结构可能类似于
-Form1
---按钮
---过去的事件处理程序

-Form2
---过去的事件处理程序

-类SomeName
--- ElapsedEvent
--- MethodName

为了真正做到这一点,您应该对以下内容进行文档处理:
- BackgroundWorker [需要InvokeRequired [ ^ ] tho显示UI线程中的经过时间
- EventArgs [ SqlConnection [ ^ ]
- SqlCommand [ ^ ]

并首先要正确保存,也 SqlTransaction [ ^ ]
Concerning showing the elapsed time on another form: I would suggest that operation you''re doing when the button is clicked would be separated to a different, non-UI class. A method in this class would make the operation and for example using an event it would inform that operation is on-going and the amount of elapsed time.

In order for the UI to actually show the results, the operation should be run in another thread. Both forms would wire the event so that when the event from the class is raised, both forms could do what they want with the information.

So the class structure could be something like
- Form1
--- button
--- eventhandler for elapsed

- Form2
--- eventhandler for elapsed

- Class SomeName
--- ElapsedEvent
--- MethodName

In order to actually do this you should go through the documenation for:
- BackgroundWorker[^] for the actual operation
- InvokeRequired[^] tho show the elapsed time in UI thread
- EventArgs[^] to create custom event arguments for your event

To save the data you would need a number column in a table where you can store for example the amount of elapsed ticks. To manipulate the data you would need:
- SqlConnection[^]
- SqlCommand[^]

and to do the saving properly in the first place, also SqlTransaction[^]


这篇关于如何节省数据库时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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