数据库/对象映射
这是一个初学者的问题,但它一直让我感到沮丧...... 顺便说一句,我正在使用 C#。
我想创建几个类,每个类都有自己的属性和方法。我还希望有一个数据库来存储这些类的某些实例,以防我需要再次查看它们。所以,举例来说……
class Polygon
{
String name;
Double perimiter;
int numSides;
public Double GetArea()
{
// ...
}
}
class Circle
{
String name;
Double radius;
public void PrintName()
{
// ...
}
}
假设我有这些课程。我还想要一个包含表“多边形”和“圆”以及列“名称”“周长”“半径”等的数据库。我想要一种简单的方法将类实例保存到数据库中,或提取类实例从数据库中出来。
我以前一直使用 MS Access 来处理我的数据库,我不介意使用它,但如果除了 .NET 之外不需要安装任何东西,我会更喜欢。
我在网上做了一些研究,但我想在这里得到一些意见。我看过 Linq-to-Sql,但似乎你需要 Sql-Server。这是真的吗?如果是这样,我真的不想使用它,因为我不想到处都安装它。
不管怎样,我只是在寻找一些想法/见解/建议/等等。所以如果可以的话请帮助我。
谢谢。
This is a beginner question, but it's been frustrating me...
I am using C#, by the way.
I'd like to make a few classes, each with their own properties and methods. I would also like to have a database to store certain instances of these classes in case I would ever need to look at them again. So, for example...
class Polygon
{
String name;
Double perimiter;
int numSides;
public Double GetArea()
{
// ...
}
}
class Circle
{
String name;
Double radius;
public void PrintName()
{
// ...
}
}
Say I've got these classes. I also want a database that has the TABLES "Polygon" and "Circle" with the COLUMNS "name" "perimeter" "radius" etc. And I want an easy way to save a class instance into the database, or pull a class instance out of the database.
I have previously been using MS Access for my database stuff, which I don't mind using, but I would prefer if nothing other than .NET need to be installed.
I've been researching online a bit, but I wanted to get some opinions on here. I have looked at Linq-to-Sql, but it seems you need Sql-Server. Is this true? If so, I'd really rather not use it because I don't want to have to have it installed everywhere.
Anway, I'm just fishing for some ideas/insights/suggestions/etc. so please help me out if you can.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
实体框架与SQL Server Express 将满足您的需求。 EF 可能有点大材小用,但 SQLServer Express 可以通过应用程序安装程序轻松安装在本地计算机上,并提供比 access 更丰富的数据库功能集。
您可能还想看看 SQLLite,它是一个嵌入式数据库,无需安装,只需分发一个还有几个图书馆。
Entity Framework with SQL Server Express would suit your needs. EF may be overkill, but SQLServer Express can easily be installed by your application installer on the local machine and provides a much richer DB feature set than access does.
You may also want to look at SQLLite, which is an embedded database which requires no installation, just the distribution of a few more libraries.
我建议使用 SQL CE 4.0(公共测试版 2)和 EF 代码优先 (CTP 5)
EF: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=35adb688-f8a7-4d28-86b1-b6235385389d
SQL CE:< a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=d9d933e7-9376-445e-8217-0c1e102a380e" rel="nofollow">http://www.microsoft.com/ downloads/en/details.aspx?FamilyID=d9d933e7-9376-445e-8217-0c1e102a380e
不要让 Beta 状态吓到您,这两种产品都已经非常稳定,如果是用于业余爱好项目,或者只是学习,我不会犹豫。
创建/查询数据库可以像这样简单
,这是您需要添加到 app.config 才能使其工作的连接字符串
<添加名称=“ShapeDbContext”connectionString=“数据源=|DataDirectory|\Database.sdf”providerName=“System.Data.SqlServerCe.4.0”/>
一个主要缺点是您无法更新现有数据库 - 您必须手动执行此操作 - 不过,这应该通过 EF Code First 的最终版本来修复...希望在一月的某个时候...
一些主要优点:简单的东西不需要太多代码,您仍然可以深入挖掘,Linq 很有趣,EF Code First 很有趣...尝试一下!
I would recommend using SQL CE 4.0 (public beta 2) and EF code first (CTP 5)
EF: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=35adb688-f8a7-4d28-86b1-b6235385389d
SQL CE: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=d9d933e7-9376-445e-8217-0c1e102a380e
Don't let the beta state scare you, both products are already very stable and if it's for a hobby project, or just learning, I wouldn't hesitate.
Creating/quering a database can be as simple as
And this is the connection string you need to add to app.config for it to work
<connectionStrings>
<add name="ShapeDbContext" connectionString="Data Source=|DataDirectory|\Database.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
One major drawback is that you cannot update an existing database - you'll have to do it manually - however that should be fixed with the final release of EF Code First... somewhere in january hopefully...
Some majar plusses: don't need much code for simple stuff, you can still digg deep, Linq is fun, EF Code First is fun... Give it a try!
LINQ2SQL
非常适合一对一对象关系映射。也就是说,对象的属性作为列保存在数据库中。这似乎是你的情况。LINQ2SQL
的本机提供程序自然是针对SQL Server
的,但现在有针对Oracle
、MySql
和PostGreSQL。将
LINQ2SQL
与SQL Server
结合使用意味着您只需要在程序可寻址的位置中有一个数据库实例。SQL Server
本身只需要安装在该位置。LINQ2SQL
is ideal for 1 to 1 object relational mappings. That is, where the properties of the object are persisted as columns in a database. Which appears to be your situation.The native provider for
LINQ2SQL
is naturally forSQL Server
but there are now 3rd party providers forOracle
,MySql
andPostGreSQL
.Using
LINQ2SQL
withSQL Server
means you just need an instance of the database in a location addressable by your program.SQL Server
itself will only need to be installed at that location.我自己从未使用过它,但我知道 NHibernate 会执行您所要求的操作: http://www.nhibernate.com /
I've never used it myself, but I know that NHibernate does what you are asking: http://www.nhibernate.com/
不,你不需要有用于 linq to sql 的 sql 服务器,你也可以在你的项目中使用 add>>newItem>>localdatabse。并且你可以使用 linq to sql 。
no its not necessory that u shud have sql server for linq to sql,u can also use add>>newItem>>localdatabse in ur project.and u can use linq to sql with it.
您始终可以将对象序列化为 xml 文件,然后在想要取回它们时反序列化它们。
我知道您说过您想要一个数据库,但我不确定如何在不安装数据库的情况下拥有数据库。
You can always serialize the objects to xml files, then deserialize them when you want to get them back.
I know you said you want a database, but I'm not sure how you can have a database without installing one.