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()
应该放在If not IsPostBack里面吧。
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
谢谢楼上的两位,但我是用Button_click触发查询事件的,不是在页面加载时运行的,不需要If not IsPostBack语句吧?!我现在在2005不需要写代码,只要设置一下GridView数据源的SQL语句就能实现。但是代码在2003里面还是不能显示数据,我发现SQL语句和代码都没有问题,直接输入查询内容就可以显示,但是根据TEXTBOX控件sub1.Text 的值查询就显示不了,问题应该就是sub1.Text的值没有传递到SQL语句里面,我试过把值赋给另一个变量再写到SQL语句里面,还是无法显示,狂晕~! 是否需要转换sub1.Text的值?怎么才能解决,请大家再赐教!!!
SQLstr &= " Where name like '%" & sub1.Text & "%'" 改成SQLstr &= " Where name like '%' &'"& sub1.Text&"' & '%'" 试试
SQLstr &= " Where name like '%" & sub1.Text & "%'" 改成SQLstr &= " Where name like '%' +'"+ sub1.Text+"' + '%'" 试试 你可以用response.write sqlstr resposne.end() 看你你的sql语句是否正确
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()
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
改成SQLstr &= " Where name like '%' &'"& sub1.Text&"' & '%'"
试试
改成SQLstr &= " Where name like '%' +'"+ sub1.Text+"' + '%'"
试试
你可以用response.write sqlstr
resposne.end()
看你你的sql语句是否正确