我想把从数据库里查到的结果付给TXTBOX,可老提示
其他信息:语法错误 (操作符丢失) 在查询表达式 count(DISTINCT Xqmc) 中。
代码:
Private Sub ButCx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButCx.Click
Dim Sql As String, RsFltj As New ADODB.Recordset()
Dim DcndCount As String
Sql = "SELECT count(DISTINCT Xqmc) as var FROM Nhsfdcb1 where Dcnd=" & ComBoxDcnd.SelectedItem.ToString & ""
RsFltj.Open(Sql, gDBDat, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly)
If RsFltj.RecordCount > 0 Then
RsFltj.MoveFirst()
DcndCount = RsFltj.RecordCount.ToString
TxtXq.Text = DcndCount.ToString
RsFltj.Close() : RsFltj = Nothing
End If
End Sub
试试:
Sql = "SELECT count(DISTINCT Xqmc) as var FROM Nhsfdcb1 where Dcnd=" & "" & ComBoxDcnd.SelectedItem.ToString & ""
好像没问题,
调试查看一下Sql的值是否正常
另外这一句有没有问题:
RsFltj.Open(Sql, gDBDat, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly)
怎么不用ADO.NET?
1.用 msgbox(sql) 看一下语句是否正确;
2.命名不要跟保留字相同,as var ->as vVar。
select Count(TBL.Xqmc) as num
From
(Select DISTINCT Xqmc from Nhsfdcb1 WHERE Dcnd=" & CInt(ComBoxDcnd.Text)) TBL
试试看