oledb exception标准表达式中的数据类型不匹配

时间:2013-06-07 21:11:20

标签: vb.net visual-studio-2010

我在msaccess中填写了一些来自某些网络资源的数据。 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

我得到{“条件表达式中的数据类型不匹配。”}异常

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的日期时间类型与vb.net的日期时间类型之间有任何区别。

1 个答案:

答案 0 :(得分:2)

您应该尝试使用参数化查询,让框架找出将Date传递给数据库的正确方法,而不是试图强制数据库理解日期的字符串格式

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注入攻击的可能性