用ini文件对基于桌面型数据库(Access)程序的数据库定位方式
来源:互联网 发布:软件测试分为哪些 编辑:程序博客网 时间:2024/05/22 05:11
private void timGetDB_Tick(object sender, EventArgs e)
{//timer
timGetDB.Enabled = false;
string dbPath = GetDBPath();
while (true)
{
if (dbPath == "0")
{
//说明数据库和ini文件都不存在
ofdDB.InitialDirectory = Application.StartupPath;
DialogResult opdr=ofdDB.ShowDialog();
if (opdr == DialogResult.OK)
{
if ((dbPath = ofdDB.FileName) != null)
if (FileExist(dbPath))
{
//IniFile ini = new IniFile(dbPath);
INIFile ini = new INIFile(Application.StartupPath + "//cowa.ini");
ini.Write("DB", "DBName", dbPath);
break;
}
}
else if (opdr == DialogResult.Cancel)
{
this.Close();
return ;
}
}
//数据库文件就在程序文件夹里
if (dbPath.Substring(dbPath.Length - 3, 3) == "mdb")
break;
if (dbPath.Substring(dbPath.Length - 3, 3) == "ini")
{
INIFile ini = new INIFile(dbPath);
dbPath = ini.ReadString("DB", "DBName");
if (!FileExist(dbPath))
{
//说明ini文件里存的路径是错的,ini文件将被删除
dbPath = "0";
ini.DeleteFile();
continue;
}
}
}
try
{
//到此处dbPath 为正确路径,以下可进行数据库操作
}
catch (Exception err)
{
MessageBox.Show("数据库操作失败", "错误");
}
}
/// <summary>
/// 通过程序文件夹中的INI文件获取数据库路径,
/// 先在程序文件夹中寻找数据库,再寻找INI文件
/// </summary>
/// <returns>返回数据库或INI文件路径,如果二者都不存在,则返回"0"</returns>
private string GetDBPath()
{//
string dbPath=Application.StartupPath + "//system.mdb" ;
string iniPath = Application.StartupPath + "//cowa.ini";
bool dbEx=FileExist(dbPath), iniEx=FileExist(iniPath);
if (dbEx)
return dbPath;
else if (iniEx)
return iniPath;
else if (!dbEx && !iniEx)
return "0";//no db, no ini;
return dbPath;
}
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="path">存在返回true,不存在或路径错误返回false</param>
/// <returns></returns>
private bool FileExist(string path)
{
try
{
FileInfo fi = new FileInfo(path);
if (fi.Exists)
return true;
else
return false;
}
catch (Exception err)
{
return false;
}
}
//INIFile类的内容参见另一篇文章:http://blog.csdn.net/ltolll/archive/2008/01/23/2061723.aspx
- 用ini文件对基于桌面型数据库(Access)程序的数据库定位方式
- 连接Access数据库的方式
- [数据库]桌面轻量级数据库的选择:Access、SQLite、自己编写?
- c# winform 桌面程序连接Access数据库,操作Access数据库,分页,ADO.NET
- 用access窗体和access数据库开发小型的桌面应用程序
- Access数据库的.ldb文件
- 用ini文件实现数据库的增、删、改、查
- Delphi用ini文件连接数据库
- 【转】DataGridView对Access数据库的操作
- python对access数据库的操作
- Access数据库对并发的限制
- C#对Access数据库的操作
- c#对Access数据库的操作
- c#对Access数据库的操作
- C#对Access数据库的操作
- access数据库的两种驱动方式
- 数据库的备份与还原+INI文件数据库参数配置
- ACCESS数据库替换程序
- SSH(struts+spring+hibernate)迅速开发--第四章 改造DAO(2)
- script 获取客户端页面信息
- 2008年最热门的七大IT软件技能
- asp.net发送邮件
- 邮箱与论坛的数据恢复
- 用ini文件对基于桌面型数据库(Access)程序的数据库定位方式
- 句柄概念
- tomcat error page
- C#中自定义操作INI文件的类
- HashCalc 2.02
- 利用FMS做在线视频录制.
- 动态连接库的创建及调用
- API程序框架
- Bibliography and Online Resources