C#-控制台程序App.config在这里

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当中了,更改数据库名称可以测试到

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页