2013-09-25 67 views
0

我有3個級別的訪問權限,學生,管理員和INSTRUCTOR在一個表中。所以我只想顯示那些是「INSTRUCTOR」的人。感謝我只是一個新手在編碼。如何過濾將顯示到datagridview中的數據庫?

Me.cboSearchBy.SelectedIndex = 0 
     Dim conn As New OleDbConnection 
     Dim cmd As New OleDbCommand 
     Dim da As New OleDbDataAdapter 
     Dim dt As New DataTable 
     Dim sSQL As String = String.Empty 

     Try 

      conn = New OleDbConnection(Get_Constring) 
      conn.Open() 
      cmd.Connection = conn 
      cmd.CommandType = CommandType.Text 
      sSQL = "SELECT Username, lname + ', ' + fname + ' ' + mname as name, password FROM Instructor" 
      If Me.cboSearchBy.Text = "Name" Then 
       sSQL = sSQL & " where lname + ', ' + fname + ' ' + mname like '%" & Me.txtSearch.Text & "%'" 
      Else 
       sSQL = sSQL & " where Username =" & Me.txtSearch.Text 
      End If 
      cmd.CommandText = sSQL 
      da.SelectCommand = cmd 
      da.Fill(dt) 

      Me.dtgResult.DataSource = dt 
      If dt.Rows.Count = 0 Then 
       MsgBox("No record found!") 
      End If 

     Catch ex As Exception 
      MsgBox(ErrorToString) 
     Finally 
      conn.Close() 
     End Try 
+0

你的意思是你有3個表格,1個是STUDENT,1個是ADMIN,另一個是INSTRUCTOR? –

+0

只有1個表,我想只顯示那些在datagridview中的講師,因爲那些3在一個表中。 – user2715202

+0

只有1個表... – user2715202

回答

2

用這個替換您的查詢部分;

 sSQL = "SELECT Username, lname + ', ' + fname + ' ' + mname as name, password FROM Instructor" 
    If Me.cboSearchBy.Text = "Name" Then 
     sSQL = sSQL & " where lname + ', ' + fname + ' ' + mname like '%" & Me.txtSearch.Text & "%'" 
     sSQL = sSQL & " and level like '%instructor%'" 
    Else 
     sSQL = sSQL & " where Username =" & Me.txtSearch.Text 
     sSQL = sSQL & " and level like '%instructor%'" 
    End If 
+0

非常感謝你:) – user2715202

0

雖然@Nadeem_MK有首選方法,讓數據庫做最擅長的,有你想要的所有數據時代什麼,你需要在本地將其過濾(沒有其它調用數據庫)。你可以這樣做的一種方法是:

...Your code 
da.SelectCommand = cmd 
da.Fill(dt) 
'Filter locally 
dim drs as datarow() = dt.select("level = 'Instructor'") 
'Use the datarow array as your datasource instead of the data table 
Me.dtgResult.DataSource = drs 

或者,如果你使用的是「BindingSource」,你也可以過濾它。

相關問題