好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > MongoDB.NET 2.2.4驅動版本對Mongodb3.3數據庫中GridFS增刪改查

MongoDB.NET 2.2.4驅動版本對Mongodb3.3數據庫中GridFS增刪改查

熱門標簽:杭州人工電銷機器人價格 呼和浩特電銷外呼系統加盟 廣州長安公司怎樣申請400電話 云南外呼系統 電銷機器人是什么軟件 蘋果汽車租賃店地圖標注 怎么投訴地圖標注 濟南電銷機器人加盟公司 老虎洗衣店地圖標注

本文實例為大家分享了針對Mongodb3.3數據庫中GridFS增刪改查,供大家參考,具體內容如下

Program.cs代碼如下:

internal class Program 
 { 
 private static void Main(string[] args) 
 { 
  GridFSHelper helper = new GridFSHelper("mongodb://localhost", "GridFSDemo", "Pictures"); 
 
  #region 上傳圖片 
 
  //第一種 
  //Image image = Image.FromFile("D:\\dog.jpg"); 
  //byte[] imgdata = ImageHelper.ImageToBytes(image); 
  //ObjectId oid = helper.UploadGridFSFromBytes(imgdata); 
 
  //第二種 
  //Image image = Image.FromFile("D:\\man.jpg"); 
  //Stream imgSteam = ImageHelper.ImageToStream(image); 
  //ObjectId oid = helper.UploadGridFSFromStream("man",imgSteam); 
  //LogHelper.WriteFile(oid.ToString()); 
  // Console.Write(oid.ToString()); 
 
  #endregion 
 
  #region 下載圖片 
 
  //第一種 
  //ObjectId downId = new ObjectId("578e2d17d22aed1850c7855d"); 
  //byte[] Downdata= helper.DownloadAsByteArray(downId); 
  //string name= ImageHelper.CreateImageFromBytes("coolcar",Downdata); 
 
  //第二種 
  // byte[] Downdata = helper.DownloadAsBytesByName("QQQ"); 
  //string name = ImageHelper.CreateImageFromBytes("dog", Downdata); 
 
  //第三種 
  //byte[] Downdata = helper.DownloadAsBytesByName("QQQ"); 
  //Image img = ImageHelper.BytesToImage(Downdata); 
  //string path = Path.GetFullPath(@"../../DownLoadImg/") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; 
  ////使用path獲取當前應用程序集的執行目錄的上級的上級目錄 
  //img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); 
 
  #endregion 
 
  #region 查找圖片 
  GridFSFileInfo gridFsFileInfo = helper.FindFiles("man"); 
  Console.WriteLine(gridFsFileInfo.Id); 
  #endregion 
 
  #region 刪除圖片 
  //helper.DroppGridFSBucket(); 
  #endregion 
 
  Console.ReadKey(); 
 } 
 } 

GridFSHelper.cs的代碼如下:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using MongoDB.Bson; 
using MongoDB.Driver; 
using MongoDB.Driver.GridFS; 
 
namespace MongoDemo 
{ 
 public class GridFSHelper 
 { 
 private readonly IMongoClient client; 
 private readonly IMongoDatabase database; 
 private readonly IMongoCollectionBsonDocument> collection; 
 private readonly GridFSBucket bucket; 
 private GridFSFileInfo fileInfo; 
 private ObjectId oid; 
 
 public GridFSHelper() 
  : this( 
  ConfigurationManager.AppSettings["mongoQueueUrl"], ConfigurationManager.AppSettings["mongoQueueDb"], 
  ConfigurationManager.AppSettings["mongoQueueCollection"]) 
 { 
 } 
 
 public GridFSHelper(string url, string db, string collectionName) 
 { 
  if (url == null) 
  { 
  throw new ArgumentNullException("url"); 
  } 
  else 
  { 
  client = new MongoClient(url); 
  } 
 
  if (db == null) 
  { 
  throw new ArgumentNullException("db"); 
  } 
  else 
  { 
  database = client.GetDatabase(db); 
  } 
 
  if (collectionName == null) 
  { 
  throw new ArgumentNullException("collectionName"); 
  } 
  else 
  { 
  collection = database.GetCollectionBsonDocument>(collectionName); 
  } 
 
  //this.collection = new MongoClient(url).GetDatabase(db).GetCollectionBsonDocument>(collectionName); 
 
  GridFSBucketOptions gfbOptions = new GridFSBucketOptions() 
  { 
  BucketName = "bird", 
  ChunkSizeBytes = 1*1024*1024, 
  ReadConcern = null, 
  ReadPreference = null, 
  WriteConcern = null 
  }; 
  var bucket = new GridFSBucket(database, new GridFSBucketOptions 
  { 
  BucketName = "videos", 
  ChunkSizeBytes = 1048576, // 1MB 
  WriteConcern = WriteConcern.WMajority, 
  ReadPreference = ReadPreference.Secondary 
  }); 
  this.bucket = new GridFSBucket(database, null); 
 } 
 
