不多說,直接上代碼 web.config如下 其中 SymmetricMethod.Decrypto 解密演算法,至於加解密演算法,大家自行百度吧 ...
不多說,直接上代碼
public partial class Model1 : DbContext { private static string connStr = ""; public Model1() : base("Data Source=.;Initial Catalog=GameDb;user id=sa;password=123456")//預設隨便填一個標準連接字元串,如果不是標準的你的後面設置連接字元串會出錯,具體原因大家有興趣的去深究吧 {if (connStr == "") { connStr = ConfigurationManager.ConnectionStrings["Model1"].ConnectionString; connStr = SymmetricMethod.Decrypto(connStr); } base.Database.Connection.ConnectionString = connStr; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } }
web.config如下
<add name="Model1" connectionString="zgtsMV7VlXojlkp2VhEQvCsN6Pq/RsadZKfvOgqRWKPtBL8/x+2yBuSiWjfkMueEcAbFVTrI094JVLn28NnyiHlAqUl8q8g4T/ybEi4gPQPVEdUfg31xF8NLgWDCAGR9Tw+7/VTm/WRpBHxrpCwUwggB35QNgqkXo+1SQ6OmpqhrNdSuPrlzh1UoOHP/p55WrVL2TNOZ5n4v6CUmODdTVw==" providerName="System.Data.SqlClient" /> </connectionStrings>
其中 SymmetricMethod.Decrypto 解密演算法,至於加解密演算法,大家自行百度吧