Beispiel #1
0
        /// <summary>
        /// 连接数据库,并返回当前连接下所有数据库名字
        /// </summary>
        /// <returns></returns>
        public ApiResult <List <string> > InitConnection(BuilderConnection param)
        {
            var result = JResult <List <string> > .Success();

            try
            {
                var db = new SugarInstance().GetInstance(param);
                result.Data = db.DbMaintenance.GetTableInfoList().Select(m => m.Name).ToList();
                return(result);
            }
            catch (Exception ex)
            {
                return(JResult <List <string> > .Error(ex.Message));
            }
        }
        public SqlSugarClient GetInstance(BuilderConnection model)
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString      = "server=" + model.Ip + ",port=" + model.Port + ";database=" + model.DbName + ";uid=" + model.Name + ";pwd=" + model.PassWord + ";charset='utf8';SslMode=None",
                DbType                = DbType.MySql,
                IsAutoCloseConnection = true,
                InitKeyType           = InitKeyType.Attribute
            });

            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql);
            };
            return(db);
        }