标签:
namespace WindowsFormsApp { public partial class listview : Form { private void func() { listView1.Items.Clear(); string sql = string.Format("select StudentId,LoginId,StudentName,UserStateId,Sex from Student where LoginId like ‘%{0}%‘", textBox1.Text); string connStr = DBHelp.connStr; using (SqlConnection conn = new SqlConnection(connStr)) { SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { ListViewItem item = new ListViewItem(dr["LoginId"].ToString()); string active = dr[3].ToString() == "1" ? "活动" : "非活动"; item.Tag = dr["StudentId"].ToString(); item.SubItems.AddRange(new string[] { dr["StudentName"].ToString(), active, dr["Sex"].ToString() }); listView1.Items.Add(item); } dr.Close(); } } private void SetActive(int state) { int id = Convert.ToInt32(listView1.SelectedItems[0].Tag); string connStr = DBHelp.connStr; using (SqlConnection conn = new SqlConnection(connStr)) { string sql = string.Format("update Student set UserStateId=@state where StudentId=@id"); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@id", id); cmd.Parameters.AddWithValue("@state", state); conn.Open(); cmd.ExecuteNonQuery(); } } private void SetDelete() { int id = Convert.ToInt32(listView1.SelectedItems[0].Tag); string connStr = DBHelp.connStr; using (SqlConnection conn = new SqlConnection(connStr)) { string sql = string.Format("delete from Student where StudentId=@id"); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@id", id); conn.Open(); cmd.ExecuteNonQuery(); } } public listview() { InitializeComponent(); } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { } private void listview_Load(object sender, EventArgs e) { func(); } private void button1_Click(object sender, EventArgs e) { func(); } private void 活动ToolStripMenuItem_Click(object sender, EventArgs e) { int state = 1; SetActive(state); func(); } private void 非活动ToolStripMenuItem_Click(object sender, EventArgs e) { int state = 0; SetActive(state); func(); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { SetDelete(); func(); } } }
标签:
原文地址:http://www.cnblogs.com/Penfield/p/4350244.html