MongoDb作为C#.net的数据源

时间:2013-10-14 09:09:42

标签: c# mongodb mongodb-.net-driver

嗨我试图在datagrid视图中显示我在gridfs中保存的项目并通过单击它来检索它们,但到目前为止我只是查询mongo时遇到了麻烦。我不确定它需要什么名义文件类型     var cursor = collection.FindAs()

大多数其他示例使用collection.Find()。但我并没有将Mongo与一个班级联系起来。

继承我的代码,你能帮助我开始工作吗?

    public partial class WebForm1 : System.Web.UI.Page
    {
    MongoClient client;
    MongoServer server;
    MongoDatabase database;
    MongoGridFs gridFs;
    MongoCollection gridCol;


    protected void Page_Load(object sender, EventArgs e)
    {
        client   = new MongoClient();
        server   = client.GetServer();
        database = server.GetDatabase("mydb");
        gridCol = database.GetCollection("fs");
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
       //gridCol.FindAllAs<CFilez>

           MongoCollection collection = database.GetCollection("fs");
           var searchQuery = Query.EQ("gefunden", "one");
           var cursor = collection.FindAs(
             MongoCursor<CFilez> cursor = collection.FindAllAs<CFilez>();
            cursor.SetLimit(5);

            var list = cursor.ToList();

        GridView1.DataSource = list;
        GridView1.DataBind();
        StatusLabel.Text = "Jive";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                client = new MongoClient();
                server = client.GetServer();
                database = server.GetDatabase("mydb");

                string filename = FileUpload1.FileName;
                ////File file = File.OpenRead(filename);
                FileUpload1.SaveAs(Server.MapPath("~/Data/") + filename);
                String Fpath = Server.MapPath("~/Data/") + filename;
                ////FileUpload1.SaveAs(Server.MapPath("~/Data/") + filename);
                ////var id = ObjectId.Empty;
                //Stream file = File.OpenRead(Fpath);
                ////gridFs.AddFile(file, filename);
                //gridFs.AddFile(filename);

                using (var fs = new FileStream(Fpath, FileMode.Open))
                {
                    var gridFsInfo = database.GridFS.Upload(fs, filename);
                    var fileId = gridFsInfo.Id;

                }
                StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    }

0 个答案:

没有答案