对于很多小项目来说,不需要搭建专门的数据库系统(例如用SQLite搭建本地数据库),这时可以用ini配置文件实现一个最基本的数据库,实现数据库最基本的增删改查功能。

ini配置文件的用法参考我以前写的文章:http://www.cnblogs.com/xh6300/p/5895759.html

这种配置文件的结构如下:

[section1]

key1=value1

key2=value2

[section2]

key3=value3

key4=value4

……

具体配置文件如下:

如果要想让这个数据库可视化,在winform中用若干个ComboBox控件是合适的选择。简略界面如下:

要达到这一目的需要实现的功能如下:

① 需要将广东省、江苏省、北京市这3个section添加到cmb_address控件的Items中。

② 当选择不同的section时,需将该section名下所有条目的key值添加到cmb_name控件的Items中;当切换cmb_address的选项时,cmb_name的内容也会随之切换。

③ 当点击“删除”时,将删除当前section名下选定的那条“key=value”记录。

④ 当点击“修改”时,将使用tbx_value的值修改当前选中的section和key所对应的value值。

至于数据库的“增”和“查”功能,那是ini配置文件的最基本的操作,用对应的SetValue()和GetValue()方法就能轻易实现,在此不再赘述。

1、将所有section添加到一个List列表中的代码实现:

 public void GetAllSections(string iniFileName, out List<string> SectionsList)
{
SectionsList = new List<string>();
string fileText = ""; try
{
StreamReader sr = new StreamReader(iniFileName, Encoding.Default);
fileText = sr.ReadToEnd();
sr.Close();//这一句一定要加 }
catch (Exception)
{
MessageBox.Show("文件" + iniFileName + "不存在或错误!");
return;
} try
{
string[] _split = fileText.Split(new Char[] { '[' }); //由于上面是用“[”分割的,因此下面的i从1开始
for (int i = ; i < _split.Count(); i++)
{
string[] split_temp = _split[i].Split(new Char[] { ']' }); if (split_temp[].Trim() != "")
{
SectionsList.Add(split_temp[].Trim());
}
} }
catch (Exception)
{
//这里我就什么也不做,这是坠吼的!
}
}

2、获得指定section下的所有key值,并将它们添加到List列表中的代码实现:

 public void GetAllKeys(string iniFileName, out List<string> keysList, int Section_index)
{
keysList = new List<string>();
string fileText = ""; try
{
StreamReader sr = new StreamReader(iniFileName, Encoding.Default);
fileText = sr.ReadToEnd();
sr.Close();//这一句一定要加 }
catch (Exception)
{
MessageBox.Show("文件" + iniFileName + "不存在或错误!");
return;
} try
{
//先把所有的[section]去掉
string[] _split = fileText.Split(new Char[] { '[' });
string[] split_temp = _split[Section_index + ].Split(new Char[] { ']' }); string text_key_value = split_temp[];//获得section索引为Section_index下的key和value的文本 text_key_value = text_key_value.Replace("\r\n", "?");//将换行符换成?,便于后面进一步分割
string[] KeyLists_temp = text_key_value.Split(new char[] { '?' });//按‘?’分割,注意可能会有空字符串数组元素 //将分割出来的每一个非空key加入到keysList里面去
for (int i = ; i < KeyLists_temp.Count(); i++)
{
if (KeyLists_temp[i].Trim() != "")
{
string _key = KeyLists_temp[i].Split(new char[] { '=' })[];
if (_key.Trim() != "")
{
keysList.Add(_key.Trim());
}
}
} }
catch (Exception)
{
MessageBox.Show("文件" + iniFileName + "不存在或错误!");
} }

3、将List中的值添加到ComboBox控件的Item中的代码实现:

 public void AddList_ToCmb(List<string> _infoList, ComboBox _cmbBox)
{
for (int i = ; i < _infoList.Count; i++)
{
_cmbBox.Items.Add(_infoList[i]);
}
}

整个程序完整代码如下:

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles; namespace test1
{
public partial class CustomManage : Form
{
public CustomManage()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
} private void customManage_Load(object sender, EventArgs e)
{
List<string> _listSections = new List<string>();
List<string> _listKeys = new List<string>(); GetAllSections("companyInfo.ini", out _listSections);
AddList_ToCmb(_listSections, cmb_address); GetAllKeys("companyInfo.ini", out _listKeys, );
AddList_ToCmb(_listKeys, cmb_name);
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//清空原有数据
int index = cmb_address.SelectedIndex;
cmb_name.Items.Clear();
cmb_name.Text = ""; //重新获得对应section下的key值的列表,并添加到cmb_Name中
List<string> _nameList = new List<string>();
GetAllKeys("companyInfo.ini", out _nameList, index);
AddList_ToCmb(_nameList, cmb_name);
} //删除条目
private void button4_Click(object sender, EventArgs e)
{
string _fileName = "companyInfo.ini";
try
{
StreamReader sr = new StreamReader(_fileName, Encoding.Default);
string str = sr.ReadToEnd();
sr.Close(); //该删除方式有缺陷,要求“=”两侧不能有空格,否则会删除失败(以后可以考虑用正则匹配一下)
string _deleteItem = cmb_name.Text + "=" + GetValue(_fileName, cmb_address.Text, cmb_name.Text);
str = str.Replace(_deleteItem, "");
StreamWriter sw = new StreamWriter(_fileName, false, Encoding.Default); sw.Write(str);
sw.Flush();
sw.Close(); MessageBox.Show("删除成功!"); }
catch (Exception)
{
MessageBox.Show("文件" + _fileName + "不存在或错误!");
}
} //修改条目
private void button1_Click(object sender, EventArgs e)
{
string _fileName = "companyInfo.ini";
SetValue(_fileName, cmb_address.Text.Trim(), cmb_name.Text.Trim(), tbx_value.Text.Trim()); MessageBox.Show("修改成功!");
} /// <summary> 获得所有的section
///
/// </summary>
/// <param name="iniFileName"></param>
/// <param name="SectionsList"></param>
public void GetAllSections(string iniFileName, out List<string> SectionsList)
{
SectionsList = new List<string>();
string fileText = ""; try
{
StreamReader sr = new StreamReader(iniFileName, Encoding.Default);
fileText = sr.ReadToEnd();
sr.Close();//这一句一定要加 }
catch (Exception)
{
MessageBox.Show("文件" + iniFileName + "不存在或错误!");
return;
} try
{
string[] _split = fileText.Split(new Char[] { '[' }); //由于上面是用“[”分割的,因此下面的i从1开始
for (int i = ; i < _split.Count(); i++)
{
string[] split_temp = _split[i].Split(new Char[] { ']' }); if (split_temp[].Trim() != "")
{
SectionsList.Add(split_temp[].Trim());
}
} }
catch (Exception)
{
//这里我就什么也不做,这是坠吼的!
}
} /// <summary> 获得指定section下的所有key值
///
/// </summary>
/// <param name="iniFileName"></param>
/// <param name="keysList"></param>
/// <param name="Section_index"></param>
public void GetAllKeys(string iniFileName, out List<string> keysList, int Section_index)
{
keysList = new List<string>();
string fileText = ""; try
{
StreamReader sr = new StreamReader(iniFileName, Encoding.Default);
fileText = sr.ReadToEnd();
sr.Close();//这一句一定要加 }
catch (Exception)
{
MessageBox.Show("文件" + iniFileName + "不存在或错误!");
return;
} try
{
//先把所有的[section]去掉
string[] _split = fileText.Split(new Char[] { '[' });
string[] split_temp = _split[Section_index + ].Split(new Char[] { ']' }); string text_key_value = split_temp[];//获得section索引为Section_index下的key和value的文本 text_key_value = text_key_value.Replace("\r\n", "?");//将换行符换成?,便于后面进一步分割
string[] KeyLists_temp = text_key_value.Split(new char[] { '?' });//按‘?’分割,注意可能会有空字符串数组元素 //将分割出来的每一个非空key加入到keysList里面去
for (int i = ; i < KeyLists_temp.Count(); i++)
{
if (KeyLists_temp[i].Trim() != "")
{
string _key = KeyLists_temp[i].Split(new char[] { '=' })[];
if (_key.Trim() != "")
{
keysList.Add(_key.Trim());
}
}
} }
catch (Exception)
{
MessageBox.Show("文件" + iniFileName + "不存在或错误!");
}
} /// <summary> 将字符串List中的值添加到ComboBox控件的Item中。
///
/// </summary>
/// <param name="_infoList"></param>
/// <param name="_cmbBox"></param>
public void AddList_ToCmb(List<string> _infoList, ComboBox _cmbBox)
{
for (int i = ; i < _infoList.Count; i++)
{
_cmbBox.Items.Add(_infoList[i]);
}
} #region 读、写ini配置文件的方法 [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal,
StringBuilder retVal, int size, string filePath); [DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); public string GetValue(string parasFileName, string section, string key)
{
var sb = new StringBuilder();
string strPath = Environment.CurrentDirectory + "\\" + parasFileName;
int i = GetPrivateProfileString(section, key, "该文件不存在", sb, , strPath);
return sb.ToString();
} public void SetValue(string parasFileName, string section, string key, string value)
{
//获得当前路径,当前是在Debug路径下
string strPath = Environment.CurrentDirectory + "\\" + parasFileName;
WritePrivateProfileString(section, key, value, strPath);
} #endregion }
}

注:上面的完整代码为了减少代码量便于阅读,有些功能的实现写得不够完善,具体使用的时候需要自行改进。

操作ini配置文件设计一个最基本的可视化数据库系统的更多相关文章

  1. Python中操作ini配置文件

    这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...

  2. php操作ini配置文件

    有些配置化的数据放到配置文件可以方便管理,比如数据库信息,路由信息,先建立配置文件,test.ini [database_setting] host=127.0.0.1 user=root passw ...

  3. VC++/MFC操作ini配置文件详解

    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...

  4. C# 操作ini配置文件

    最近使用Winform做一个小系统,由于需要保存一些默认配置项.自然就想到了轻量级的配置文件类型ini.在此也分享和记录一下实现方式,方便以后查询和使用. 废话不多说上代码: 实现公共函数↓ publ ...

  5. 【转】ini载入保存类,操作INI配置文件方便的很

    /****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...

  6. C#操作INI配置文件示例

    源文件地址:http://pan.baidu.com/share/link?shareid=2536126078&uk=1761850335创建如图所示的控件: 源代码: using Syst ...

  7. 【个人使用.Net类库】(1)INI配置文件操作类

    开发接口程序时,对于接口程序配置的IP地址.端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用W ...

  8. python开发_configparser_解析.ini配置文件工具_完整版_博主推荐

    # # 最近出了一趟差,是从20号去的,今天回来... # 就把最近学习的python内容给大家分享一下... # ''' 在python中,configparser模块提供了操作*.ini配置文件的 ...

  9. python 提供INI配置文件的操作 ConfigParser

    原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...

随机推荐

  1. Python中的变量和常量

    本文主要介绍Python中的变量和常量,包括变量的命名规范,使用注意事项 -------------- 完美的分割线 --------------- 1.变量 1.1.变量理解 1)什么是变量 变量即 ...

  2. spring注解-@Autowired。@Resource。@Service

    Spring的@Autowired注解.@Resource注解和@Service注解 什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: ...

  3. deno学习一 安装试用&&几个问题解决

      基本的依赖可以参考github 我的环境是centos 7 基本安装 需要golang 以及yarn安装 Protobuf 3 这是官方的方式,实际可以变通下 cd ~ wget https:// ...

  4. FastAdmin 学习线路 (2018-06-09 更新)

    FastAdmin 学习线路 以下为常规线路,非常规可跳过. FastAdmin 学习线路 基础 HTML CSS DIV Javascript 基础 jQuery php 基础 对象 命名空间 进阶 ...

  5. hadoop之 hadoop 机架感知

    1.背景 Hadoop在设计时考虑到数据的安全与高效,数据文件默认在HDFS上存放三份,存储策略为本地一份,同机架内其它某一节点上一份,不同机架的某一节点上一份.这样如果本地数据损坏,节点可以从同一机 ...

  6. Spring AOP 实现读写分离

    原文地址:Spring AOP 实现读写分离 博客地址:http://www.extlight.com 一.前言 上一篇<MySQL 实现主从复制> 文章中介绍了 MySQL 主从复制的搭 ...

  7. 微信小程序设置底部导航栏目方法

    微信小程序底部想要有一个漂亮的导航栏目,不知道怎么制作,于是百度找到了本篇文章,分享给大家. 好了 小程序的头部标题 设置好了,我们来说说底部导航栏是如何实现的. 我们先来看个效果图 这里,我们添加了 ...

  8. PHP向客户端广播信息

    在网络中数据传播分为:Unicast(单播) , Multicast(多播或者组播) 和 Broadcast(广播).广播和多播仅应用于UDP,它们对需将报文同时传往多个接收者的应用来说十分重要.而 ...

  9. 20181123_SQL Server 2008_找出以逗号分隔的字符串中最大的数字

    --select [dbo].[Fun_GetMaxNum]('棉 20%,麻 190%,涤纶60%') CREATE FUNCTION [dbo].[Fun_GetMaxNum] ( @StrAll ...

  10. 寒武纪-1005 Travel(树形DP)

    一.题目链接 http://aiiage.hustoj.com/problem.php?id=1005 二.题面 PDF:http://aiiage.hustoj.com/upload/file/20 ...