VB.NET:如何将值从访问转移到变量

时间:2016-03-20 11:37:19

标签: vb.net ms-access time

我想从Access检索TIMEIN和TIMEOUT的值,并将其存储到变量中供以后使用。我正在尝试的那段代码不起作用。这是代码:

Public timein As DateTime
Public timeout As DateTime 
Dim selectquery As String

    selectquery = "SELECT * FROM emp_timing " & "WHERE emp_code = @p1 AND day = @p2"


    Using con = New OleDbConnection(dbProvider & dbSource)
        Using cmd = New OleDbCommand(str, con)

            timein = cmd.Parameters.Item("emp_timein")
            timeout = cmd.Parameters.Item("emp_timeout")

            con.Open()

            'cmd.Parameters.Add("@p1", OleDbType.VarWChar).Value = System.DateTime.Now
            cmd.Parameters.Add("@p1", OleDbType.VarWChar).Value = TextBox1.Text
            cmd.Parameters.Add("@p2", OleDbType.Date).Value = New DateTime(Now.Year, Now.Month, Now.Day)

            Try

                Dim rowsSelect = cmd.ExecuteScalar()
                If rowsSelect > 0 Then
                    MsgBox("Data added to variables")

                End If
            Catch ex As Exception
                MsgBox(ex.Message)

            End Try
        End Using
    End Using

1 个答案:

答案 0 :(得分:0)

根据您的需要,将ExecuteNonQuery替换为ExecuteReader或ExecuteScaler。要根据选择查询从数据库中检索结果集,您将使用ExecuteReader

请参阅MSDN Documentstion