我把可能出错的地方再重新帖出来,方便各位看一下 呵呵,开始不知道是什么地方出错了,就把条件语句改成最简单的格式,发现还是不行,后来才查出来应该是数据类型的问题Sub btnClick_Search(S As Object,E As EventArgs) Dim username As String Dim userid As String username=txtSuser.Text userid=txtSID.Text conMyData=New SqlConnection("Server=Localhost;UID=sa;PWD=1;DataBase=Exam") If txtSID.Text = "" and txtSuser.Text = "" Then cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where dept.id=userlist.did order by userlist.id",conMyData) End If If txtSID.Text <> "" and txtSuser.Text = "" Then cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where userlist.id=@id and dept.id=userlist.did order by userlist.id",conMyData) End If If txtSuser.Text <> "" and txtSID.Text = "" Then cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where username=@name and dept.id=userlist.did order by userlist.id",conMyData) End If If txtSuser.Text <> "" and txtSID.Text <> "" Then cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where username=@name and userlist.id=@id and dept.id=userlist.did order By userlist.id",conMyData) End If cmdSelect.Parameters.Add("@id",userid) cmdSelect.Parameters.Add("@name",username) conMyData.Open() dgrdUser.DataSource=cmdSelect.ExecuteReader() dgrdUser.DataBind() conMyData.Close() End Sub
OleDbDataAdapter da = new OleDbDataAdapter(sql, Conn)
DataTable dt = new DataTable()
da.Fill(dt)
dgrdUser.DataSource = dt
dgrdUser.DataBind()
ExecuteReader 返回是个DataReader是个只进流,应该用while(Reader.Read()) 来遍历行。为何不用DataSet?
DataAdapter Adapter = new DataAdapter(cmdSelect.Text,conMyData)
DataSet Dt = new DataSet();
Adapter.Fill(Dt);
dgrdUser.DataSource= Dt;
不好意思啊
一时心急,就把他全发上来了
应该是数据类型的问题
我已经解决了下拉列表的查询问题
我是初学这个的
对很多变量的类型转换什么的都不怎么清楚
如果用5楼的兄弟的方法,使用dataset 是不是可以转换成string 类型呢?
我刚刚也发现了,如果输入数字的话是可以查询的,但是输入汉字就不行了
呵呵,开始不知道是什么地方出错了,就把条件语句改成最简单的格式,发现还是不行,后来才查出来应该是数据类型的问题Sub btnClick_Search(S As Object,E As EventArgs)
Dim username As String
Dim userid As String
username=txtSuser.Text
userid=txtSID.Text
conMyData=New SqlConnection("Server=Localhost;UID=sa;PWD=1;DataBase=Exam")
If txtSID.Text = "" and txtSuser.Text = "" Then
cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where dept.id=userlist.did order by userlist.id",conMyData)
End If
If txtSID.Text <> "" and txtSuser.Text = "" Then
cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where userlist.id=@id and dept.id=userlist.did order by userlist.id",conMyData)
End If
If txtSuser.Text <> "" and txtSID.Text = "" Then
cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where username=@name and dept.id=userlist.did order by userlist.id",conMyData)
End If
If txtSuser.Text <> "" and txtSID.Text <> "" Then
cmdSelect=New SqlCommand("Select userlist.*,dept.* from userlist,dept where username=@name and userlist.id=@id and dept.id=userlist.did order By userlist.id",conMyData)
End If
cmdSelect.Parameters.Add("@id",userid)
cmdSelect.Parameters.Add("@name",username) conMyData.Open()
dgrdUser.DataSource=cmdSelect.ExecuteReader()
dgrdUser.DataBind()
conMyData.Close()
End Sub