linq to sql 小实例

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace LinqToSqlFrom1

{

    using DBSpace;

    using System.Data.Linq;



    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {

            DBConn conn = new DBConn("database=Jiang;User ID = Jiang;pwd=admin;server=JIANG-PC\\SQL2010");

            var user = from u in conn.CreateUser where u.Uid < 3 orderby u.Uid select u;



            foreach (var v in user)

                this.listBox1.Items.Add(v.Name.ToString());

        }



        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            this.listBox2.Items.Clear();

            DBConn conn = new DBConn("database=Jiang;User ID = Jiang;pwd=admin;server=JIANG-PC\\SQL2010");

            var users = from u in conn.CreateUser from us in u.Userid where u.Name == this.listBox1.SelectedItem.ToString() select us;

            foreach (var v in users)

                this.listBox2.Items.Add(v.UserName.ToString());



        }

    }

}

namespace DBSpace

{

    using System.Data.Linq;

    using System.Data.Linq.Mapping;



    public class DBConn : DataContext

    {

        public Table<CDB_MEMBERS> CreateUser;

        public DBConn(string connectionstring)

            : base(connectionstring)

        { }

        public DBConn(IDbConnection connectionstring)

            : base(connectionstring)

        { }

    }

    [Table(Name = "cdb_members")]

    public class CDB_MEMBERS

    {

        [Column(IsPrimaryKey = true)]

        public int Uid;

        [Column]

        public string Name;

        [Column]

        public string Pass;

        private EntitySet<CDB_MEMBERSINFO> userid;

        [Association(Storage = "userid", OtherKey = "Uid")]

        public EntitySet<CDB_MEMBERSINFO> Userid

        {

            get { return this.userid; }

            set { this.userid.Assign(value); }

        }

        [Table(Name = "cdb_membersinfo")]

        public class CDB_MEMBERSINFO

        {

            [Column(IsPrimaryKey = true)]

            public int Fid;

            [Column]

            public string UserName;

            [Column]

            public string Sex;

            [Column]

            public string Age;

            [Column]

            public string Address;

            [Column]

            public string Tel;

            [Column]

            public string Phone;

            [Column]

            public int Uid;



            private EntityRef<CDB_MEMBERS> uid;

            [Association(Storage = "uid", ThisKey = "Uid")]

            public CDB_MEMBERS Members

            {

                get { return this.uid.Entity; }

                set { this.uid.Entity = value; }



            }

        }

    }

}

你可能感兴趣的:(LINQ)