vb.net MS Access 的 SQL INSERT INTO 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15276449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:39:34  来源:igfitidea点击:

SQL INSERT INTO statement for MS Access

sqlvb.netms-accesssql-insertoledbexception

提问by gromit1

I am trying to learn how to use MS Access with my VB.net program. I am practicing learning how to use the INSERT INTO statement but I keep getting an error.

我正在尝试学习如何在我的 VB.net 程序中使用 MS Access。我正在练习学习如何使用 INSERT INTO 语句,但我不断收到错误消息。

Here is my code:

这是我的代码:

Imports System.Data.OleDb

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myConnection As OleDbConnection
    Dim DBpath As String = "C:\Documents and Settings\nordeen1\Desktop\Test.mdb"
    Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";Persist Security Info=True"
    myConnection = New OleDbConnection(sConnectionString)
    myConnection.Open()
    Dim SQLstr As String
    SQLstr = "INSERT INTO Test (Text, [Date], Number) VALUES ('testing', #2/6/1990#, 5)"
    Dim cmd As New OleDbCommand(SQLstr, myConnection)
    cmd.ExecuteNonQuery()
End Sub
End Class

I get this error "OleDbException was unhandled. Syntax error in INSERT INTO statement." at cmd.ExecuteNonQuery()

我收到此错误“OleDbException 未处理。INSERT INTO 语句中的语法错误。” 在cmd.ExecuteNonQuery()

Any suggestions are greatly appreciated! Thanks!

任何建议都非常感谢!谢谢!

回答by John Woo

TEXTand NUMBERare also reserved so they should be delimited,

TEXT并且NUMBER也被保留,所以它们应该被分隔,

SQLstr = "INSERT INTO Test ([Text], [Date], [Number]) VALUES ('testing', #2/6/1990#, 5)"

回答by Maryam Arshi

Use CDATEfor your Date :

使用CDATE你的时间:

   "INSERT INTO Test ([Text], [Date], [Number]) 
              VALUES ('testing',  CDATE('1990-06-02 00:00:00'), 5)"