首页 > Linq To Entity 的增删改查(技术储备,怕忘了)

Linq To Entity 的增删改查(技术储备,怕忘了)

1.新增Customers记录

            using (NorthwindEntities myDb = new NorthwindEntities())

            {

                Customers customer = new Customers();

 

                Random rm = new Random();

 

                customer.CustomerID = "A" + rm.Next(9999).ToString();

                customer.CompanyName = "EntLib.com Forum";

                customer.Address = "http://www.EntLib.com";

 

                myDb.AddToCustomers(customer);

 

                int count = myDb.SaveChanges();

 

                txtCustomerID.Text = customer.CustomerID;

            }

 

2.更新Customers记录

            using (NorthwindEntities myDb = new NorthwindEntities())

            {

                var query = from customer in myDb.Customers

                            where customer.CustomerID == txtCustomerID.Text.Trim()

                            select customer;

 

                foreach (var row in query)

                {

                    row.CompanyName = "Updated Company Name";

                }

 

                myDb.SaveChanges();

            }

 

3.删除Customers记录

            using (NorthwindEntities myDb = new NorthwindEntities())

            {

                var query = from customer in myDb.Customers

                            where customer.CustomerID == txtCustomerID.Text.Trim()

                            select customer;

 

                foreach (var row in query)

                {

                    myDb.DeleteObject(row);

                }

 

                myDb.SaveChanges();

            }

 

4.查询Customers,返回所有记录

            using (NorthwindEntities myDb = new NorthwindEntities())

            {

                dataGridView1.DataSource = myDb.Customers;

            }

转载于:https://www.cnblogs.com/mbailing/archive/2010/10/09/1846302.html

更多相关:

  • 菜鸟一枚,正在学习C++ Gui Qt4,整理很零碎,欢迎批评指正   1.窗口标题: QWidget *window = new QWidget; window->setWindowTitle("Enter Your Age"); **************************************** 关于标题...

  • 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 总体思路是: 比较两个链表头节点,较小的插入新链表指针之后,同时较小链表指针向后移动一位 实现如下: ListNode* mergeTwo...

  • 1.直接调用微软socket对象处理 static void Main(string[] args){try{IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });//在3721端口新建一个TcpListener对象TcpListener listener = new...

  •   现在很多地方都会用到zookeeper, 用到它的地方就是为了实现分布式。用到的场景就是服务注册,比如一个集群服务器,需要知道哪些服务器在线,哪些服务器不在线。   ZK有一个功能,就是创建临时节点,当机器启动应用的时候就会连接到一个ZK节点,然后创建一个临时节点,那么通过获取监听该路径,并且获取该路径下的节点数量就知道有哪些服务...

  • 前台到后台java时data日期类型的转化 在实体类中用@DataTimeFormat,这样设置即使传过来是空的字符串也是可以转的,要和前面传过来的格式一致,如 @XmlElement(name="BeginDate") @DateTimeFormat(pattern="yyyy-MM-dd") private Date begin...