2013年3月28日 星期四

mongodb in ASP.NET 之 第一次切芒果就上手

mangle...





喔耶~ NoSQL

喔耶~ BSON 百度

喔耶~ mongodb Wiki





要是以為我會寫介紹文 就太傻太天真

直接玩弄





截圖





testQuery.aspx

    <form id="form1" runat="server">
        name:<asp:TextBox ID="name_tb" runat="server"></asp:TextBox>
        age:<asp:TextBox ID="ageHead_tb" runat="server"></asp:TextBox>
        ~<asp:TextBox ID="ageEnd_tb" runat="server"></asp:TextBox>
        <asp:CheckBox ID="male_cb" runat="server" Text="Male" />
        <asp:CheckBox ID="female_cb" runat="server" Text="Female" />
        <asp:Button ID="Query" runat="server" Text="Query" OnClick="Query_Click" />
        <asp:GridView ID="show_gv" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="_id" HeaderText="_id" />
                <asp:BoundField DataField="name" HeaderText="name" />
                <asp:BoundField DataField="age" HeaderText="age" />
                <asp:BoundField DataField="gender" HeaderText="gender" />
            </Columns>
        </asp:GridView>
    </form>




testQuery.aspx.cs

using System;
using System.Linq;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace testMongo
{
    public partial class testQuery : System.Web.UI.Page
    {
        private MongoCollection memberCollection = GetMemberCollection();

        protected void Query_Click(object sender, EventArgs e)
        {
            bool ckMale, ckFemale;
            if (
                (!male_cb.Checked && !female_cb.Checked) ||
                (male_cb.Checked && female_cb.Checked)
            )
            {
                ckMale = true;
                ckFemale = false;
            }
            else
            {
                ckMale = ckFemale = male_cb.Checked;
            }
            int ageHead =
                string.IsNullOrEmpty(ageHead_tb.Text)
                ? 0
                : Convert.ToInt32(ageHead_tb.Text);
            int ageEnd =
                string.IsNullOrEmpty(ageEnd_tb.Text)
                ? int.MaxValue
                : Convert.ToInt32(ageEnd_tb.Text);
            IQueryable q = memberCollection.AsQueryable<Member>().Where(
                x =>
                    x.name.Contains(name_tb.Text) &&
                    x.age >= ageHead &&
                    x.age <= ageEnd &&
                    (
                        x.gender == ckMale ||
                        x.gender == ckFemale
                    )
            );
            IMongoQuery mongoQuery = (q as MongoQueryable<Member>).GetMongoQuery();
            show_gv.DataSource = memberCollection.FindAs<Member>(mongoQuery);
            show_gv.DataBind();
        }

        private static MongoCollection<Member> GetMemberCollection()
        {
            MongoServer server = MongoServer.Create("mongodb://192.168.7.25:27017");
            MongoDatabase database = server.GetDatabase("test");
            MongoCollection<Member> member = database.GetCollection<Member>("member");
            return member;
        }
    }
}




截圖





testSaveMember.aspx

    <form id="form1" runat="server">
        name:<asp:TextBox ID="name_tb" runat="server"></asp:TextBox>
        age:<asp:TextBox ID="age_tb" runat="server"></asp:TextBox>
        <asp:RadioButton ID="male_rb" runat="server" Checked="True" GroupName="Gender" Text="Male" />
        <asp:RadioButton ID="female_rb" runat="server" GroupName="Gender" Text="Female" />
        <asp:Button ID="insert" runat="server" OnClick="insert_Click" Text="Insert" />
        <br />
        _id:<asp:TextBox ID="id_tb" runat="server"></asp:TextBox>
        <asp:Button ID="update" runat="server" Text="Update" OnClick="update_Click" />
        <asp:Button ID="delete" runat="server" Text="Delete" OnClick="delete_Click" />
        <asp:GridView ID="show_gv" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="_id" HeaderText="_id" />
                <asp:BoundField DataField="name" HeaderText="name" />
                <asp:BoundField DataField="age" HeaderText="age" />
                <asp:BoundField DataField="gender" HeaderText="gender" />
            </Columns>
        </asp:GridView>
    </form>




testSaveMember.aspx.cs


using System;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace testMongo
{
    public partial class testSaveMember : System.Web.UI.Page
    {
        private MongoCollection memberCollection = GetMemberCollection();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            BindGridViwe();
        }

        private void BindGridViwe()
        {
            show_gv.DataSource = memberCollection.FindAllAs(typeof(Member));
            show_gv.DataBind();
        }

        protected void insert_Click(object sender, EventArgs e)
        {
            SaveMember(
                name: name_tb.Text,
                age: int.Parse(age_tb.Text),
                gender: male_rb.Checked
            );
        }

        protected void update_Click(object sender, EventArgs e)
        {
            SaveMember(
                _id: new ObjectId(id_tb.Text),
                name: name_tb.Text,
                age: int.Parse(age_tb.Text),
                gender: male_rb.Checked
            );
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            DeleteMember(new ObjectId(id_tb.Text));
            BindGridViwe();
        }

        private void SaveMember(string name, int age, bool gender)
        {
            SaveMember(ObjectId.Empty, name, age, gender);
        }

        private void SaveMember(ObjectId _id, string name, int age, bool gender)
        {
            Member member = new Member { _id = _id, name = name, age = age, gender = gender };
            memberCollection.Save(member);
            BindGridViwe();
        }

        private void DeleteMember(ObjectId _id)
        {
            IQueryable q = memberCollection.AsQueryable<Member>().Where(x => x._id == _id);
            IMongoQuery mongoQuery = (q as MongoQueryable<Member>).GetMongoQuery();
            memberCollection.Remove(mongoQuery);
        }

        private static MongoCollection<Member> GetMemberCollection()
        {
            MongoServer server = MongoServer.Create("mongodb://192.168.7.25:27017");
            MongoDatabase database = server.GetDatabase("test");
            MongoCollection<Member> member = database.GetCollection<Member>("member");
            return member;
        }
    }
}