使用实体框架在WPF中的数据库中插入多行 [英] Insert multiple rows in database in WPF by using entity framework

查看:114
本文介绍了使用实体框架在WPF中的数据库中插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用实体框架在WPF中创建一个主细节条目表单。现在我的问题是当用户按下保存按钮时如何从DataGrid向Sql Database插入多行。我想通过使用实体框架将datagrid与数据库绑定。



我尝试过:



I want to make a master detail entry form in WPF by using entity framework. Now my question is how to insert multiple rows from DataGrid to Sql Database when user press save button. And I want to bind datagrid with database by using entity framework.

What I have tried:

I want to make a master detail entry form in WPF by using entity framework. Now my question is how to insert multiple rows from DataGrid to Sql Database when user press save button. And I want to bind datagrid with database by using entity framework.

推荐答案

好吧,我已经与Entity Framework一起工作了,我可以告诉你与使用普通的sql查询没有什么不同。首先要做的是声明你正在使用的db实体的名称:

eg -
Well,I have worked a little with Entity Framework and I can tell you that is not so different from working with normal sql queries.First thing that you have to do is to declare the name of the db entity that you are using:
e.g-
using(DBDemoEntites db=new DBDemoEntites())
{
}



然后你必须实例化你想要的表(表)插入值。例如,如果你想将数据插入表中水果:


then you have to instantiate the table(tables) that you want to insert the values into.For e.g if you want to insert the data into table Fruits:

Fruits d=new Fruits();



然后使用表中的引用开始将视图中数据网格的值传递到数据库中,如下所示:


then with the reference from the table you start passing values from the datagrid in the view into the database like this:

d.firstValue=firstValue;
d.secondValue=secondValue;





执行此操作后,将所有值保存到表格中,如下所示:


etc.
After you do this, you save all the values into the table like this:

db.Fruits.Add(d);



最后将所有内容保存到数据库中:


Finally you save everything into the database:

db.SaveChanges();





这就是我如何将值保存到数据库中实体框架。试试这个,看它是否有帮助。



This is how I save values into the db with Entity Framework.Try this and see if it helps.


这篇关于使用实体框架在WPF中的数据库中插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