1

嗨,我试图在数据网格视图中显示我保存在 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;
            }
        }
    }
4

0 回答 0