2015-02-23 115 views
0

當我把文字信箱放入文本框時,我該怎麼辦?visual studio 2010使用文本框搜索

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtsearch.TextChanged 
     Dim SqlQuery As String = "SELECT * FROM tblsample WHERE fName LIKE '%" & Txtsearch.Text & "%' " 

     Dim SqlCommand As New OleDb.OleDbCommand 

     Dim SqlAdpter As New OleDb.OleDbDataAdapter 

     Dim TABLE As New DataTable 

     With SqlCommand 
      .CommandText = SqlQuery 
      .Connection = conn 

     End With 

     With SqlAdpter 
      .SelectCommand = SqlCommand 
      .Fill(TABLE) 
     End With 

     lvw.Items.Clear() 
     For i = 0 To TABLE.Rows.Count - 1 
      With lvw 
       .Items.Add(TABLE.Rows(i)("uid")) 
       With .Items(.Items.Count - 1).SubItems 
        .Add(TABLE.Rows(i)("fname")) 
        .Add(TABLE.Rows(i)("lname")) 
        .Add(TABLE.Rows(i)("me")) 
       End With 
      End With 
     Next 
    End Sub 
End Class 

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtsearch.TextChanged 
    Dim SqlQuery As String = "SELECT * FROM tblsample WHERE fName LIKE '%" & Txtsearch.Text & "%' " 

    Dim SqlCommand As New OleDb.OleDbCommand 

    Dim SqlAdpter As New OleDb.OleDbDataAdapter 

    Dim TABLE As New DataTable 

    With SqlCommand 
     .CommandText = SqlQuery 
     .Connection = conn 

    End With 

    With SqlAdpter 
     .SelectCommand = SqlCommand 
     .Fill(TABLE) 
    End With 

    lvw.Items.Clear() 
    For i = 0 To TABLE.Rows.Count - 1 
     With lvw 
      .Items.Add(TABLE.Rows(i)("uid")) 
      With .Items(.Items.Count - 1).SubItems 
       .Add(TABLE.Rows(i)("fname")) 
       .Add(TABLE.Rows(i)("lname")) 
       .Add(TABLE.Rows(i)("me")) 
      End With 
     End With 
    Next 
End Sub 
+0

說出__錯誤_而不說什麼是錯誤並不是很有幫助。請添加收到的錯誤消息。 – Steve 2015-02-23 09:47:32

+0

抱歉,這是錯誤----- >>。填充(TABLE)ConnectionString屬性尚未初始化。 – user3342642 2015-02-23 09:51:02

+0

這是非常基本的。當你想連接到一個數據庫時,你使用了一個DbConnection派生類的實例。這個類需要一個幫助定位數據庫的基本屬性。它被稱爲ConnectionString。變量con沒有ConnectionString,沒有這個屬性你就無法連接任何東西。 [看到這個網站的ConnectionStrings的例子](http://www.connectionstrings.com) – Steve 2015-02-23 09:53:31

回答

0

錯誤很明顯。 conn全局變量沒有設置ConnectionString。爲什麼你要保留一個全局連接對象?相反,請將字符串與ConnectionString屬性保持爲全局,或者每次從配置文件中讀取它。然後在現場建立SqlConnection並在完成使用後立即銷燬。

ADO.NET實現連接池基礎結構併爲連接保留一個全局對象是一種不好的做法。

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtsearch.TextChanged 

    Dim SqlQuery As String = "SELECT * FROM tblsample " & _ 
          "WHERE fName LIKE @search" 
    Dim cnString as String = GetConnectionString() 
    Using conn = new SqlConnection(cnString)  
    Using cmd = New OleDb.OleDbCommand(SqlQuery, conn) 
    Using SqlAdpter = New OleDb.OleDbDataAdapter() 
     Dim TABLE As New DataTable 
     cmd.Parameters.Add("@search", _ 
      SqlDbType.NVarChar, 255).Value = "%" & Txtsearch.Text & "%" 
     With SqlAdpter 
      .SelectCommand = SqlCommand 
      .Fill(TABLE) 
     End With 
    End Using 
    End Using 
    End Using 

    lvw.Items.Clear() 
    For i = 0 To TABLE.Rows.Count - 1 
     With lvw 
      .Items.Add(TABLE.Rows(i)("uid")) 
      With .Items(.Items.Count - 1).SubItems 
       .Add(TABLE.Rows(i)("fname")) 
       .Add(TABLE.Rows(i)("lname")) 
       .Add(TABLE.Rows(i)("me")) 
      End With 
     End With 
    Next 
End Sub 

在上面的例子中,所有與數據庫工作的變量是局部的,從子退出之前在本地創建和銷燬。唯一需要定義的部分是對GetConnectionString的調用。這應該是你的代碼中的一個函數,它返回用於SqlConnection的ConnectionString,你可以用很多方式準備它(固定文本(不推薦),讀取你的web.Config或App.Config(推薦))

+0

很好的解釋!現在它的工作感謝您的幫助:) :) :) – user3342642 2015-02-23 10:25:46