App.config应用(WindowsForm程序)

1、添加新项——》应用程序配置文件(使用默认名称App.config,不要修改)
2、添加配置文件内容
 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
   <connectionStrings>
     <add name="VideoMonitorEntities" connectionString="Data Source=202.196.188.135;Initial Catalog=VideoMonitor;User ID=jp;Password=123456@mjp" />
   </connectionStrings>
 </configuration>
3、调用配置文件
 private SqlConnection con;  //创建连接对象     
        #region   打开数据库连接
        /// <summary>
        /// 打开数据库连接.
        /// </summary>
        private void Open()
        {
            try
            {
                // 打开数据库连接
                if (con == null)
                {
                    con = new SqlConnection(ConfigurationManager.ConnectionStrings["VideoMonitorEntities"].ConnectionString.ToString());
                }
                if (con.State == System.Data.ConnectionState.Closed)
                    con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("数据库打开失败!","提示");
            }
        }
        #endregion

你可能感兴趣的:(windows)