C#探秘系列(九)WPF连接Mysql数据库
来源:互联网 发布:丹东盘古网络技术支持 编辑:程序博客网 时间:2024/04/29 06:22
两个函数即可搞定:
private void GetData() { MySqlConnection connection = new MySqlConnection(myConnectionString); connection.Open(); if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); LoadData(); } } private void LoadData() { MySqlConnection connection = new MySqlConnection(myConnectionString); connection.Open(); try { MySqlCommand cmd = connection.CreateCommand(); cmd.CommandText = "select * from notes"; MySqlDataAdapter adap = new MySqlDataAdapter(cmd); DataSet ds = new DataSet(); adap.Fill(ds); //this.clothesListView.DataContext = ds.Tables[0].DefaultView; foreach(DataRow r in ds.Tables[0].Rows) { string t = r["title"].ToString(); string d = r["date"].ToString(); string i = r["image"].ToString(); //NotesItem是自定义的控件,noteList是listBox NotesItem noteTemp = new NotesItem(this, t, d, i); notesList.Items.Add(noteTemp); } } catch (Exception) { throw; } finally { if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } } }
其中myConnectionString是登录的口令:
private String myConnectionString = "Server=localhost;Database=mydatabse;Uid=root;Pwd=password;";
0 0
- C#探秘系列(九)WPF连接Mysql数据库
- C#探秘系列(八)WPF数据绑定
- C#探秘系列(一)
- C#探秘系列(二)
- C#探秘系列(三)
- C#探秘系列(四)
- C#探秘系列(五)
- C#探秘系列(六)
- C#探秘系列(十)WPF:打开文件选择器选择文件并保存
- WPF 4.5探秘系列文章
- C#连接mysql数据库
- C#连接mysql 数据库
- C# 连接MySQL数据库
- C#连接mysql数据库
- C#连接MySql数据库
- C#连接MySql数据库
- C#连接mysql数据库
- C#连接Mysql数据库
- Linux中的进程间通信
- Android java.lang.IllegalArgumentException: pointerIndex out of range
- UML类图几种关系的总结
- HTML文本域(文本框)禁止修改写入数据方法
- python初识
- C#探秘系列(九)WPF连接Mysql数据库
- 第十一周 项目一 存储班长信息的学生类(2)
- HDU 1166敌兵布阵-线段树入门
- OC的多继承实现
- profiler Reserved Total
- 字典树(trie)
- C和指针
- DICOM:基于JMeter+dcm4che2测试PACS服务器性能的解决方案(前篇)
- 鲁棒性语音识别系统设计与实现