 public GridFSHelper(IMongoCollectionBsonDocument> collection) 
 { 
  if (collection == null) 
  { 
  throw new ArgumentNullException("collection"); 
  } 
  this.collection = collection; 
  this.bucket = new GridFSBucket(collection.Database); 
 } 
 
 
 public ObjectId UploadGridFSFromBytes(string filename, Byte[] source) 
 { 
  oid = bucket.UploadFromBytes(filename, source); 
  return oid; 
 } 
 
 public ObjectId UploadGridFSFromStream(string filename,Stream source) 
 { 
  using (source) 
  { 
  oid = bucket.UploadFromStream(filename, source); 
  return oid; 
  } 
 } 
 
 public Byte[] DownloadAsByteArray(ObjectId id) 
 { 
  Byte[] bytes = bucket.DownloadAsBytes(id); 
  return bytes; 
 } 
 
 public Stream DownloadToStream(ObjectId id) 
 { 
  Stream destination = new MemoryStream(); 
  bucket.DownloadToStream(id, destination); 
  return destination; 
 } 
 
 public Byte[] DownloadAsBytesByName(string filename) 
 { 
  Byte[] bytes = bucket.DownloadAsBytesByName(filename); 
  return bytes; 
 } 
 
 public Stream DownloadToStreamByName(string filename) 
 { 
  Stream destination = new MemoryStream(); 
  bucket.DownloadToStreamByName(filename, destination); 
  return destination; 
 } 
 
 public GridFSFileInfo FindFiles(string filename) 
 { 
  var filter = BuildersGridFSFileInfo>.Filter.And( 
  BuildersGridFSFileInfo>.Filter.Eq(x => x.Filename, "man"), 
  BuildersGridFSFileInfo>.Filter.Gte(x => x.UploadDateTime, new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc)), 
  BuildersGridFSFileInfo>.Filter.Lt(x => x.UploadDateTime, new DateTime(2017, 2, 1, 0, 0, 0, DateTimeKind.Utc))); 
  var sort = BuildersGridFSFileInfo>.Sort.Descending(x => x.UploadDateTime); 
  var options = new GridFSFindOptions 
  { 
  Limit = 1, 
  Sort = sort 
  }; 
  using (var cursor = bucket.Find(filter, options)) 
  { 
   fileInfo = cursor.ToList().FirstOrDefault(); 
  } 
  return fileInfo; 
 } 
 
 
 public void DeleteAndRename(ObjectId id) 
 { 
  bucket.Delete(id); 
 } 
 
 //The “fs.files” collection will be dropped first, followed by the “fs.chunks” collection. This is the fastest way to delete all files stored in a GridFS bucket at once. 
 public void DroppGridFSBucket() 
 { 
  bucket.Drop(); 
 } 
 
 public void RenameAsingleFile(ObjectId id,string newFilename) 
 { 
  bucket.Rename(id, newFilename); 
 } 
 
 public void RenameAllRevisionsOfAfile(string oldFilename,string newFilename) 
 { 
  var filter = BuildersGridFSFileInfo>.Filter.Eq(x => x.Filename, oldFilename); 
  var filesCursor = bucket.Find(filter); 
  var files = filesCursor.ToList(); 
  foreach (var file in files) 
  { 
  bucket.Rename(file.Id, newFilename); 
  } 
 } 
 
 } 
} 

