2012-10-08 73 views
1

刚学习了一下VB,并希望如下搜索一个非常简单的SQL查询:SQL查询在VB - 缺少操作数

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    firstOrd = txtFirstOrd.Text 
    Me.OrdersBindingSource.Filter = "select * from orders WHERE firstDate =" & firstOrd 
End Sub 

我刚得到这个执行一个简单的按钮,并不断收到这样的:

Syntax error: Missing operand after 'orders' operator. 

Im相当肯定它是一个报价问题,但不能工作了!任何帮助将是伟大的!

回答

1

它看起来像你试图使用bindingSource的过滤器。这filter property可包括条件检查给定的BindingSource

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    firstOrd = txtFirstOrd.Text 
    Me.OrdersBindingSource.Filter = String.Format("firstDate = '{0}'", firstOrd) 
End Sub 

的每一行我假设firstOrd将包含DATETIME值,所以可以考虑在看看BindingSource.Filter by Date

+0

嗨Pilgerstorfer,是'firstOrd'是数据库内的dataTime值。你能向我解释'{0}'吗?感谢您的帮助! – user1352057

+0

而不是连接我喜欢使用的每个字符串** String.Format **。这会在第一个值之后用'{0}'代替,(这里:'firstOrder') –

+0

尝试了Pilgerstorfer。我不能让我的方法找到'.Filter By date'过滤功能。当我运行上面的代码时,我得到以下内容:无法对System.DateTime和System.String执行'='操作。感谢您的帮助! – user1352057

1

这听起来像你正在使用和SQL解释器,它认为orders SQL中的关键字。为了清楚表明它是一个表名,请引用它。

"select * from [orders] WHERE firstDate =" & firstOrd 

但是,通常建议(但不总是实用)只是为了避免表名是关键字。

+0

嗨康拉德,尝试了以前,并得到这个语法错误:'[orders]'操作符后缺少操作数。 – user1352057