当前位置: 首页>>代码示例>>C#>>正文


C# DbManager.Dispose方法代码示例

本文整理汇总了C#中DbManager.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# DbManager.Dispose方法的具体用法?C# DbManager.Dispose怎么用?C# DbManager.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DbManager的用法示例。


在下文中一共展示了DbManager.Dispose方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Init


//.........这里部分代码省略.........
                }
            }

            Previews.Sort();
            Previews.Reverse();

            dbstr = String.Format("server={0};database={1};userid={2};password={3};",
                                  DatabaseInfo.Host, DatabaseInfo.Name,
                                  DatabaseInfo.Username, DatabaseInfo.Password);

            Log.Debug(">>> Connecting to database...");

            try {
                dbcon = new MySqlConnection(dbstr);
                dbcon.Open();
            #if DEBUG
                DB.Query("TRUNCATE `file`");
                DB.Query("TRUNCATE `preview`");
            DB.Query("TRUNCATE `keyword`");
            DB.Query("TRUNCATE `keyword_rel`");
            #endif
                Log.Debug("OK!\n");
            }
            catch (Exception e) {
                Log.Debug("FAILED! ");
                Log.Werror("{0}\n", e.Message);
                return false;
            }

            var db = new DbManager();

            // Collect base paths from database
            if (db.QueryReader("SELECT * FROM base_path")) {
                while (db.DataReader.Read()) {
                    string p = db.DataReader.GetString("path");
                    long id = db.DataReader.GetInt64("id");

                    if (!(basePaths.Contains(p) && Tools.DirectoryExists(p))) {
                        Log.Notice("Base path \"{0}\" is removed from config or file " +
                                   "system! Removing from database...\n", p);
                        // NOTE! This uses the global DB object. It's not supposed to
                        // use the local one since it would conflict with the DataReader.
                        DB.Query("DELETE FROM base_path WHERE path = @path",
                                 DB.Param("path", p));
                    }
                    else
                        BasePaths.Add(new BasePath(id, p));
                }
            }

            // Sync paths from config file with path from database
            foreach (string path in basePaths) {
                var t = BasePaths.Find(delegate(BasePath tp) {
                    return tp.Name == path;
                });
                if (t == null) {
                    if (!Tools.DirectoryExists(path)) {
                        Log.Warning("Path \"{0}\" in config file doesn't exits in file " +
                                    "system!\n");
                    }
                    else {
                        Log.Debug("+++ Add {0} to base paths\n", path);
                        long myid;
                        string sql = "INSERT INTO base_path(path) VALUES (@path)";
                        // NOTE! This uses the global DB object. It's not supposed to
                        // use the local one since it would conflict with the DataReader.
                        if (DB.QueryInsert(out myid, sql, DB.Param("path", path)))
                            BasePaths.Add(new BasePath(myid, path));
                        else
                            Log.Warning("Failed adding {0} to base_path!\n", path);
                    }
                }
            }

            // Setup the directory list
            if (db.QueryReader("SELECT * FROM directory")) {
                while (db.DataReader.Read()) {
                    MediaDB.Backend.Directory d =
                        MediaDB.Backend.Directory.FromSql(db.DataReader);
              if (Tools.DirectoryExists(d.FullName) && InBasePaths(d.FullName)) {
                        Directories.Add(d);
              }
              else {
                        DB.Query("DELETE FROM directory WHERE id='" + d.Id + "'");
              }
                }
            }

            // Setup the list of files
            if (db.QueryReader("SELECT fullname FROM `file`")) {
                while (db.DataReader.Read())
                    FileIndex.Add(db.DataReader.GetString("fullname"));
            }

            filesCount = FileIndex.Count;

            db.Dispose();

            return true;
        }
开发者ID:poppa,项目名称:MediaDB,代码行数:101,代码来源:Manager.cs

示例2: SaveFolder

        public object SaveFolder(Folder folder, DbManager db)
        {
            var ownsManager = false;
            if (folder == null) throw new ArgumentNullException("folder");

            folder.Title = Global.ReplaceInvalidCharsAndTruncate(folder.Title);

            folder.ModifiedOn = TenantUtil.DateTimeNow();
            folder.ModifiedBy = SecurityContext.CurrentAccount.ID;

            if (folder.CreateOn == default(DateTime)) folder.CreateOn = TenantUtil.DateTimeNow();
            if (folder.CreateBy == default(Guid)) folder.CreateBy = SecurityContext.CurrentAccount.ID;
            try
            {
                if (db == null)
                {
                    ownsManager = true;
                    db = GetDb();
                }

                var isnew = false;

                using (var tx = db.BeginTransaction(true))
                {
                    if (folder.ID != null && IsExist(folder.ID))
                    {
                        db.ExecuteNonQuery(
                            Update("files_folder")
                                .Set("title", folder.Title)
                                .Set("modified_on", TenantUtil.DateTimeToUtc(folder.ModifiedOn))
                                .Set("modified_by", folder.ModifiedBy.ToString())
                                .Where("id", folder.ID));
                    }
                    else
                    {
                        isnew = true;
                        folder.ID = db.ExecuteScalar<int>(
                            Insert("files_folder")
                                .InColumnValue("id", 0)
                                .InColumnValue("parent_id", folder.ParentFolderID)
                                .InColumnValue("title", folder.Title)
                                .InColumnValue("create_on", TenantUtil.DateTimeToUtc(folder.CreateOn))
                                .InColumnValue("create_by", folder.CreateBy.ToString())
                                .InColumnValue("modified_on", TenantUtil.DateTimeToUtc(folder.ModifiedOn))
                                .InColumnValue("modified_by", folder.ModifiedBy.ToString())
                                .InColumnValue("folder_type", (int)folder.FolderType)
                                .Identity(1, 0, true));

                        //itself link
                        db.ExecuteNonQuery(
                            new SqlInsert("files_folder_tree")
                                .InColumns("folder_id", "parent_id", "level")
                                .Values(folder.ID, folder.ID, 0));

                        //full path to root
                        db.ExecuteNonQuery(
                            new SqlInsert("files_folder_tree")
                                .InColumns("folder_id", "parent_id", "level")
                                .Values(
                                    new SqlQuery("files_folder_tree t")
                                        .Select(folder.ID.ToString(), "t.parent_id", "t.level + 1")
                                        .Where("t.folder_id", folder.ParentFolderID)));
                    }

                    tx.Commit();
                }

                if (!db.InTransaction && isnew)
                {
                    RecalculateFoldersCount(db, folder.ID);
                }
            }
            finally
            {
                if (db != null && ownsManager)
                {
                    //If it's our manager - dispose
                    db.Dispose();
                }
            }

            return folder.ID;
        }
