listView控件对数据库的增、删、改、查实例

来源:互联网 发布:sql 代表减的符号 编辑:程序博客网 时间:2024/06/06 08:52

接昨天的,今天弄listView控件的实例

 

其时对这个控件的数据库操作也是蛮简单的,开始的时候不懂,看了蛮多别人的贴子也不懂,现在总算有点小心得,贴出来与大家分享

 

界面如下(不用多说):

 

至于listView的一些设置这里就不多说啦!

 

 

代码如下:

 

Form1.cs源码:

 

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

namespace ListView控件增删改查
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //显示指定表中的全部信息
        public void listViewShow()
        {
            listView1.Items.Clear();

            string sql = string.Format("select AID,AName,APwd,AJob from Account");
            SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
            try
            {
                DBHelper.conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader["AID"].ToString());
                        item.SubItems.Add(reader["AName"].ToString());
                        item.SubItems.Add(reader["APwd"].ToString());
                        item.SubItems.Add(reader["AJob"].ToString());
                        listView1.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    MessageBox.Show("没有数据", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "数据库错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            DBHelper.conn.Close();
        }
       
        //查询功能
        private void SearchBtn_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            DBHelper.conn.Open();
            string sql = string.Format("select AID,AName,APwd,AJob from Account where AID='{0}'", textBox1.Text);

            SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
            try
            {
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem(reader["AID"].ToString());
                    item.SubItems.Add(reader["AName"].ToString());
                    item.SubItems.Add(reader["APwd"].ToString());
                    item.SubItems.Add(reader["AJob"].ToString());
                    listView1.Items.Add(item);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "数据库错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            DBHelper.conn.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listViewShow();
        }

        //添加功能
        private void AddBtn_Click(object sender, EventArgs e)
        {
            string x = textBox1.Text; 

            string y = textBox2.Text;

            string z = textBox3.Text;

            string i = textBox4.Text;

            DBHelper.conn.Open();

            string commandStr = "INSERT INTO Account(AID,AName,APwd,AJob) VALUES ('" + x + "','" + y + "','" + z + "','" + i + "')";     //创建插入语句
            SqlCommand command = new SqlCommand(commandStr, DBHelper.conn);
            MessageBox.Show("添加内容成功!");
            command.ExecuteNonQuery();
            DBHelper.conn.Close();

            listView1.Items.Clear();
            listViewShow();
        }

        //刷新功能
        private void RefurBtn_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            listViewShow();
        }

        //删除功能
        private void DelBtn_Click(object sender, EventArgs e)
        {
            //listView1.SelectedItems[0].Remove();  //只能移除控件上的,不能更新到数据库
            string sql = string.Format("delete from Account where AID = '{0}'", listView1.SelectedItems[0].SubItems[0].Text.Trim());
            SqlCommand com = new SqlCommand(sql, DBHelper.conn);
            DBHelper.conn.Open();
            com.ExecuteNonQuery();
            DBHelper.conn.Close();
            MessageBox.Show("成功删除记录,请按刷新按钮刷新显示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //鼠标点击列表,右边TextBox显示对应信息
        private void listView1_Click(object sender, EventArgs e)
        {
            textBox1.Text = listView1.SelectedItems[0].SubItems[0].Text.Trim();
            textBox2.Text = listView1.SelectedItems[0].SubItems[1].Text.Trim();
            textBox3.Text = listView1.SelectedItems[0].SubItems[2].Text.Trim();
            textBox4.Text = listView1.SelectedItems[0].SubItems[3].Text.Trim();
        }

        //退出功能
        private void CenBtn_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

       
    }
}

 

DBHelper.cs源码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ListView控件增删改查
{
    class DBHelper
    {
        public static string conStr = "Data Source =.;Initial Catalog = AworkeDB;Integrated Security=True";
        public static SqlConnection conn = new SqlConnection(conStr);
    }
}