C# 通过配置文件获取数据库连接

时间:2014-04-17 01:58:25
【文件属性】:
文件名称:C# 通过配置文件获取数据库连接
文件大小:138KB
文件格式:RAR
更新时间:2014-04-17 01:58:25
获取数据库连接 C# 读取保存App.config配置文件的完整源码参考(转) http://smartsoft.5d6d.com/thread-6550-1-1.html C# 读取保存App.config配置文件的完整源码参考 最近出差在北京做一个小项目,项目里需要读取配置文件的小功能,觉得挺有参考意义的就把代码发上来给大家参考一下。我们选择了直接用微软的读取配置文件的方法。 这个是程序的运行设计效果,就是把这些参数可以进行灵活设置,灵活保存设置状态。 程序编译后自动会产生相应的配置文件,是跟项目的名称一样的配置文件。 读取配置文件及保存配置的具体代码参考如下,希望能给你节省一些时间,直接复制粘贴这个代码就可以用了: //------------------------------------------------------------ // All Rights Reserved , Copyright (C) 2010 , CDPF , Ltd. //------------------------------------------------------------ using System; using System.Configuration; using System.Windows.Forms; using Utilities; namespace DirectSeeding { /// /// FrmConfig /// 读取配置文件 /// /// 修改纪录 /// /// 2011.01.14 版本: 1.0 JiRiGaLa 完善程序的注释等、从新整理代码。 /// /// 版本:1.0 /// /// /// JiRiGaLa /// 2011.01.14 /// /// public partial class FrmConfig : Form { public FrmConfig() { InitializeComponent(); } /// /// 读取配置文件 /// private void GetConfig() { this.txtWriteFileName.Text = ConfigurationManager.AppSettings["WriteFileName"]; this.txtWritePath.Text = ConfigurationManager.AppSettings["WritePath"].Replace("|", Environment.NewLine); this.txtPostMessageURL.Text = ConfigurationManager.AppSettings["PostMessageURL"]; this.txtLeasedLineURL.Text = ConfigurationManager.AppSettings["LeasedLineURL"]; } private void FrmDirectSeeding_Load(object sender, EventArgs e) { this.GetConfig(); } /// /// 保存配置文件 /// private void SaveConfig() { // 写入参数设置 Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); configuration.AppSettings.Settings["WriteFileName"].Value = this.txtWriteFileName.Text; configuration.AppSettings.Settings["WritePath"].Value = this.txtWritePath.Text.Trim().Replace(Environment.NewLine, "|"); configuration.AppSettings.Settings["PostMessageURL"].Value = this.txtPostMessageURL.Text; configuration.AppSettings.Settings["LeasedLineURL"].Value = this.txtLeasedLineURL.Text; configuration.Save(); // 重新读取参数 ConfigurationManager.RefreshSection("appSettings"); WriteFile.WriteFileName = ConfigurationManager.AppSettings["WriteFileName"]; WriteFile.WritePath = ConfigurationManager.AppSettings["WritePath"].Split('|'); PostMessage.PostMessageURL = ConfigurationManager.AppSettings["PostMessageURL"]; // PostMessage.LeasedLineURL = ConfigurationManager.AppSettings["LeasedLineURL"]; } private void btnSavaConfig_Click(object sender, EventArgs e) { // 保存设置 SaveConfig(); } } }
【文件预览】:
config
----config()
--------bin()
--------Form1.Designer.cs(4KB)
--------新建文本文档.txt(0B)
--------Program.cs(465B)
--------新建 Microsoft Office Word 97-2003 文档.doc(134KB)
--------obj()
--------Form1.cs(5KB)
--------Form1.resx(6KB)
--------config.csproj(3KB)
--------App.config(666B)
--------Properties()
----config.sln(907B)
----config.suo(13KB)

网友评论

  • 不错,正好需要此资料
  • 能用不错!学习了
  • 很好用,对我有帮助
  • 能用不错!学习了!
  • 不错的资料,正好需要学习,谢谢了
  • 最近一直在找配置文件的使用方法 ,很好用,谢谢
  • 方法提供的很好,后两个功能经验证,貌似无效
  • 恩嗯嗯,很实用,解决了好大的问题
  • 谢谢,有一定的参考价值!
  • 最近一直在找配置文件的使用方法 ,很好用,谢谢
  • 终于找到有用的通过配置文件连接方法了
  • 谢谢,虽然还没弄清楚,但是感觉很有用
  • 感谢哦,我用了个第三方的DLL,配置的config文件用不了,看了下,是自己的问题