我写了一段查询数据的代码,分别在“.NET2003+Access ”里用DataGrid和“.Net2005+SQLSERVER"里用GridView绑定数据进行测试,编译运行都成功,但是查询时就是无法显示数据,偶尔成功过一次,但大部分时候都无法显示数据,请问大侠们究竟是什么原因?是要编辑模板吗,怎么解决?

解决方案 »

  1.   

    VB的查询代码如下:
            Dim Sqlserver As String
            Dim SQLstr As String
            SQLstr = " Select * From TABLE1"
            SQLstr &= " Where name like '%" & sub1.Text & "%'"
            Sqlserver = "server=GDSDXY-F7138C21\DB;database=TEASHOP;uid=sa;pwd=123456 "
            Dim conn As SqlConnection = New SqlConnection(Sqlserver)
            Dim mycommand As SqlCommand = New SqlCommand(SQLstr, conn)
            conn.Open()
            Dim mydatareader As SqlDataReader = mycommand.ExecuteReader()
            GridView1.DataSource = mydatareader
            GridView1.DataBind()
            mydatareader.Close()
            conn.Close()
      

  2.   

    应该放在If not IsPostBack里面吧。
      

  3.   

    sub page_load()
        if not ispostback then
            DataBind()  
        end if
    end sub
    sub DataBind()       
            Dim Sqlserver As String
            Dim SQLstr As String
            SQLstr = " Select * From TABLE1"
            if sub1.Text<>"" then
                 SQLstr &= " Where name like '%" & sub1.Text & "%'"
            end if
            Sqlserver = "server=GDSDXY-F7138C21\DB;database=TEASHOP;uid=sa;pwd=123456 "
            Dim conn As SqlConnection = New SqlConnection(Sqlserver)
            Dim mycommand As SqlCommand = New SqlCommand(SQLstr, conn)
            conn.Open()
            Dim mydatareader As SqlDataReader = mycommand.ExecuteReader()
            GridView1.DataSource = mydatareader
            GridView1.DataBind()
            mydatareader.Close()
            conn.Close()
    end sub
      

  4.   

    谢谢楼上的两位,但我是用Button_click触发查询事件的,不是在页面加载时运行的,不需要If not IsPostBack语句吧?!我现在在2005不需要写代码,只要设置一下GridView数据源的SQL语句就能实现。但是代码在2003里面还是不能显示数据,我发现SQL语句和代码都没有问题,直接输入查询内容就可以显示,但是根据TEXTBOX控件sub1.Text 的值查询就显示不了,问题应该就是sub1.Text的值没有传递到SQL语句里面,我试过把值赋给另一个变量再写到SQL语句里面,还是无法显示,狂晕~! 是否需要转换sub1.Text的值?怎么才能解决,请大家再赐教!!!
      

  5.   

    SQLstr &= " Where name like '%" & sub1.Text & "%'"
    改成SQLstr &= " Where name like '%' &'"& sub1.Text&"' & '%'"
    试试
      

  6.   

    SQLstr &= " Where name like '%" & sub1.Text & "%'"
    改成SQLstr &= " Where name like '%' +'"+ sub1.Text+"' + '%'"
    试试
    你可以用response.write sqlstr
            resposne.end()
    看你你的sql语句是否正确
      

  7.   

    按你的意思应该是要根据textbox里的值来查询数据。然后进行绑定。gridview不显示是没有数据。你设个断点,然后在即时窗口里看看sql语句具体是什么。你再到查询分析器里看看能不能查出数据。说不定少了个单引号呢。这种问题只能调试来查看了。你的做法应该是对的。
      

  8.   

    谢谢大家,现在成功了,估计是SQL语句的引号和空格造成的,散分给大家!!
      

  9.   

    sub1.Text改成sub1.text.tostring()试试