Exemplo n.º 1
0
        /// <summary>
        /// 链接数据库并查询
        /// </summary>
        /// <param name="dbLink"></param>
        /// <param name="dbType"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public JsonResult LinkServer(string dbLink, DbType dbType, string tableName)
        {
            ResultInfo     result = new ResultInfo();
            SqlSugarClient db     = GetSugarClient(dbLink, dbType);

            try
            {
                List <DbTableInfo> list = db.DbMaintenance.GetTableInfoList(false);
                if (!string.IsNullOrEmpty(tableName))  //模糊查询
                {
                    List <DbTableInfo> tableList = list.Where <DbTableInfo>(t => t.Name.Contains(tableName) || t.Description.Contains(tableName)).ToList();
                    result.info = tableList;
                }
                else
                {
                    result.info = list;
                }

                //获取客户端的Cookie对象
                HttpCookie c*k = Request.Cookies["MyData"];

                if (c*k != null)
                {
                    //修改Cookie的两种方法
                    c*k.Values["dbLink"] = Base64Helper.Encode(dbLink);
                    c*k.Values["dbType"] = dbType.ToString();
                    Response.AppendCookie(c*k);
                }
                else
                {
                    HttpCookie cookie = new HttpCookie("MyData");    //初使化并设置Cookie的名称
                    DateTime   dt     = DateTime.Now;
                    TimeSpan   ts     = new TimeSpan(2, 0, 0, 0, 0); //过期时间为1分钟
                    cookie.Expires = dt.Add(ts);                     //设置过期时间
                    cookie.Values.Add("dbLink", Base64Helper.Encode(dbLink));
                    cookie.Values.Add("dbType", dbType.ToString());
                    Response.AppendCookie(cookie);
                }
                db.Dispose();
                db.Close();
                result.res = true;
                result.msg = "链接成功!";
            }
            catch (Exception ex)
            {
                db.Dispose();
                db.Close();
                result.msg = ex.Message;
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 2
0
 void IDisposable.Dispose()
 {
     if (db != null)
     {
         db.Dispose();
     }
 }
Exemplo n.º 3
0
        public void Init()
        {
            var mstStr  = "server=.;uid=sa;pwd=sasa;database=SqlSugarTest";
            var conList = new List <string>()
            {
                "server=localhost;uid=sa;pwd=sasa;database=SqlSugarTest"
            };

            SqlSugarClient db = new SqlSugarClient(mstStr, conList);

            var l1 = db.Queryable <Student>().ToList();      //走从
            var d1 = db.Queryable <Student>().ToDataTable(); //走从
            var t3 = db.Queryable <Student>().ToList();      //走从

            db.Insert <Student>(new Student()
            {
                name = "主"
            });

            db.BeginTran();
            var l2 = db.Queryable <Student>().ToList();//走主

            db.CommitTran();

            var l3 = db.Queryable <Student>().ToList();//走从

            db.Dispose();
        }
Exemplo n.º 4
0
 //关闭数据库链接
 public void Dispose()
 {
     if (Db != null)
     {
         Db.Dispose();
     }
 }
Exemplo n.º 5
0
        protected void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // 手动释放托管资源
                }
                // 手动释放非托管资源

                if (_client != null)
                {
                    try
                    {
                        _client.Dispose();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        _client = null;
                    }
                }

                _disposed = true;
            }
        }
Exemplo n.º 6
0
 public void Dispose()
 {
     if (client != null)
     {
         client.Dispose();
     }
 }
Exemplo n.º 7
0
        public static void Init()
        {
            if (!File.Exists("data.db"))
            {
                SqlSugarClient conn = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString      = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\data.db;",
                    DbType                = SqlSugar.DbType.Sqlite,
                    InitKeyType           = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                });
                conn.CodeFirst.InitTables <CustomListModel>();
                conn.CodeFirst.InitTables <CustomTitleModel>();
                conn.CodeFirst.InitTables <DefaultListModel>();
                conn.CodeFirst.InitTables <MusicInfoModel>();
                conn.CodeFirst.InitTables <LocalListModel>();
                conn.CodeFirst.InitTables <SearchHistoryModel>();
                conn.CodeFirst.InitTables <PlatformListModel>();
                conn.CodeFirst.InitTables <PlatformInfoModel>();
                conn.Dispose();
            }
            IniConfig ini = new IniConfig("Config.ini");

            ini.Load();
            bool result = ini.Object["System"].TryGetValue("DownloadPath", out IValue v);

            if (!result || v?.ToString() == string.Empty)
            {
                DownloadManager.MusicDownloadPath = AppDomain.CurrentDomain.BaseDirectory + "Download\\";
            }
            else
            {
                DownloadManager.MusicDownloadPath = v.ObjToString();
            }
        }
Exemplo n.º 8
0
 public void Dispose()
 {
     if (db != null)
     {
         db.Dispose();
     }
 }
