美文网首页
在配置文件App.config中添加Sql Server连接字符

在配置文件App.config中添加Sql Server连接字符

作者: Sombod_Y | 来源:发表于2016-11-24 21:23 被阅读0次

1.微软在App.config文件中提供了connectionStrings字段,可在其中添加连接字符串,供程序中任何地方调用。

App.config文件.png

2.为项目添加引用:

添加引用.png

3.测试代码如下:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01ConfigureDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 获取配置文件中的AppSetting中的数据
            Console.WriteLine(ConfigurationManager.ConnectionStrings["SqlConn"]);
        }
    }
}

4.运行结果如下:

运行结果.png

相关文章

网友评论

      本文标题:在配置文件App.config中添加Sql Server连接字符

      本文链接:https://www.haomeiwen.com/subject/szivpttx.html