其他分享
首页 > 其他分享> > 窗体通讯录展示

窗体通讯录展示

作者:互联网

https://blog.csdn.net/jiahui513/article/details/110749202

窗体

实现增删改查功能

一,主页面

在这里插入图片描述

二,查询窗口

在这里插入图片描述
代码连接

 SqlConnection conn =new SqlConnection("Data Source=.;Initial Catalog=dbok;User ID=sa;Password=cnm.040513");
         
           

            string sql = "select * from QQcard where qqname = "+textBox1.Text;
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader result = cmd.ExecuteReader();
           while(result.Read())
            {
               
                
                string tempName = result["qqname"].ToString();//查询姓名
                string tempAge = result["qqnameber"].ToString();//qq号
              
                ListViewItem item = new ListViewItem(tempName);
                listView1.Items.Add(item);
                item.SubItems.Add(tempAge);
                
                
            }
           conn.Close();

搜索功能代码展示

listView1.Items.Clear();
            string ww = "";
            int ww1 = 0;
            try { ww1 = Convert.ToInt32(textBox1.Text); }
            catch { ww = textBox1.Text; }
            String sql2 = "select * from QQcard where nickname = '"+ww+"' or qqid= "+ww1+";";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql2, conn);
            try
            {
                SqlDataReader result = cmd.ExecuteReader();
                result.Read();
                string tempName = result["qqname"].ToString();//查询姓名
                string tempAge = result["qqnameder"].ToString();//qq号
                
                ListViewItem item = new ListViewItem(tempName);
                listView1.Items.Add(item);
                item.SubItems.Add(tempAge);
               
                conn.Close();
            }
            catch {
                conn.Close();
                MessageBox.Show("请输入内容!");
            }

右键删除功能代码展示

MessageBox.Show(listView1.SelectedItems[0].SubItems[1].Text);
            
            String sql = String.Format("delete from QQcard where qqnameber  = {0}", listView1.SelectedItems[0].SubItems[1].Text);

            MessageBox.Show(sql);
            //开启对象
            conn.Open();

            SqlCommand cmd = new SqlCommand(sql, conn);

            
            int n = cmd.ExecuteNonQuery();

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

            //关一下
            conn.Close();

            //清空listview对象中的项
            listView1.Items.Clear();

            //重新加载load一下load
            loadData();

新增代码展示

String sql = String.Format("insert into QQCard(qqname,qqnumber) values('五一',22)");
            MessageBox.Show(sql);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataReader reader = cmd.ExecuteReader();
            conn.Close();
            lvCards.Items.Clear();
            loadData();

标签:展示,cmd,窗体,result,sql,new,SqlCommand,conn,通讯录
来源: https://blog.csdn.net/yangxueqin0415/article/details/110790914