/// <summary> /// 连接Redis服务器 /// </summary> /// <param name="conndetail">服务器地址信息对象</param> /// <returns></returns> public IDatabase connectionRedis(Conndetail conndetail) { ConnectionMultiplexer redisClient = ConnectionMultiplexer.Connect(conndetail.ServerHost); IDatabase db = redisClient.GetDatabase(); return(db); }
/// <summary> /// 数据库连接初始化 /// </summary> /// <returns>返回连接初始化信息对象</returns> Conndetail InitConnet() { Conndetail conndetail = new Conndetail(); conndetail.ServerHost = "10.12.2.61"; //conndetail.ServerHost = "127.0.0.1"; conndetail.DBName = "TestDemo"; conndetail.DbUser = "******"; conndetail.DbPassword = "******"; return(conndetail); }
/// <summary> /// 连接SQL数据库的方法 /// </summary> /// <param name="conndetail">传入连接信息:用户名,密码,数据库</param> public SqlConnection ConneSql(Conndetail conndetail) { //拼接连接字符串 string connstr = String.Format($"Server={conndetail.ServerHost}; Database={conndetail.DBName}; uid={conndetail.DbUser}; pwd={conndetail.DbPassword};"); SqlConnection connection = new SqlConnection(connstr); try { if (connection.State == ConnectionState.Closed) { connection.Open(); Console.WriteLine("数据库连接正常"); } //Console.WriteLine(connection.State); } catch (Exception e) { Console.WriteLine(e.Message); } return(connection); }