吉吉于

C#每天抄一点(35):利用ListView导航数据库信息

01 using System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Linq;
07 using System.Text;
08 using System.Windows.Forms;
09
10 using System.Data.OleDb;
11
12 namespace Case05_4
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20
21         OleDbConnection Lazy_con;
22         string Lazy_music;
23
24         public void getScoure(string str_name)
25         {
26             string Lazy_sql = “select * from LazyData where 歌曲 = ‘” + Lazy_music + “‘”;
27             //Sql查询语句
28             OleDbCommand mycom = new OleDbCommand(Lazy_sql, Lazy_con);
29             //定义OleDbCommand对象实例并连接数据库
30             OleDbDataReader Lazy_read = mycom.ExecuteReader();
31             //定义OleDbDataReader对象实例
32             while (Lazy_read.Read())
33             {
34                 this.textBox1.Text = Lazy_read[1].ToString();
35                 //把数据库表中的记录显示在相应的文本框中
36                 this.textBox2.Text = Lazy_read[2].ToString();
37                 this.textBox3.Text = Lazy_read[3].ToString();
38                 this.textBox4.Text = Lazy_read[4].ToString();
39                 this.textBox5.Text = Lazy_read[5].ToString();
40             }
41             Lazy_read.Close();
42             Lazy_read.Dispose();
43         }
44
45         private void Form1_Load(object sender, EventArgs e)
46         {
47             string Lazy_path = Application.StartupPath + “\\Lazy_Data.accdb”;
48             //连接数据库的路径及数据库名
49             string constr = “Provider = Microsoft.ACE.OLEDB.12.0; Data Source =” + Lazy_path;
50             //生成连接数据库字符串
51             Lazy_con = new OleDbConnection(constr);
52             //定义OleDbConnection对象实例并连接数据库
53             Lazy_con.Open();
54             string Lazy_sql = “select * from LazyData”;
55             //Sql查询语句
56             OleDbCommand mycom = new OleDbCommand(Lazy_sql, Lazy_con);
57             //定义OleDbCommand对象实例并连接数据库
58             OleDbDataReader Lazy_read = mycom.ExecuteReader();
59             //定义OleDbDataReader对象实例
60             listView1.FullRowSelect = true;
61             while (Lazy_read.Read())
62             {
63                 ListViewItem Lazy_item = new ListViewItem(Lazy_read[1].ToString());
64                 listView1.Items.Add(Lazy_item);
65                 //把数据库表中"歌曲"字段信息添加到ListView控件中
66             }
67             Lazy_read.Close();
68             Lazy_read.Dispose();
69         }
70
71         private void listView1_Click(object sender, EventArgs e)
72         {
73             Lazy_music = listView1.SelectedItems[].SubItems[].Text;
74             if (Lazy_music != null)
75             {
76                 getScoure(Lazy_music);
77             }
78         }
79
80
81     }
82 }

下载源码

转载请注明:于哲的博客 » C#每天抄一点(35):利用ListView导航数据库信息