开发者ID:vipwan,项目名称:CommunityServer,代码行数:83,代码来源:FolderDao.cs

示例3: Save

        /// <summary>
        /// Save to database. Inserts if new, updates otherwise
        /// </summary>
        /// <returns>
        /// A <see cref="System.Boolean"/>
        /// </returns>
        public bool Save()
        {
            if (Id == 0) {
                string sql = "INSERT INTO `file` (name, fullname, directory_id,"       +
                             " mimetype, title, description, copyright, width, height,"+
                             " size, resolution, exif, created, modified, keywords, "  +
                             " sha1_hash) "                                            +
                             "VALUES (@name, @fullname, @directory_id, @mimetype,"     +
                             " @title, @description, @copyright, @width, @height,"     +
                             " @size, @resolution, @exif, @created, @modified, "       +
                             " @keywords, @sha1_hash)";

                long tmpid;
                object mddate = null;
                if (Modified != Manager.NullDate)
                    mddate = Modified;
                var db = new DbManager(Manager.DbCon);
                if (db.QueryInsert(out tmpid, sql,
                                   DB.Param("name", Name),
                                   DB.Param("fullname", FullName),
                                   DB.Param("directory_id", DirectoryId),
                                   DB.Param("mimetype", Mimetype),
                                   DB.Param("title", Title),
                                   DB.Param("description", Description),
                                   DB.Param("copyright", Copyright),
                                   DB.Param("width", Width),
                                   DB.Param("height", Height),
                                   DB.Param("size", Size),
                                   DB.Param("resolution", Resolution),
                                   DB.Param("exif", Exif),
                                   DB.Param("created", Created),
                                   DB.Param("modified", mddate),
                                   DB.Param("keywords", Keywords),
                                                     DB.Param("sha1_hash", Sha1Hash)))
                {
                    Id = tmpid;

              SaveKeywords(Id, Keywords);

                    // Update the file index in Manager
                    Manager.AddToFileIndex(FullName);

                    foreach (PreviewFile f in Previews) {
                        f.FileId = Id;
                        if (!f.Save()) {
                            Log.Warning("  >>> Failed inserting preview \"{0}\" for " +
                                        "\"{1}\"\n", f.Name, FullName);
                        }
                    }

                    db.Dispose();
                    return true;
                }
                else {
                    Log.Debug("Failed inserting {0} into database!\n", FullName);
                    db.Dispose();
                    return false;
                }
            }
            else {
                // To regenerate hash
                sha1Hash = null;

                DB.Query("DELETE FROM `preview` WHERE [email protected]",
                                 DB.Param("id", Id));

                string sql = "UPDATE `file` SET "           +
                                         " [email protected]_hash,"       +
                                         " [email protected],"                 +
                                         " [email protected],"         +
                                         " [email protected]_id," +
                                         " [email protected],"         +
                                         " [email protected],"               +
                                         " [email protected],"   +
                                         " [email protected],"       +
                                         " [email protected],"               +
                                         " [email protected],"             +
                                         " [email protected],"     +
                                         " [email protected],"                 +
                                         " [email protected],"         +
                                         " [email protected] "         +
                                         "WHERE [email protected]";

                if (DB.Query(sql, DB.Param("sha1_hash", Sha1Hash),
                                  DB.Param("name", Name),
                                  DB.Param("fullname", FullName),
                                  DB.Param("directory_id", DirectoryId),
                                                    DB.Param("mimetype", Mimetype),
                                                    DB.Param("title", Title),
                                                    DB.Param("description", Description),
                                                    DB.Param("copyright", Copyright),
                                                    DB.Param("width", Width),
                                                    DB.Param("height", Height),
                                                    DB.Param("resolution", Resolution),
//.........这里部分代码省略.........
开发者ID:poppa,项目名称:MediaDB,代码行数:101,代码来源:MediaFile.cs


注:本文中的DbManager.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。