2012年7月3日 星期二

LINQ存取範例

查詢
            StringBuilder msg = new StringBuilder();
            using (Entities db = new Entities())
            {
                var q = (from c in db.SYS_EIPLOG
                         orderby c.LOG_DATE descending
                         select c);
                foreach (var s in q)
                {
                    msg.AppendFormat("{0}\r\n", s.LOG_ACTION);
                }
            }
            label1.Text = msg.ToString();   
新增
            using (Entities db = new Entities())
            {
                IPAddress sip = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
                SYS_EIPLOG slog = new SYS_EIPLOG();
                slog.REC_NO = (from c in db.SYS_EIPLOG select c).Count() + 1;
                slog.LOG_DATE = System.DateTime.Now;
                slog.LOG_USER = "K0675";
                slog.LOG_IP = sip.ToString();
                slog.LOG_MOD = "linq";
                slog.LOG_ACTION = "TEST";
                slog.LOG_VALUE = "";
                db.SYS_EIPLOG.AddObject(slog);
                db.SaveChanges();               
            }       
修改
            using (Entities db = new Entities())
            {
                var q = (from c in db.SYS_EIPLOG
                         where c.REC_NO == 0
                         select c).First();
                q.LOG_VALUE = "2";
                db.SaveChanges();
            }       
刪除
            using (Entities db = new Entities())
            {
                var q = (from c in db.SYS_EIPLOG
                         where c.REC_NO == 0
                         select c).First();
                db.SYS_EIPLOG.DeleteObject(q);
                db.SaveChanges();
            }

     

--
看到、想到、說到、做到 
能夠填平大海的誓言,也比不上邁出一步的價值 
是以吾輩此生,再無任何誓言

沒有留言: