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);
}
}
- listView控件对数据库的增、删、改、查实例
- DataGridView控件对数据库的增、删、改、查实例
- ListView控件(一)--增,删,改,查
- ListView控件(一)--增,删,改,查
- ListView控件(一)--增,删,改,查
- Android对ListView控件增删改查
- Android对ListView控件增删改查
- 【Visual Basic】vb6的ListView控件,对Access2003数据库的增删改查,判断是否有中文、多窗体操作
- 对ACCESS数据库增,删,查,改的源码(zz)
- Android数据库知识点(实例)增删改查,listview显示
- 对JDBC进行简单的封装,实现对数据库的增、删、改、查功能
- 【C#】ListView的使用,对Access数据库的增删改查
- 对mysql数据库的增删改查
- vickate_iOS_FMDB 对数据库的增删改查
- JDBC对数据库的增删改查
- Java对数据库的增删查改
- pymysql 对数据库的增删改查
- 使用NHibernate对数据库实现增、删、改、查操作
- 悬而未决:APACHE上开启mod_fcgid.so后打开PHP一片空白
- 如何写恶意插件
- 使用libpng,libjpeg从文件读取和从内存读取2中方式的实现
- 解剖 JavaScript 中的 null 和 undefined
- 关于using filesort(一)
- listView控件对数据库的增、删、改、查实例
- QT视图框架
- 锋利的jquery 笔记 第1~2章
- cairo介绍
- asp伪装成jsp的方法
- Not Found ComponentDesigner.dcu 解决方法
- 一份招PHP高级工程师的面试题
- abstract class和interface有什么区别
- J2me socket