当前位置:网站首页>C# 使用数据库对ListView控件数据绑定
C# 使用数据库对ListView控件数据绑定
2022-07-29 08:46:00 【InfoQ】
前言
每日一遍,防止颓废

1.将我们上一篇文章的项目打开,把静态的数据删除。我们采用数据库处理不会的同学需要看上一篇文章

2.使用数据库对ListView数据展示操作
2.1 双击窗体界面自动生成函数

2.2. 创建我们需要的数据库并写入值

2.3. 使用代码对ListView控件与数据库建立连接并绑定数据

2.4. 使用数据库对LIstVIew控件进行数据绑定查询效果展示

3.使用对话框对数据库创建搜索查询

4.加一个窗体实现新建文件操作
4.1 使用conTextMenuStrip控件实现右击新建

4.2 实现窗体和窗体之间的跳转

4.3 对”新建文件“窗体代码处理

5.整体代码展示,效果图

5.1窗体1核心代码:
private void Form1_Load(object sender, EventArgs e)
{
string connString = "server=.;database=mychat; User ID = sa; Pwd=123456";//写好数据库打开语句,博主使用的是SQL server数据库,如果你是MySQL 可以看博主之前的文章里面有讲解。
SqlConnection conn = new SqlConnection(connString);//实列化
conn.Open();//打开数据库
string sql = "select name,type,size,date,Imageid from listview";//查询语句
SqlCommand cmd = new SqlCommand(sql, conn);//建立连接
SqlDataReader reader = cmd.ExecuteReader();
listView1.Items.Clear();//清空listView
while (reader.Read())//查询结果遍历,并进行数据绑定
{
ListViewItem item = new ListViewItem(reader[0].ToString().Trim());//Trim()清空空格
item.ImageIndex = Convert.ToInt32(reader[4]);//绑定图标id
item.SubItems.Add(reader[1].ToString().Trim());//数据绑定
item.SubItems.Add(reader[2].ToString().Trim());
item.SubItems.Add(reader[3].ToString().Trim());
this.listView1.Items.Add(item);
}
reader.Close();
conn.Close();
}
private void button4_Click(object sender, EventArgs e)
{
string connString = "server=.;database=mychat; User ID = sa; Pwd=123456";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string sql =string.Format("select name,type,size,date,Imageid from listview where name like '%{0}%';", textBox1.Text);//实现模糊查询
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
listView1.Items.Clear();
while (reader.Read())//添加数据和上面代码一致
{
ListViewItem item = new ListViewItem(reader[0].ToString().Trim());
item.ImageIndex =Convert.ToInt32(reader[4]);
item.SubItems.Add(reader[1].ToString().Trim());
item.SubItems.Add(reader[2].ToString().Trim());
item.SubItems.Add(reader[3].ToString().Trim());
this.listView1.Items.Add(item);
}
reader.Close();
conn.Close();
}
private void 新建文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
5.2 窗体二核心代码:
private void button2_Click(object sender, EventArgs e)//重置按钮
{
textBox1.Clear();//对textbox清空
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}
private void button1_Click(object sender, EventArgs e)//添加按钮
{
string connString = "server=.;database=mychat; User ID = sa; Pwd=123456";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string sql = string.Format("INSERT INTO listview(name, type, size, date, Imageid) VALUES('{0}','{1}','{2}','{3}',{4});",textBox1.Text, textBox2.Text, textBox3.Text+"MB", textBox4.Text,Convert.ToInt32(textBox5.Text));//添加数据语句
SqlCommand cmd = new SqlCommand(sql, conn);
int x =cmd.ExecuteNonQuery();//添加操作,成功了返回你添加数据的条数,否则为0
if (x > 0)
{
MessageBox.Show("添加成功");
this.Close();//添加成功,关闭这个窗体
}
else
MessageBox.Show("添加失败");
}
}
总结

边栏推荐
- 2022 Shandong Province safety officer C certificate work certificate question bank and answers
- How to quickly experience oneos
- Week 2: convolutional neural network basics
- MySQL statement mind map
- Chrony 时间同步
- Common query optimization technology of data Lake - "deepnova developer community"
- 2022 P cylinder filling test simulation 100 questions simulation test platform operation
- QT version of Snake game project
- Markdown concise grammar manual
- Can the access database be accessed remotely
猜你喜欢

Qpalette learning notes

MySQL statement mind map

Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)

Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)

01-01-osg GL3 environment setup

2022 question bank and answers of operation certificate examination for main principals of hazardous chemical business units

7.2-function-overloading

Sword finger offer 50. the first character that appears only once

What if official account does not support markdown format file preparation?

ADB common command list
随机推荐
Data is the main body of future world development, and data security should be raised to the national strategic level
Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
多重背包,朴素及其二进制优化
BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
Curl -v | JQ
MySQL statement mind map
To create a thread pool for the rate, start the core thread
Design of distributed (cluster) file system
Application of matrix transpose
A little knowledge [synchronized]
Opencv cvcircle function
Ar virtual augmentation and reality
Amazfit dial toolbox Online
C language -- 22 one dimensional array
7.1-default-arguments
Vs2019 compilation cryengine failure problem handling
Osgsimplegl3 combined with renderdoc tool
Cloud security daily 220712: the IBM integration bus integration solution has found a vulnerability in the execution of arbitrary code, which needs to be upgraded as soon as possible
Can the access database be accessed remotely