本文介绍了使用vb在acess db中加盖时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为访问数据库中的条目添加时间戳。我得到一个错误,说OleDb异常未处理,并且没有一个参数被激活。请帮忙。

Hi, I am trying to time stamp entries in my access db. i get an error saying that OleDb exception was unhandled and that one less prameter was pssed . please help.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Electric_Cables.Checked = True Then
            If RadioButton1.Checked = True Then
                mycon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Warehouses\prjct_mntrng.mdb"
                Dim command As New OleDbCommand()
                command.CommandText = "UPDATE Electric_Cables SET Actual_Start= '" & DateTime.Now & "'WHERE Asset_ID=" + TextBox1.Text + ""
                mycon.Open()
                command.Connection = mycon
                command.ExecuteNonQuery()
                MessageBox.Show("Entry Registered Successfully.")
            ElseIf RadioButton2.Checked = True Then
                mycon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Warehouses\prjct_mntrng.mdb"
                Dim command As New OleDbCommand()
                command.CommandText = "UPDATE Electric_Cables SET Actual_Finish= '" & Date.Now & "' WHERE Asset_ID=" + TextBox1.Text + ""
                mycon.Open()
                command.Connection = mycon
                command.ExecuteNonQuery()
                MessageBox.Show("Entry Registered Successfully.")
            ElseIf RadioButton1.Checked = False AndAlso RadioButton2.Checked = False Then
                MessageBox.Show("Check Report Start or Report Finish option for successful Entry.")
            End If

            If Utility_Poles.Checked = True Then

                If RadioButton1.Checked = True Then
                    mycon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Warehouses\prjct_mntrng.mdb"
                    Dim command1 As New OleDbCommand()
                    command1.CommandText = "UPDATE Utility_Poles SET Actual_Start= '" & Date.Now & "' WHERE Asset_ID=" + TextBox1.Text + ""
                    mycon.Open()
                    command1.Connection = mycon
                    command1.ExecuteNonQuery()
                    MessageBox.Show("Entry Registered Successfully.")
                ElseIf RadioButton2.Checked = True Then
                    mycon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Warehouses\prjct_mntrng.mdb"
                    Dim command2 As New OleDbCommand()
                    command.CommandText = "UPDATE Utility_Poles SET Actual_Finish= '" & Date.Now & "' WHERE Asset_ID=" + TextBox1.Text + ""
                    mycon.Open()
                    command2.Connection = mycon
                    command2.ExecuteNonQuery()
                    MessageBox.Show("Entry Registered Successfully.")
                ElseIf RadioButton1.Checked = False AndAlso RadioButton2.Checked = False Then
                    MessageBox.Show("Check Report Start or Report Finish option for successful Entry.")
                End If
            End If
        End If

    End Sub

推荐答案

command.CommandText = "UPDATE Electric_Cables SET Actual_Finish= '" + Format(Date.Now(), "yyyy-MM-dd") + "' WHERE Asset_ID=" + TextBox1.Text 





另一种方法是使用OLEDB。参数这将正确构造和转换参数内容。





The alternative is to use OLEDB .Parameters which will construct and convert parameters content correctly.

command.CommandText= "UPDATE Electric_Cables SET Actual_Finish=? WHERE Asset_ID=" + TextBox1.Text
command.prepare
command.Parameters.Add(Date.Now)





祝你好运。



Good Luck.


command.CommandText = "UPDATE Utility_Poles SET Actual_Finish= '" & Date.Now & "' WHERE Asset_ID=" + TextBox1.Text + ""





附:



With:

command.CommandText = "PARAMETERS [assetid] INT; UPDATE Utility_Poles SET Actual_Finish=#Now()# WHERE Asset_ID=[assetid]"



并使用OleDbCommand与 []。



注意:

我使用内置函数Now()用于MS JET OleDB引擎,Asset_ID用作数字字段。



如需了解更多信息,请参阅:

[]

[]

[]

[]


and use OleDbCommand with named parameters[^].

Note:
I used built-in function Now() for MS JET OleDB engine and Asset_ID as numeric field.

For further information, please see:
Configuring Parameters and Parameter Data Types[^]
OleDbParameterCollection.AddWithValue Method [^]
MS Access: PARAMETERES declaration[^]
Tips and Techniques for Queries in Access 2007[^]


这篇关于使用vb在acess db中加盖时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:55