Exemplo n.º 9
0
 public void Dispose()
 {
     if (dbcontext != null)
     {
         dbcontext.Dispose();
     }
 }
Exemplo n.º 10
0
 protected void btnCreateClassCode_Click(object sender, EventArgs e)
 {
     string connection = ConfigSugar.GetConfigString("connstring"); //这里可以动态根据cookies或session实现多库切换
     var db = new SqlSugarClient(connection);
     txtResult.Value = db.ClassGenerating.SqlToClass(db, txtSql.Text, txtClassName.Text);
     db.Dispose();
 }
Exemplo n.º 11
0
 public void Dispose()
 {
     if (_db != null)
     {
         _db.Dispose();
     }
 }
Exemplo n.º 12
0
        protected void btnCreateClassCode_Click(object sender, EventArgs e)
        {
            string connection = ConfigSugar.GetConfigString("connstring"); //这里可以动态根据cookies或session实现多库切换
            var    db         = new SqlSugarClient(connection);

            txtResult.Value = db.ClassGenerating.SqlToClass(db, txtSql.Text, txtClassName.Text);
            db.Dispose();
        }
 public void Dispose()
 {
     if (_db != null)
     {
         _db.Dispose();
         // LogNHelper.Exception("释放了资源");
     }
 }
Exemplo n.º 14
0
        public void BindDgv()
        {
            SqlSugarClient db = null;

            try
            {
                db = SqlLiteHelper.GetInstance();
                if (!File.Exists(Device.DB_FilePath))
                {
                    MessageBox.Show("文件下载中,请稍后再试!");
                    return;
                }
                var sourceList = db.Queryable <SYS_USERROOM, SYS_MODELS>((u, m) => new object[] {
                    JoinType.Left, u.GUID == m.GUID
                })
                                 .Select((u, m) => new
                {
                    u.GUID,
                    u.NAME,
                    u.CREATETIME,
                    m.MODELID,
                    m.FEATURE,
                    u.STARTTIME,
                    u.ENDTIME,
                    u.AREACODE,
                    u.AREANAME,
                    u.USERTYPE,
                });
                if (!string.IsNullOrWhiteSpace(txtName.Text))
                {
                    sourceList = sourceList.Where(u => u.NAME.Contains(txtName.Text));
                }
                TotalCount = sourceList.Count();
                if (PageIndex <= 1)
                {
                    PageIndex = 1;
                }
                var maxPageIndex = Math.Ceiling((decimal)TotalCount / (decimal)PageSize);
                if (PageIndex >= maxPageIndex)
                {
                    PageIndex = maxPageIndex.ToInt32();
                }
                var skip = (PageIndex - 1) * PageSize;
                sourceList = sourceList.Take(PageSize).Skip(skip)
                             .OrderBy(u => u.CREATETIME, OrderByType.Desc);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource          = sourceList.ToList();
                lblTotalCount.Text = $"第{PageIndex}页,每页{PageSize}条,总数:" + TotalCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件下载中,请稍后再试!");
            }
            db.Close();
            db.Dispose();
            System.Data.SQLite.SQLiteConnection.ClearAllPools();
        }
Exemplo n.º 15
0
 protected void btnCreate_Click(object sender, EventArgs e)
 {
     string connection = ConfigSugar.GetConfigString("connstring"); //这里可以动态根据cookies或session实现多库切换
     var db = new SqlSugarClient(connection); ;
     db.ClassGenerating.CreateClassFiles(db, txtPath.Text, txtNS.Text);
     db.Dispose();
     //还有其它方法我这边只是最简单的
     //db.ClassGenerating.CreateClassFilesByTableNames
     //db.ClassGenerating....
 }
Exemplo n.º 16
0
 /// <summary>
 /// 解决MSBUILD : warning CA1001: Microsoft.Design 报错提示
 /// fork from https://blog.csdn.net/unopenmycode/article/details/38311797?hp.com
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         /// 释放前提交更改
         Db.Ado.CommitTran();
         Db.Ado.Close();
         /// 释放各个对象
         Db.Dispose();
     }
 }
Exemplo n.º 17
0
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            string connection = ConfigSugar.GetConfigString("connstring"); //这里可以动态根据cookies或session实现多库切换
            var    db         = new SqlSugarClient(connection);;

            db.ClassGenerating.CreateClassFiles(db, txtPath.Text, txtNS.Text);
            db.Dispose();
            //还有其它方法我这边只是最简单的
            //db.ClassGenerating.CreateClassFilesByTableNames
            //db.ClassGenerating....
        }
Exemplo n.º 18
0
        private void Dispose(bool disposing)
        {
            if (!isDisposed && disposing)
            {
                if (_sqlSugarClient != null)
                {
                    _sqlSugarClient.Dispose();
                }
            }

            isDisposed = true;
        }
