我正在尝试连接到ODBC连接。
这很有效。
Dim cn As OdbcConnection
cn = New OdbcConnection("DRIVER={SQL Server};SERVER=ServerName;UID=UserName;" &
"PWD=Password;DATABASE=dbName;")
Dim mystring As String = "SELECT * FROM dbo_lData WHERE S_DT > #3/18/2018#"
Dim cmd As OdbcCommand = New OdbcCommand(mystring)
cn.Open()
MsgBox("Connected")
cn.Close()
我尝试过使用我在互联网上找到的不同代码的几种变体,但我一直都遇到同样的错误。错误42000,#附近的语法不正确。这是代码。
Dim selectSQL As String = "SELECT * FROM dbo_lData WHERE S_DT > #3/18/2018#"
cn = New OdbcConnection("DRIVER={SQL Server};SERVER=ServerName;UID=UserName;" &
"PWD=Password;DATABASE=dbName;")
Dim custDA As New OdbcDataAdapter
Dim selectCMD As OdbcCommand = New OdbcCommand(selectSQL, cn)
custDA.SelectCommand = selectCMD
Dim custDS As DataSet = New DataSet
custDA.Fill(custDS, "lData")
DataGridView1.Visible = True
DataGridView1.DataSource = custDA
我很遗憾,但我想做的只是......
答案 0 :(得分:0)
您获得的错误是指您的Select语句。正如评论所说的使用参数。首先仔细检查S_DT列的数据类型。我想知道为什么在SQL服务器本机提供程序SQLClient将产生更好的结果时使用ODBC。
Dim cn As New OdbcConnection("connection string")
Dim cmd As New OdbcCommand("SELECT * FROM dbo_lData WHERE S_DT > @sdate", cn)
cmd.Parameters.Add("@sdate", OdbcType.Date)