0

I'm doing some practice in VB 2012, I've already connect it to the database in microsoft access 2013. When I run it there's an error in the Select Trans_date from [Transaction] where Trans_date = SYSDATE

here's my code...

Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable

Public Class Form1

Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Dim ds As DataSet = New DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    provider = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
    dataFile = "C:\Users\hp-2\Documents\Visual Studio 2012\Projects\Delta\Delta.mdb"

    connString = provider & dataFile
    myConnection.ConnectionString = connString
     da = New OleDbDataAdapter("Select Trans_date from [Transaction] where Trans_date = SYSDATE" 
    & t_date.Text & "'", myConnection)
    da.Fill(ds, "Transaction")

    Dim view1 As New DataView(tables(0))
    Dim source1 As New BindingSource()
    source1.DataSource = view1
    showdata.DataSource = view1
    showdata.Refresh()
   End Sub

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

  End Sub
  End Class

and here's my error..

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error in string in query expression 'Trans_date = SYSDATE''.
4

2 回答 2

2

尝试使用Date()而不是SYSDATE

于 2013-08-11T12:02:28.667 回答
1

您应该使用参数化查询。举个例子

Dim query = "Select Trans_date from [Transaction] " & _
            "where Trans_date = ?" 
da = New OleDbDataAdapter(query, myConnection)
da.SelectCommand.Parameters.AddWithValue("@p1", DateTime.Today)
da.Fill(ds, "Transaction")

?在这里,框架将使用查询文本中的占位符以及添加到SelectCommand集合中的参数来构建正确的查询以传递给底层数据库引擎。

OleDb中,参数不能通过特定名称识别(就像其他网络数据库驱动程序那样),而只能通过它们在查询文本中的位置来识别。所以第一个占位符 (?) 与第一个参数 (@p1) 相关联,其值被格式化并传递给 MSAccess 引擎。

使用参数传递文本命令至关重要。您不必担心如何格式化日期、字符串和十进制数字。并且(使用 MSAccess 不太可能,但总是有可能)您不会编写暴露于Sql Injections的代码

于 2013-08-11T11:58:46.793 回答