ImageHelper.cs的代碼如下:

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace MongoDemo 
{ 
 public static class ImageHelper 
 { 
 /// summary> 
 /// //將Image轉換成流數據,并保存為byte[] 
 /// /summary> 
 /// param name="image">/param> 
 /// returns>/returns> 
 public static byte[] ImageToBytes(Image image) 
 { 
  ImageFormat format = image.RawFormat; 
  using (MemoryStream ms = new MemoryStream()) 
  { 
  if (format.Equals(ImageFormat.Jpeg)) 
  { 
   image.Save(ms, ImageFormat.Jpeg); 
  } 
  else if (format.Equals(ImageFormat.Png)) 
  { 
   image.Save(ms, ImageFormat.Png); 
  } 
  else if (format.Equals(ImageFormat.Bmp)) 
  { 
   image.Save(ms, ImageFormat.Bmp); 
  } 
  else if (format.Equals(ImageFormat.Gif)) 
  { 
   image.Save(ms, ImageFormat.Gif); 
  } 
  else if (format.Equals(ImageFormat.Icon)) 
  { 
   image.Save(ms, ImageFormat.Icon); 
  } 
  byte[] buffer = new byte[ms.Length]; 
  //Image.Save()會改變MemoryStream的Position,需要重新Seek到Begin 
  ms.Seek(0, SeekOrigin.Begin); 
  ms.Read(buffer, 0, buffer.Length); 
  return buffer; 
  } 
 } 
 
 
 public static Stream ImageToStream(Image image) 
 { 
  ImageFormat format = image.RawFormat; 
  MemoryStream ms = new MemoryStream(); 
 
  if (format.Equals(ImageFormat.Jpeg)) 
  { 
  image.Save(ms, ImageFormat.Jpeg); 
  } 
  else if (format.Equals(ImageFormat.Png)) 
  { 
  image.Save(ms, ImageFormat.Png); 
  } 
  else if (format.Equals(ImageFormat.Bmp)) 
  { 
  image.Save(ms, ImageFormat.Bmp); 
  } 
  else if (format.Equals(ImageFormat.Gif)) 
  { 
  image.Save(ms, ImageFormat.Gif); 
  } 
  else if (format.Equals(ImageFormat.Icon)) 
  { 
  image.Save(ms, ImageFormat.Icon); 
  } 
  return ms; 
 } 
 
 //參數是圖片的路徑 
 public static byte[] GetPictureData(string imagePath) 
 { 
  FileStream fs = new FileStream(imagePath, FileMode.Open); 
  byte[] byteData = new byte[fs.Length]; 
  fs.Read(byteData, 0, byteData.Length); 
  fs.Close(); 
  return byteData; 
 } 
 
 
 
 /// summary> 
 /// Convert Byte[] to Image 
 /// /summary> 
 /// param name="buffer">/param> 
 /// returns>/returns> 
 public static Image BytesToImage(byte[] buffer) 
 { 
  MemoryStream ms = new MemoryStream(buffer); 
  Image image = System.Drawing.Image.FromStream(ms); 
  return image; 
 } 
 
 /// summary> 
 /// Convert Byte[] to a picture and Store it in file 
 /// /summary> 
 /// param name="fileName">/param> 
 /// param name="buffer">/param> 
 /// returns>/returns> 
 public static string CreateImageFromBytes(string fileName, byte[] buffer) 
 { 
  string file = fileName; 
  Image image = BytesToImage(buffer); 
  ImageFormat format = image.RawFormat; 
  if (format.Equals(ImageFormat.Jpeg)) 
  { 
  file += ".jpg"; 
  } 
  else if (format.Equals(ImageFormat.Png)) 
  { 
  file += ".png"; 
  } 
  else if (format.Equals(ImageFormat.Bmp)) 
  { 
  file += ".bmp"; 
  } 
  else if (format.Equals(ImageFormat.Gif)) 
  { 
  file += ".gif"; 
  } 
  else if (format.Equals(ImageFormat.Icon)) 
  { 
  file += ".icon"; 
  } 
  System.IO.FileInfo info = new System.IO.FileInfo(Path.GetFullPath(@"DownLoadImg\")); //在當前程序集目錄中添加指定目錄DownLoadImg 
  System.IO.Directory.CreateDirectory(info.FullName); 
  File.WriteAllBytes(info+file, buffer); 
  return file; 
 } 
 } 
} 

LogHelper.cs代碼如下:

/// summary> 
 /// 手動記錄錯誤日志,不用Log4Net組件 
 /// /summary> 
 public class LogHelper 
 { 
 /// summary> 
 /// 將日志寫入指定的文件 
 /// /summary> 
 /// param name="Path">文件路徑,如果沒有該文件,剛創建/param> 
 /// param name="content">日志內容/param> 
 public static void WriteFile(string content) 
 { 
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log"; 
  if (!Directory.Exists(Path)) 
  { 
  //若文件目錄不存在 則創建 
  Directory.CreateDirectory(Path); 
  } 
  Path += "\\" + DateTime.Now.ToString("yyMMdd") + ".log"; 
  if (!File.Exists(Path)) 
  { 
  File.Create(Path).Close(); 
  } 
  StreamWriter writer = new StreamWriter(Path, true, Encoding.GetEncoding("gb2312")); 
  writer.WriteLine("時間:" + DateTime.Now.ToString()); 
  writer.WriteLine("日志信息:" + content); 
  writer.WriteLine("-----------------------------------------------------------"); 
  writer.Close(); 
  writer.Dispose(); 
 } 
 
 /// summary> 
 /// 將日志寫入指定的文件 
 /// /summary> 
 /// param name="Path">文件路徑,如果沒有該文件,剛創建/param> 
 /// param name="content">日志內容/param> 
 public static void WriteFile(int content) 
 { 
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log"; 
  if (!Directory.Exists(Path)) 
  { 
  //若文件目錄不存在 則創建 
  Directory.CreateDirectory(Path); 
  } 
  Path += "\\" + DateTime.Now.ToString("yyMMdd") + ".log"; 
  if (!File.Exists(Path)) 
  { 
  File.Create(Path).Close(); 
  } 
  StreamWriter writer = new StreamWriter(Path, true, Encoding.GetEncoding("gb2312")); 
  writer.WriteLine("時間:" + DateTime.Now.ToString()); 
  writer.WriteLine("日志信息:" + content); 
  writer.WriteLine("-----------------------------------------------------------"); 
  writer.Close(); 
  writer.Dispose(); 
 } 
 
 
 /// summary> 
 /// 將日志寫入指定的文件 
 /// /summary> 
 /// param name="erroMsg">錯誤詳細信息/param> 
 /// param name="source">源位置/param> 
 /// param name="fileName">文件名/param> 
 public static void WriteFile(string erroMsg, string source, string stackTrace, string fileName) 
 { 
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log"; 
  if (!Directory.Exists(Path)) 
  { 
  //若文件目錄不存在 則創建 
  Directory.CreateDirectory(Path); 
  } 
  Path += "\\" + DateTime.Now.ToString("yyMMdd") + ".log"; 
  if (!File.Exists(Path)) 
  { 
  File.Create(Path).Close(); 
  } 
  StreamWriter writer = new StreamWriter(Path, true, Encoding.GetEncoding("gb2312")); 
  writer.WriteLine("時間:" + DateTime.Now.ToString()); 
  writer.WriteLine("文件:" + fileName); 
  writer.WriteLine("源:" + source); 
  writer.WriteLine("錯誤信息:" + erroMsg); 
  writer.WriteLine("-----------------------------------------------------------"); 
  writer.Close(); 
  writer.Dispose(); 
 } 
 } 

結果如下:

Mongodb數據:

查找圖片:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • MongoDB服務端JavaScript腳本使用方法
  • mongoDB中CRUD的深入講解
  • Mongo DB增刪改查命令
  • mongodb+php實現簡單的增刪改查
  • PHP簡單操作MongoDB的方法(安裝及增刪改查)
  • mongodb增刪改查詳解_動力節點Java學院整理
  • MongoDB簡單操作示例【連接、增刪改查等】
  • koa+mongoose實現簡單增刪改查接口的示例代碼
  • Node.js對MongoDB進行增刪改查操作的實例代碼
  • java連接Mongodb實現增刪改查
  • MongoDB的基本操作實例詳解【服務端啟動,客戶端連接,CRUD操作】

標簽:雞西 廈門 遼陽 興安盟 無錫 泰安 玉林 自貢

巨人網絡通訊聲明:本文標題《MongoDB.NET 2.2.4驅動版本對Mongodb3.3數據庫中GridFS增刪改查》,本文關鍵詞  MongoDB.NET,2.2.4,驅動,版本,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《MongoDB.NET 2.2.4驅動版本對Mongodb3.3數據庫中GridFS增刪改查》相關的同類信息!
  • 本頁收集關于MongoDB.NET 2.2.4驅動版本對Mongodb3.3數據庫中GridFS增刪改查的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 熟妇高潮精品一区二区三区下载| 99er热久久精品中文字幕| 中文字幕一区二区三区乱码在线 | 小姨子的爱在线观看| 白胖肥妇熟女Bwwbww图片| 色人阁导航| 无码精品蜜桃无套内谢的新婚少 | 国产?精品?无码?在线看漫画| 男人一边做边爱一边吃奶| mitunav在线| 啊…学长我们换个地方| 国产精品爆乳在线第一区| 91人妻精品一区二区三区果冻| www.无码爆浆蜜桃.com| 大渡口区| 久久久久麻豆V国产精华液好用吗| 欧美videos巨大18free| 久久久久精品免费人妻奶头| 在厨房被C到高潮A片漫画免费看| 美国一级毛片在线| 精品亚洲AV在线无码综合文学 | 一级成人a毛片免费播放| 无限看片动漫的视频在线观看免费| 严厉主人 调教室 规矩| 91色欲色香天天天综合欧美狼牙 | 文登市| 在线观看深夜视频| 91在线码无精品秘?入口| 徐若瑄三级真做| 亚洲国产成人精品女人久久久| 肥妇BBwBBw高潮| 中文字幕avv| 中国videos偷窥| 91人妻精品国产综合| 91视日韩人妻欧美在线软件| 乱妇伦小说| 男男上床视频| 太深了慢一点轻一点| 亚洲精品久久久中文字幕痴女| 国产网站免费视频| 成人国产mv免费视频|