C#- App.config 的使用

2019/12/09 15:57
阅读数 106


using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.Common;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = null;
            try
            {
                conn = (SqlConnection)GetConnection();
                conn.Open();
                Console.WriteLine("Open "+conn.Database);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                
                conn.Close();
            }
            Console.Read();

        }

        private static DbConnection GetConnection()
        {
            ConnectionStringSettings conSetting = ConfigurationManager.ConnectionStrings["dbName"];
            DbProviderFactory factory = DbProviderFactories.GetFactory(conSetting.ProviderName);
            DbConnection con = factory.CreateConnection();
            con.ConnectionString = conSetting.ConnectionString;
            return con;

        }
    }
}

 

/***************App.config*****************/

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <connectionStrings>
    <add name="dbName" connectionString="server=.;database=D:\DATABASE\NORTHWND.MDF;uid=sa;pwd=Server2012;"  
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

/*****************************/

App.config的内容写入到了ConsoleApp2.exe.config当中了,更改数据库名称可以测试到

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
vga博主
https://blog.csdn.net/softimite_zifeng/article/details/60591488
2019/12/11 11:45
回复
举报
vga博主
public bool DelleteValue(string keyName)
{
try
{
config.AppSettings.Settings.Remove(keyName);
config.Save(ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
}
}
2019/12/09 16:34
回复
举报
vga博主
public bool SetValue(string keyName, string content)
{
try
{
config.AppSettings.Settings[keyName].Value = content;
config.Save(ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
public bool AddValue(string keyName, string value)
{
try
{
config.AppSettings.Settings.Add(keyName, value);
config.Save(ConfigurationSaveMode.Modified);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
2019/12/09 16:34
回复
举报
vga博主
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace Cfg
{
public class AppConfig
{
private System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
public bool GetValue(string keyName,out string value)
{
try
{
value = ConfigurationManager.AppSettings[keyName].ToString();
return true;
}
catch
{
value = null;
return false;
}

}
2019/12/09 16:33
回复
举报
vga博主
https://blog.csdn.net/tanhuiong/article/details/88584004
2019/12/09 16:30
回复
举报
更多评论
打赏
5 评论
0 收藏
0
分享
返回顶部
顶部