Winforms中的数据库C# [英] databases in winforms c#

查看:71
本文介绍了Winforms中的数据库C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 无法理解如何使用etnity框架管理数据库
我会尝试逐步描述我的问题
1)创建Winforms应用程序
2)将数据库(.mdf)添加到解决方案
3)当出现窗口选择数据库模型"时,选择数据集
4)然后完成,因为数据库为空
5)转到服务器资源管理器,选择创建的数据库,创建一些表
6)添加到解决方案EDM ADO.NET
问题是新数据没有出现在服务器资源管理器中的数据库中.这意味着在我的winforms应用程序运行时,我可以修改我的数据库(更改现有值,添加新的..).但是,如果我关闭我的应用程序,则服务器资源管理器中的数据库中没有更新.我不要忘记使用功能SaveChanges()


P.S.当我的应用程序运行时,值正在更新.认为,这意味着数据在数据集中而不是在数据库中更新.如果我是对的,请给出一个提示如何从数据集中更新数据库.
pps. visual studio 2010终极版.对不起,英语(

代码例如:

 dbentity db =  dbentity();
db.items.addobject( item(){ value  = something});
db.savechanges(); 

解决方案

假设您的EDM是dbentity:

using (var myContext = new dbentity())
{
    var person = new Person();
    // add some stuff here to populate
    myContext.Person.AddObject(person);
    myContext.SaveChanges();
}


如果人"是实体模型中的一个对象,那么这就是向数据库中的表添加记录所需要的全部. (用您自己的对象替换人").

祝您好运,并提供适合您的代码.


浏览下面的链接可能会解决您对C#和DataBase的所有疑问
Sqlserver和C# [ http://msdn.microsoft.com/en-us/library/aa697427 (v = vs.80).aspx [ 4) then finish, as database is empty
5) go to server explorer, choose created database, create some tables
6) add to solution EDM ADO.NET
the problem is that new data don''t appears in my database in server explorer. this means that while my winforms app is working, i can modify my db (change existing values, add new, ..). but if i close my app, there is no update in my database in server explorer. i DON''T forget to use function SaveChanges()


P.S. while my app is working, values are updating. think, that means that data updates in dataset, but not in database. if i am right, give a clue how to update database from dataset.
pps. visual studio 2010 ultimate. sorry for english(

code ex:

dbentity db=new dbentity();
db.items.addobject(new item() {value=something});
db.savechanges();

解决方案

Assuming your EDM is dbentity:

using (var myContext = new dbentity())
{
    var person = new Person();
    // add some stuff here to populate
    myContext.Person.AddObject(person);
    myContext.SaveChanges();
}


If ''person'' is an object in the entity model, that''s all that''s required to add a record to a table in the database. (Replace ''person'' with your own object).

Good luck and supply your code if this doesn''t work for you.


Go through the link below it might solve all your doubts regarding C# and DataBase
Sqlserver and C#[^]


check this blog
http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx[^]
--NDK


这篇关于Winforms中的数据库C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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