Exemplo n.º 19
0
        private bool disposedValue = false;      //要检测冗余调用

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    db.Dispose();
                    db = null;
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }
Exemplo n.º 20
0
        public void Init()
        {
            SqlSugarClient db = new SqlSugarClient("server=.;uid=sa;pwd=sasa;database=SqlSugarTest", "server=localhost;uid=sa;pwd=sasa;database=SqlSugarTest");

            var l1= db.Queryable<Student>().ToList();//走从
            var d1= db.Queryable<Student>().ToDataTable();//走从
            var t3 = db.Queryable<Student>().ToList();//走从

            db.Insert<Student>(new Student() { name="主" });

            db.BeginTran();
            var l2 = db.Queryable<Student>().ToList();//走主
            db.CommitTran();

            var l3 = db.Queryable<Student>().ToList();//走从
            db.Dispose();
        }
Exemplo n.º 21
0
 private static void Dispose()
 {
     try
     {
         if (SugarContext != null)
         {
             SugarContext.Dispose();
         }
     }
     catch (Exception e)
     {
         Msg.ShowException(e, "数据库连接失败");
     }
     finally
     {
         SugarContext = null;
     }
 }
Exemplo n.º 22
0
 public static void Dispose()
 {
     try
     {
         if (SugarContext != null)
         {
             SugarContext.Dispose();
         }
     }
     catch (Exception e)
     {
         LogNHelper.Exception(e);
     }
     finally
     {
         SugarContext = null;
     }
 }
Exemplo n.º 23
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region SqlSugar
            var defaultDbPath = Path.Combine(PathExtenstions.GetApplicationCurrentPath(), $"{Configuration["EasyLogger:DbName"]}.db");

            services.AddSingleton <ISqlSugarProvider>(new SqlSugarProvider(new SqlSugarSetting()
            {
                Name             = SqlSugarDbStorageConsts.DefaultProviderName,
                ConnectionString = @$ "Data Source={defaultDbPath}",
                DatabaseType     = DbType.Sqlite,
                LogExecuting     = (sql, pars) =>
                {
                    Console.WriteLine($"sql:{sql}");
                }
            }));

            services.AddTransient(typeof(ISqlSugarRepository <,>), typeof(SqlSugarRepository <,>));
            services.AddTransient(typeof(IDbRepository <,>), typeof(SqlSugarRepository <,>));
            services.AddSingleton <ISqlSugarProviderStorage, DefaultSqlSugarProviderStorage>();
            #endregion

            #region 默认创建基础数据库 和  时间数据库

            if (!File.Exists(defaultDbPath))
            {
                var db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString      = @$ "Data Source={defaultDbPath}",
                    DbType                = DbType.Sqlite,
                    IsAutoCloseConnection = true,                 // 自动释放数据务,如果存在事务,在事务结束后释放
                    InitKeyType           = InitKeyType.Attribute // 从实体特性中读取主键自增列信息
                });

                db.CodeFirst.BackupTable().InitTables <EasyLoggerProject>();

                db.Dispose();
            }
            #endregion


            services.AddControllers();
        }
Exemplo n.º 24
0
        public void Init()
        {
            SqlSugarClient db = new SqlSugarClient("server=.;uid=sa;pwd=sasa;database=SqlSugarTest", "server=localhost;uid=sa;pwd=sasa;database=SqlSugarTest");

            var l1 = db.Queryable <Student>().ToList();      //走从
            var d1 = db.Queryable <Student>().ToDataTable(); //走从
            var t3 = db.Queryable <Student>().ToList();      //走从

            db.Insert <Student>(new Student()
            {
                name = "主"
            });

            db.BeginTran();
            var l2 = db.Queryable <Student>().ToList();//走主

            db.CommitTran();

            var l3 = db.Queryable <Student>().ToList();//走从

            db.Dispose();
        }
        public void DbTableCreate(string path, bool isBaseDb)
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString      = $@"Data Source={path}",
                DbType                = DbType.Sqlite,
                IsAutoCloseConnection = true,                 // 自动释放数据务,如果存在事务,在事务结束后释放
                InitKeyType           = InitKeyType.Attribute // 从实体特性中读取主键自增列信息
            });

            // 生成数据库
            // db.Ado.ExecuteCommand($"create dataabse {dbName}");

            if (isBaseDb)
            {
                db.CodeFirst.BackupTable().InitTables <EasyLoggerProject>();
            }
            else
            {
                CreateLoggerTable(db);
            }

            db.Dispose();
        }
Exemplo n.º 26
0
 public void Cleanup()
 {
     _sqlSugarClient.Dispose();
 }
Exemplo n.º 27
0
 public void Dispose()
 {
     Db.Dispose();
 }
Exemplo n.º 28
0
 public void Cleanup()
 {
     DB.Dispose();
 }
Exemplo n.º 29
0
 public void Dispose()
 {
     _db.Dispose();
 }