对于Windows桌面VB.NET应用程序,我试图仅在用户键入3个字母或更多字母时调用自动建议。该数据库包含数千行,我不想在表单加载时加载所有行。我想只在需要时从数据库获取,并且只能匹配前几个字母的项目。
到目前为止我已经
了Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If Len(TextBox1.Text) >= 3 Then
'invoke autosuggest
End If
End Sub
然后不确定如何调用autosuggest以便用户不必输入全文,我的查询将是这样的:
Select Fullname, email from Contact where fullname like & textbox1.text order by full name
非常感谢任何指示或代码建议。
答案 0 :(得分:1)
您可以将查询更改为:
Dim cmd As SqlCommand
Dim con As SqlConnection ='your connexion string here
Dim csql As String = "Select Fullname, email from Contact where fullname like @SEARCH order by full name"
cmd = New SqlCommand(csql, con)
cmd.Parameters.AddWithValue("@SEARCH", "'" + TextBox1.Test + "%'")
cmd.ExecuteNonQuery()
dalso use
If TextBox1.Text.Length >3 Then
'invoke autosuggest
End If
而不是
If Len(TextBox1.Text) >= 3 Then
'invoke autosuggest
End If