日期:2014-05-18  浏览次数:20545 次

请教高手:怎么执行查询语句
使用的是vb6.0
        实现的功能是:用户任意选择界面上的查询项(即要搜索的关键字选项),然后程序自动生产一条sql查询语句字符串变量strsql,执行了这条查询语句后再把得到的结果绑定到DataGrid上输出
        我的单击搜索按钮的代码是这样的:(strsql变量是一条查询语句例如:select   studentName   from   studentInfo   where   studentName= '陈% ')

        Public   Sub   operateDatabase(strsql   As   String)
            Dim   st1   As   String
       
            st1   =   " " " "   +   strsql   +   " " " "
            Adodc1.RecordSource   =   st1
            Set   DataGrid1.DataSource   =   Adodc1
       
End   Sub

        但是运行时报出的错是:不存在存储过程 'select   studentName   from   studentInfo   where   studentName= '陈% '   '(当st1变量的值是这个字符串时)。Adodc1的CommandType属性设成任何一种都不行。反正Adodc1.RecordSource就不把st1当作一条查询语句。当我把st1换成程 'select   studentName   from   studentInfo   where   studentName= '陈% '这种原句时就没问题。但是这样就没法动态的查询了啊!
        这个问题困扰了我三天了,图书馆官于Ado   的书太少了,望大侠们给予指教,先谢过啦!!


------解决方案--------------------
把你的字符串用这个 括 起来

比如:

exec(st1)