在WinForm的cs文件中使用app.config里的connection string

1. 在cs文件顶部添加 using System.Management;

2. 在项目中添加引用(add reference->system.configuration)

image

3.在app.config中添加以下配置字符串(黑体部分)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="IU_GARAGE_EXPORT.Properties.Settings.IU_GarageConnectionString"
      connectionString="Data Source=192.168.3.33;Initial Catalog=IU_Garage;Persist Security Info=True;User ID=IU_Garage;Password=IU_Garage"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

 

4.在cs文件中按以下格式调用即可

string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["IU_GARAGE_EXPORT.Properties.Settings.IU_GarageConnectionString"].ConnectionString;

你可能感兴趣的:(Connection)