0

我在 msaccess 中填写了一个表格,其中包含来自某些 Web 资源的数据。msaccess 中表的元数据,我的代码为

Dim Con1 As OleDbConnection = Nothing
Dim LastDateTimeOfRawNews As DateTime = Nothing
Private Function copyLatestNewstoNewsAndTemporary()
Con1 = DB_Manager.getConnection()
Con1.Open()
Dim SQL2 As String = ""
LastDateTimeOfRawNews = "#6/7/2013 4:36:46 PM#"
SQL2 = "insert into TemporaryNews(Title) SELECT Title FROM News where News.news_Date >='" + LastDateTimeOfRawNews + "'"
Dim objCmd2 As OleDbCommand = New OleDbCommand(SQL2, Con1)
objCmd2.ExecuteNonQuery()
Con1.Close()
End Function

元数据:

NewsId ->    AutoNumber
Title  ->    Text
news_Date    Date/Time         etc

我得到 {"Data type mismatch in criteria expression."} 异常

exception full detail is as under

System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217913
Message=Data type mismatch in criteria expression.
Source=Microsoft JET Database Engine
StackTrace:
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at WBand.frmMain.copyLatestNewstoNewsAndTemporary() in C:\Users\Khanz\Desktop\Latest news\Editing\WBand\Form1.vb:line 679
   at WBand.frmMain.Button3_Click(Object sender, EventArgs e) in C:\Users\Khanz\Desktop\Latest news\Editing\WBand\Form1.vb:line 918
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at WBand.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

另外请告诉我 msaccess 的 datetime 类型和 vb.net 的 datetime 类型之间有什么区别。

4

1 回答 1

3

与其试图强制数据库理解日期的字符串格式,不如尝试使用参数化查询并让框架找出将日期传递给数据库的正确方法

Private Sub copyLatestNewstoNewsAndTemporary()
    Dim SQL2 = "insert into TemporaryNews(Title) " & _
               "SELECT Title FROM News where News.news_Date >= ?"
    Using Con1 = DB_Manager.getConnection()
    Using objCmd2 As OleDbCommand = New OleDbCommand(SQL2, Con1)
        Con1.Open()
        Dim LastDateTimeOfRawNews = new DateTime(2013, 6,7, 16,36,46)
        objCmd2.Parameters.AddWithValue("@p1", LastDateTimeOfRawNews)
        objCmd2.ExecuteNonQuery()
    End Using
    End Using
End Function

参数化查询避免了格式化值(日期、字符串、小数)的问题,并消除了任何 Sql 注入攻击的机会

于 2013-06-07T22:39:04.333 回答