Encapsulates a MongoDB ASP. NET Link class

Source: Internet
Author: User

  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using MongoDB;
  5. <summary>
  6. Packaging classes for MONGO and Mongodatabase
  7. </summary>
  8. Public class Mymongodb:idisposable
  9. {
  10. private Mongo _mongo;
  11. private Imongodatabase _db;
  12. private static string connectionString = "Server=127.0.0.1;mydb";
  13. private static readonly string _connectionstring = Connectionstring.split ('; ') [0];  //"server=127.0.0.1";
  14. private static readonly string _dbname = Connectionstring.split ('; ')  [1];
  15. Public Mymongodb ()
  16. : This (_connectionstring, _dbname)
  17. {
  18. }
  19. Public Mymongodb (string dbName)
  20. : This (_connectionstring, DbName)
  21. {
  22. }
  23. // <summary>
  24. /// constructor. Based on the specified connection string and database name
  25. // </summary>
  26. /// <param name= "connectionString" > Connection string </param>
  27. //<param name= "dbName" > Database name, can be empty, but must be called Usedb () method before any operations database </param>
  28. Public Mymongodb (string connectionString, string dbName)
  29. {
  30. if ( string. IsNullOrEmpty (connectionString))
  31. throw New ArgumentNullException ("connectionString");
  32. _mongo = new MONGO (connectionString);
  33. //Connect MongoDB now
  34. _mongo. Connect ();
  35. if ( string. IsNullOrEmpty (dbName) = = false)
  36. _db = _mongo. Getdatabase (DbName);
  37. }
  38. // <summary>
  39. /// switch to the specified database
  40. // </summary>
  41. /// <param name= "DbName" ></param>
  42. // <returns></returns>
  43. Public imongodatabase usedb (string dbName)
  44. {
  45. if ( string. IsNullOrEmpty (dbName))
  46. throw New ArgumentNullException ("DbName");
  47. _db = _mongo. Getdatabase (DbName);
  48. return _db;
  49. }
  50. // <summary>
  51. /// get the current connected database
  52. // </summary>
  53. Public imongodatabase CurrentDb
  54. {
  55. Get
  56. {
  57. if (_db = = null)
  58. throw New Exception ("The current connection does not specify any database.") Please specify the database name in the constructor or call the Usedb () method to switch the database.  ");
  59. return _db;
  60. }
  61. }
  62. // <summary>
  63. /// get the specified collection of the current connection database "by type"
  64. // </summary>
  65. /// <typeparam name= "T" ></typeparam>
  66. // <returns></returns>
  67. Public imongocollection<t> getcollection<t> () where T: class
  68. {
  69. return this .  Currentdb.getcollection<t> ();
  70. }
  71. // <summary>
  72. /// Gets the specified collection of the current connection database "according to the specified name"
  73. // </summary>
  74. /// <typeparam name= "T" ></typeparam>
  75. // <param name= "Name" > collection name </param>
  76. // <returns></returns>
  77. Public imongocollection<t> getcollection<t> (string name) where T: class
  78. {
  79. return this .  currentdb.getcollection<t> (name);
  80. }
  81. public void Dispose ()
  82. {
  83. if (_mongo! = null) {
  84. _mongo. Dispose ();
  85. _mongo = null;
  86. }
  87. }
  88. }

Working with linked classes [CSharp]View Plaincopy
      1. Public void Insert (user user)
      2. {
      3. using (Mymongodb mdb = new Mymongodb ())
      4. {
      5. var collection = mdb. Getcollection<user> ();
      6. Collection. Insert (user);
      7. }

Encapsulates a MongoDB ASP. NET Link class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.