private string link = "server=.;database=list;user=sa;pwd=123"; public void chaxun() //創建一個查詢函數 { SqlConnection coon = new SqlConnection(link); //連接數據庫 coon.Open();//打開數據庫 SqlCommand cmd = coon.CreateCommand();//創建命令 cmd.CommandText = "select * from listview";//寫命令內容 SqlDataReader dr = cmd.ExecuteReader();//執行命令 int index = 0; //定義listview.Items的索引 listView1.Items.Clear(); //先將listView1.Items清空; while (dr.Read()) //循環條件,里面有數據 { //將數據一條一條增加到listView.Items的集合中 listView1.Items.Add(dr["code"].ToString());//Items代表的是每列 listView1.Items[index].SubItems.Add(dr["name"].ToString());//SubItems其他列 listView1.Items[index].SubItems.Add(dr["pass"].ToString()); index++; } cmd.Dispose(); coon.Close(); }
private void button1_Click(object sender, EventArgs e) //查詢 { SqlConnection coon = new SqlConnection(link);//連接數據庫 coon.Open();//打開數據庫 SqlCommand cmd = coon.CreateCommand();//創建命令 cmd.CommandText = "select * from listview";//寫命令內容 SqlDataReader dr = cmd.ExecuteReader();//執行命令 int index = 0;//定義listview.Items的索引 listView1.Items.Clear();//先將listView1.Items清空; while (dr.Read())//循環條件,里面有數據 { //將數據一條一條增加到listView.Items的集合中 listView1.Items.Add(dr["code"].ToString());//Items代表的是每列 listView1.Items[index].SubItems.Add(dr["name"].ToString());//SubItems其他列 listView1.Items[index].SubItems.Add(dr["pass"].ToString()); index++;//索引++ ,再循環 } cmd.Dispose();//命令清空 coon.Close(); //數據庫關閉 }
private void button2_Click(object sender, EventArgs e) //刪除 { if(listView1.SelectedItems.Count>0) //如果選中的要刪除數據的數量大於0才可以執行刪除,否則不執行 { string scode = listView1.SelectedItems[0].Text.ToString(); //創建一個變量來接收第一列索引的文本內容 SqlConnection coon = new SqlConnection(link);//連接數據庫 coon.Open();//打開數據庫 SqlCommand cmd = coon.CreateCommand();//創建命令 cmd.CommandText = "delete from listview where code='" + scode + "'";//寫命令內容 cmd.ExecuteNonQuery(); //執行命令 cmd.Dispose();//命令清空 coon.Close();//數據庫關閉 chaxun(); //執行查詢函數 } }
private void button3_Click(object sender, EventArgs e) //主頁增加按鈕 { add f = new add(); f.Show(); //彈出增加框 }
private void button1_Click(object sender, EventArgs e) //增加 { if(textBox2.Text!=null&&textBox3.Text!=null) //只有當文本欄不為空才能執行增加,否則點增加沒反應 { string code=textBox1.Text; //第一欄的文本 string name=textBox2.Text; //第二欄的文本 string pass=textBox3.Text; //第三欄的文本 SqlConnection coon = new SqlConnection("server=.;database=list;user=sa;pwd=123");//連接數據庫 coon.Open();//打開數據庫 SqlCommand cmd = coon.CreateCommand();//創建命令 cmd.CommandText = "insert into listview values('"+name+"','"+pass+"')";//寫命令語句 cmd.ExecuteNonQuery();//執行命令 cmd.Dispose();//清空命令 coon.Close();//關閉數據庫 MessageBox.Show("增加成功"); } }
private void button4_Click(object sender, EventArgs e) //主頁修改按鈕 { if(listView1.SelectedItems.Count>0) //選中要修改的內容才能執行修改 { string code = listView1.SelectedItems[0].Text; //將所選中文本的第一列的內容賦給code update f = new update(code);//將code扔進去 是為了讓修改窗體接收這個code值 (鼠標右鍵 轉到定義) f.Show(); } }
private string Code; public update(string code) //將主頁面所選中內容的code值傳給窗體update,並且顯示在窗體update的第一欄文本內 { InitializeComponent(); Code = code;//傳值 textBox1.Text = Code;//顯示 } private void button1_Click(object sender, EventArgs e) //修改 { if(textBox2.Text!=null&&textBox3.Text!=null) //只有當文本欄內容不為空才會執行修改 { string name = textBox2.Text;//第二欄文本 string pass = textBox3.Text;//第三欄文本 SqlConnection coon = new SqlConnection("server=.;database=list;user=sa;pwd=123");//連接數據庫 coon.Open();//打開數據庫 SqlCommand cmd = coon.CreateCommand();//創建命令 cmd.CommandText = "update listview set name='"+name+"',pass='"+pass+"' where code="+Code+"";//寫命令語句 cmd.ExecuteNonQuery();//執行命令 cmd.Dispose();//清空命令 coon.Open();//關閉數據庫 MessageBox.Show("修改成功"); } }