這裏是我用於搜索的代碼,當我們鍵入一個完整的名字時它會返回一個值,但是我需要一個自動完成的文本框,當我輸入部分名稱時顯示建議。如何在wpf文本框中進行自動填充?
private void textBox3_KeyUp_1(object sender, System.Windows.Input.KeyEventArgs e)//Name Search
{
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select [Patient ID],[Patient Name],[Gender],[StudyDateTim],[Modality],[Study Name] From RepView Where [Patient Name] like '%" + textBox3.Text + "%'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
con.Close();
}
你不需要'ExecuteNonQuery'在你的情況。你應該總是使用[參數化查詢](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。這種字符串連接對於[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻擊是開放的。還可以使用_using statement_來處理數據庫連接。 – 2014-09-05 10:08:56
@SonerGönül:但是我不知道如何編碼 – Ramji21 2014-09-05 10:10:24
@ Ramji21:在這種情況下學習代碼。我的意圖不是難,但如果你對自己的工作沒有最基本的理解,不要期望創造出好的計劃。 – 2014-09-05 10:18:49