/// <summary> /// 建立连接 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="DbSetting">连接字符串配置节参数(注意! 是配置节参数,不是连接字符串)</param> /// <returns></returns> public static IDbConnection CreateConnection <T>(string DbSetting) where T : IDbConnection, new() { IDbConnection connection = new T(); //connection.ConnectionString = "server=192.168.4.223;database=CA;User ID=cyyz;Password=cyyz7201;Connect Timeout=120;"; //ConnectionConfig.ConnectionString;拿到 app里 connection.ConnectionString = (AppConfiguration.Get(CY_SystemConsts.ServiceProjectName).GetConnectionString(DbSetting)); connection.Open(); return(connection); }
/// <summary> /// 直接获取配置值,如果有多层则用冒号分割 eg.: /// var x = GetValue("ConnectionStrings:Default") /// </summary> /// <param name="sectionName"></param> /// <returns></returns> public static string GetValue(string sectionName, string projectName = CY_SystemConsts.ServiceProjectName) { IConfigurationRoot root = AppConfiguration.Get(projectName); return(root.GetSection(sectionName).Value); }