国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫 - MongoDB - MongoDB.NET 2.2.4驅(qū)動版本對Mongodb3.3數(shù)據(jù)庫中GridFS增刪改查

MongoDB.NET 2.2.4驅(qū)動版本對Mongodb3.3數(shù)據(jù)庫中GridFS增刪改查

2020-05-09 16:05天風隼 MongoDB

這篇文章主要為大家詳細介紹了使用MongoDB.NET 2.2.4驅(qū)動版本對Mongodb3.3數(shù)據(jù)庫中GridFS增刪改查,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

Program.cs代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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獲取當前應用程序集的執(zhí)行目錄的上級的上級目錄
  //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的代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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 IMongoCollection<BsonDocument> 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.GetCollection<BsonDocument>(collectionName);
  }
 
  //this.collection = new MongoClient(url).GetDatabase(db).GetCollection<BsonDocument>(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(IMongoCollection<BsonDocument> 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 = Builders<GridFSFileInfo>.Filter.And(
  Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, "man"),
  Builders<GridFSFileInfo>.Filter.Gte(x => x.UploadDateTime, new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc)),
  Builders<GridFSFileInfo>.Filter.Lt(x => x.UploadDateTime, new DateTime(2017, 2, 1, 0, 0, 0, DateTimeKind.Utc)));
  var sort = Builders<GridFSFileInfo>.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 = Builders<GridFSFileInfo>.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的代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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轉(zhuǎn)換成流數(shù)據(jù),并保存為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;
 }
 
 //參數(shù)是圖片的路徑
 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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/// <summary>
 /// 手動記錄錯誤日志,不用Log4Net組件
 /// </summary>
 public class LogHelper
 {
 /// <summary>
 /// 將日志寫入指定的文件
 /// </summary>
 /// <param name="Path">文件路徑,如果沒有該文件,剛創(chuàng)建</param>
 /// <param name="content">日志內(nèi)容</param>
 public static void WriteFile(string content)
 {
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log";
  if (!Directory.Exists(Path))
  {
  //若文件目錄不存在 則創(chuàng)建
  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">文件路徑,如果沒有該文件,剛創(chuàng)建</param>
 /// <param name="content">日志內(nèi)容</param>
 public static void WriteFile(int content)
 {
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log";
  if (!Directory.Exists(Path))
  {
  //若文件目錄不存在 則創(chuàng)建
  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))
  {
  //若文件目錄不存在 則創(chuàng)建
  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();
 }
 }

結(jié)果如下:

MongoDB.NET 2.2.4驅(qū)動版本對Mongodb3.3數(shù)據(jù)庫中GridFS增刪改查

Mongodb數(shù)據(jù):

MongoDB.NET 2.2.4驅(qū)動版本對Mongodb3.3數(shù)據(jù)庫中GridFS增刪改查

查找圖片:

MongoDB.NET 2.2.4驅(qū)動版本對Mongodb3.3數(shù)據(jù)庫中GridFS增刪改查

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
  • MongoDB遷移sqlserver數(shù)據(jù)到MongoDb的方法

    遷移sqlserver數(shù)據(jù)到MongoDb的方法

    這篇文章主要介紹了遷移sqlserver數(shù)據(jù)到MongoDb的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下...

    聽楓xl9682021-01-03
  • MongoDBmongodb基本命令實例小結(jié)

    mongodb基本命令實例小結(jié)

    這篇文章主要介紹了mongodb基本命令,結(jié)合實例形式總結(jié)分析了MongoDB數(shù)據(jù)庫切換、查看、刪除、查詢等基本命令用法與操作注意事項,需要的朋友可以參考下...

    dawn-liu3652020-05-26
  • MongoDBMongodb實現(xiàn)定時備份與恢復的方法教程

    Mongodb實現(xiàn)定時備份與恢復的方法教程

    這篇文章主要給大家介紹了Mongodb實現(xiàn)定時備份與恢復的方法教程,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面...

    chenjsh364522020-05-13
  • MongoDBMongoDB憑什么躋身數(shù)據(jù)庫排行前五

    MongoDB憑什么躋身數(shù)據(jù)庫排行前五

    MongoDB以比去年同期超出65.96分的成績繼續(xù)雄踞榜單前五,這個增幅在全榜僅次于PostgreSQL的77.99,而其相對于4月份的6.10分的增長也是僅次于微軟SQL Server排名...

    孫浩峰3892020-05-22
  • MongoDB分布式文檔存儲數(shù)據(jù)庫之MongoDB分片集群的問題

    分布式文檔存儲數(shù)據(jù)庫之MongoDB分片集群的問題

    這篇文章主要介紹了分布式文檔存儲數(shù)據(jù)庫之MongoDB分片集群的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋...

    Linux-18743072020-12-20
  • MongoDBMongoDB安裝圖文教程

    MongoDB安裝圖文教程

    這篇文章主要為大家詳細介紹了MongoDB安裝圖文教程,分為兩大部分為大家介紹下載MongoDB和安裝MongoDB的方法,感興趣的小伙伴們可以參考一下 ...

    Yangyi.He6132020-05-07
  • MongoDBMongoDB 內(nèi)存使用情況分析

    MongoDB 內(nèi)存使用情況分析

    都說 MongoDB 是個內(nèi)存大戶,但是怎么知道它到底用了多少內(nèi)存呢...

    MongoDB教程網(wǎng)10002020-09-29
  • MongoDBMongoDB中javascript腳本編程簡介和入門實例

    MongoDB中javascript腳本編程簡介和入門實例

    作為一個數(shù)據(jù)庫,MongoDB有一個很大的優(yōu)勢——它使用js管理數(shù)據(jù)庫,所以也能夠使用js腳本進行復雜的管理——這種方法非常靈活 ...

    MongoDB教程網(wǎng)6982020-04-24
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲一区二区三区在线 | 国产激情一区二区三区成人免费 | 久久免费99精品久久久久久 | 午夜精品网站 | 91免费版在线观看 | 久久精品久久久久电影 | 日韩在线精品强乱中文字幕 | 久久久精品日本 | 亚洲品质自拍视频网站 | 亚洲免费在线 | 精品在线一区 | 色综合天天天天做夜夜夜夜做 | 亚洲精品成人 | 国内精品久久久久久中文字幕 | 国产91久久精品一区二区 | 性爽视频 | 精品一区二区三区中文字幕 | 国内精品一级毛片国产99 | 久久精品国产精品青草 | 欧美福利| 毛片无码国产 | 九热精品| a视频在线| 免费黄色小片 | 日韩成人在线网 | 中文字幕精品一区久久久久 | 亚洲精品久久久久久久久久久 | 一本久道视频一本久道 | 一级a性色生活片久久毛片 国产精品久久久久久久久久免费看 | 欧美日本韩国一区二区 | 欧美一区二区三区在线视频 | 99re国产| 免费在线观看黄色网址 | 在线免费黄色网址 | 午夜视频在线观看免费视频 | 久久精品六 | 午夜久久久 | 91麻豆精品国产91久久久更新资源速度超快 | 99亚洲精品 | 日日摸天天做天天添天天欢 | 国产精品亚洲视频 |