系統應用管理 C# 第三章課後5
阿新 • • 發佈:2019-01-10
public string s ="Data Source=.;Initial Catalog=chaoshi;Integrated Security=True"; private void CS() { if(listView1.Items.Count>0){ listView1.Items.Clear(); } StringBuilder sb = new StringBuilder(); sb.AppendLine("SELECT [Name],[Type],[Number],[Price] FROM [chaoshi].[dbo].[shangpin]"); sb.AppendFormat("WHERE [Type] LIKE '{0}'", this.textBox1.Text.Trim()); MySchool.DBHelper a = new MySchool.DBHelper(); try { SqlCommand command = new SqlCommand(sb.ToString(), a.Connection); a.OpenConnection(); SqlDataReader reader = command.ExecuteReader(); if (!reader.HasRows) { MessageBox.Show("沒有記錄", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { while (reader.Read()) { string name = reader["Name"].ToString(); string type = reader["Type"].ToString(); string number = reader["Number"].ToString(); string price = reader["Price"].ToString(); ListViewItem item = new ListViewItem(name); item.SubItems.Add(type); item.SubItems.Add(number); item.SubItems.Add(price); listView1.Items.Add(item); } reader.Close(); } } catch (Exception e) { MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { a.CloseConnection(); } } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) {CS(); }