using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string qry = @"select * from s Condition"; SqlConnection cnn = new SqlConnection(@"Data Source=192.168.0.111;Initial Catalog=db1;Persist Security Info=True;User ID=ali;password=aaazzz"); cnn.Open(); if(textBox1.Text!="") qry = qry.Replace("Condition","where Sname like '"+ textBox1.Text+"%'"); SqlCommand cmd = new SqlCommand(qry,cnn); SqlDataReader dr; dr = cmd.ExecuteReader(); listBox1.Items.Clear(); while (dr.Read()) { listBox1.Items.Add(dr["Sname"].ToString()); } cnn.Close(); } private void textBox1_TextChanged(object sender, EventArgs e) { button1_Click(sender, e); } } }