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();
}
系统应用管理 C# 第三章课后5
最新推荐文章于 2017-02-24 16:01:36 发布