目录
一、直接使用连接字符串
//使用直接的数据库连接字符串【测试成功】
private string connString = @"Data Source=数据库地址;Initial Catalog=……;Persist Security Info=True;User ID=登录名;Password=密码";
缺点:连接字符串是固定的,无法进行更改。
二、使用App.config进行配置
(一)明文配置
(1)配置App.config
<!--*****************SqlServer连接设置*****************-->
<add name="DbSql" connectionString="Data Source=数据库地址;Initial Catalog=……;Persist Security Info=True;User ID=登录名;Password=密码" providerName="System.Data.SqlClient"/>
(2)SQLHelper类中配置
//使用明文字符串【测试成功】
private readonly static string connString = ConfigurationManager.ConnectionStrings["DbSql"].ConnectionString;
优点:可以通过 App.config配置文件对数据库连接进行更改
缺点:连接字符串安全性差。
(二)密文配置
(1)配置App.config
<!--*****************SqlServer连接设置*****************-->
<add name="DbSql_AES" connectionString="pwKMbymGvKRjMwrlsXWr5n7pRXbzM8jG/DNk5DtHe/I3C0YJfdsBeG2PCag7yWSAUZmS1o6kATZEPdSTX9aGPORPHM15q9sz+lX2JV8tZ4/6+S8gLhWFnaKNaZcJwCJGEt2Z4pmD43WOcXksia+YXv6RD86XE6ooxDAHlnZWQq+o3ER04TysrEV/rQKQXktt" providerName="System.Data.SqlClient"/>
(2)SQLHelper类中配置
//使用加密解密连接字符串【测试成功】
static byte[] dbsql = DAL.AESData.AESDecrypt(Convert.FromBase64String(ConfigurationManager.ConnectionStrings["DbSql_AES"].ConnectionString), "秘钥");
private readonly static string connString = Encoding.UTF8.GetString(dbsql); //解密后的明文
优点:连接字符串具有安全性。注意:static的使用.