winform程序中读取config文件获得连接字符串

在App.Config文件加下两个节点中任意一个<appSettings>……</appSettings>或
< connectionStrings >…… </ connectionStrings >

App.config
<? xml version ="1.0" encoding ="utf-8" ?>
< configuration >
     < appSettings >
         < add key ="constri" value ="Data Source=.;Initial Catalog=travel;Integrated Security=True" />
     </ appSettings >
    
     < connectionStrings >        
         < add name ="constri" connectionString ="Data Source=.;Initial Catalog=travel;Integrated Security=True" />
         </ connectionStrings >
</ configuration >
然后在.cs文件中的读取连接字符串的方法为:
using System.Configuration;

1     string constr = ConfigurationSettings.AppSettings[ "constri"].ToString ();
2                 //string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constri"].ConnectionString;
       
首先引入命名空间,第1行是如果config文件加入的是<appSettings>节点的读取方式,第二行是另一种字节的读取方法。

本文出自 “叶子文文” 博客,转载请与作者联系!

你可能感兴趣的:(读取,连接字符串,WinForms,教学思路,config文件)