扣扣通讯1203 演练中的代码

144 阅读2分钟

主窗体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication49
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 实例化名片列表窗体,显示
            fmCardList cl = new fmCardList();
            cl.Show();

        }
    }
}

第二个窗体的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication49
{
    public partial class fmCardList : Form
    {
        // 定义一个全局的连接对象的变量
        SqlConnection conn = null;

        public fmCardList()
        {
            InitializeComponent();
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            //  找到被选中的项
            String qqnumber = lvCards.SelectedItems[0].SubItems[1].Text;

            // 要做的事情
            String sql = String.Format("delete from QQCard where qqnumber = {0}", qqnumber);

            // 连接对象开启
            conn.Open();

            // 生成执法者
            SqlCommand cmd = new SqlCommand(sql,conn);

            // 执法者调用方法 执行不查询的方法
            int n = cmd.ExecuteNonQuery();

            MessageBox.Show("受影响的行数为" + n);
            

            // 假设需要删除吕布
            // delete from 表名 where name='吕布'

            // 关一下
            conn.Close();
            1
            // 清空listview对象中的项
            lvCards.Items.Clear();

            // 重新load一下data
            loadData();
        }

        private void tsmiAddItem_Click(object sender, EventArgs e)
        {
            // 往listview对象中添加数据,先加假数据
            // 获得listview的项的集合,往集合中中一个item对象
            // 实例化一个项,添加到集合中

            // 类名 变量名 = new 类名;
            ListViewItem tempItem = new ListViewItem();
            tempItem.SubItems.Add("1111111");
            // 对象.属性 = 值
            tempItem.Text = "张飞";

            // 往集合中添加这个项
            lvCards.Items.Add(tempItem);
        }

        private void fmCardList_Load(object sender, EventArgs e)
        {
            

            // 读取数据库 获得所有的数据 并且添加到listview中
            loadData();
        }

        public void loadData() {
            // 实例化一个连接对象
            // 连接字符串
            String connStr = "Data Source=.;Initial Catalog=dbok;Integrated Security=True";
            // 连接对象
            conn = new SqlConnection(connStr);

            // 创建执法者
            String sql = "select * from QQCard";
            SqlCommand cmd = new SqlCommand(sql,conn);


            // 打开连接
            conn.Open();

            // 让执法者获取所有的数据
            SqlDataReader reader =cmd.ExecuteReader();

            // 遍历处理数据表中的所有内容
            String name;
            String qqnumber;
            while(reader.Read()){
                name = reader["qqname"].ToString();
                qqnumber = reader["qqnumber"].ToString();
                // 生成一个项
                ListViewItem temp_item = new ListViewItem();
                // 给项添加文本
                temp_item.Text = name;
                // 给项添加子项
                temp_item.SubItems.Add(qqnumber);
                // 把项添加到lv对象的Items集合中
                lvCards.Items.Add(temp_item);

            }

            // 读数据。。。

            // 填入到listview对象的items列表中

            // 关闭连接对象
            conn.Close();
        }
    }
}