I'm new to C# with MSSQL, so I was wondering, what is the most reliable vs Easy method to connect my C# App to MSSQL to do the usual read/add/edit/update/delete .....
which path I should take?
I just want a reliable way to do the work
note: using VS2010
cheers
Start with an ADO.NET tutorial: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx
ADO.NET is pretty easy to use!
Maybe later, you will want to play with Entity Framework, but I highly suggest you to start with simple things and try to understand basics first.
As all people on here will say this is down to personal preference. The modern thinking is to use an ORM wrapper such as nHibernate. Entity Framework or Subsonic (others are available). It also depends on whether you are modeling your c# code on db tables direct or you have store procedures in the way.
If you are going the simple mapping from table to object then something like the Entity Framework or Linq2Sql might be the way to go as you get tooling built into VS2010.
Hope this helps.
Most easy would probably be an ORM with Code generation (e.g. Linq2SQL or EF4) as per Pierre and WestDiscGolf
If you don't like ORMS or prefer handcrafted Stored Procedures you might also look at the Data Access Application Block (DAAB) in Enterprise Library - since you are using VS2010, EntLib 5 would make sense?
http://entlib.codeplex.com/
Related
I use ASP.NET with a informix database.
I use a set of written classes to handle the connections, the CRUD operations, transactions, etc....
Now, I feel these classes are not the best choice, less performance,take a lot of time and have many drawbacks.
I want to use an ORM, but I don't know how to choose the one which suits my web applications (ASP.NET, Informix).
Please help to select the convenient ORM.
I'm confused among nHibernate, Entity Framework, LINQ To SQL, and the Open Access (Telerik Component).
Note: I will use Visual Studio 2012 so I want the recent comparison.
I'd really suggest Entity Framework, as it is the native framework of .NET. Here it is stated that an Informix provider is available for EF.
Have you thought about using Fluent NHibernate? There's a wealth of articles on the web for it, and plenty on SO!! Here's one such article : converting to Fluent NHibernate sessionmanager.
EDIT :
Been thinking about your situation and I'll tell you how I usually think. Firstly, I'll think about exactly what it is I want my Gateway layer to do (this is the layer I use to talk to the persistance medium). Now, most will say, I want it to talk to the database or I want to insert and update stuff. But recently, I have found this isn't enough! Halfway through coding a gateway layer with these questions in mind, I suddenly realised that I wanted to do something ever so slightly different, and boom, I couldn't do it using NHibernate very easily. So, I made a few concessions and went with Linq-ToSql as it supported what was a higher priority requirement over some of the niceties of NHibernate.
Now, the reason for my tale is this : NHibernate provides some great little features like Result Transformation. I can have a view on my Db with masses of joined tables, giving an aliase for each field and with a lovely result transformer, bang, it's instantly transformed into my DTO. Now, don't get me wrong, Linq-To-Sql has a similar thing with the auto-generated classes. But I don't want these visible outside of the gateway layer (another conversation). Equally, Linq-To-Sql handles transactions with ease - something I thought NHibernate didn't do so well!
So, it all boils down to : what are my EXACT requirements within my gateway/repository layer and what technologies are compatible with my persistance medium?! And now, I can think about what technology I want to use.
I realise I may not have answered your question per se, but I hope it has given you something to think about!
Happy coding,
Cheers,
Chris.
http://www.ibm.com/developerworks/data/library/techarticle/dm-0903linqentity/
The link above will provide you with links to download the informix provider, as well as take you through a step by step process of using the informix provider in conjunction with Entity Framework.
More about Entity Framework
http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx
Simply put, once you have this set up you will be able to create classes that map to your informix data structures and perform queries using linq. EF is quite a powerful and useful tool, I recommend it.
My vote is with Fluent NHibernate - you get the configurability and cross-platform usage of Hibernate but you get to obviate the necessity of using massive XML configuration files. Entity Framework is good, but I don't like tools which rely too much on IDE/Designer support.
Happy coding,
Mel
I'd recommend Fluent NHibernate. Personally I see it used in more companies than any other ORM framework.
I've used Entity Framework, both design-first, which has serious lock-in drawbacks, and code-first, which is okay.
I posted an answer on Code Review when someone was asking an almost identical question.
i think its better to read this book first :
http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784.html
Creating My Windows Form Application and using ADO.Net as Data Access layer and
SQL server as my Back End with lots of SP's.
Do i still stick to ADO.NET or go to studying FnH or Linq to SQL? Which shall i choose? Or i still stick in ADO.NET?
Can you give me Recommended WebSites on EF or FluentNhibernate for kick of tutorials..
Thanks in Regards
It's really just up to you to pick one - they're all valid technologies.
If you're already familiar with the low-level ADO.NET constructs, and you don't feel like putting the time into learning a different methodology, you can stick with plain old ADO.NET - this is not going away anytime soon.
If you want to start off with a very simple ORM, I would suggest LINQ to SQL. However, Microsoft has basically left LINQ to SQL in the dust in favor of Entity Framework, so if your project has long-term maintenance concerns, LINQ to SQL may or may not be the best choice. It is a really nice, lightweight, easy-to-use framework though...
If you want to learn the latest MS data access technology, you could try Entity Framework. The initial setup is not too bad, but Entity Framework is a beast, so there might be a bit of a learning curve at some point, if you run into something that works differently than you expect, or you want to learn more. EF is fairly full-featured at this point, but it still lacks some of the functions offered by more mature data access technologies like NHibernate.
Finally, if you want to try something different than the Microsoft offerings, NHibernate is a great framework. You're not going to find the entity designers, property pages, wizards, hand-holding, and stuff like that, but that's almost the point of NHibnerate. In Fluent NHibernate, the primary focus can be on your domain code, and less on the database, which makes it very conducive to unit testing. Entity Framework has gotten better with persistence ignorance, but it still feels a bit heavy-weight compared to NHibernate.
In addition to these, there are several other solid data access technologies that you could look into, but I hope this gives you some info to start with.
I would like your opinions regarding "DataSet Designer" and DAL (Data Access Layer) best practices.
I use Visual Studio 2010 Framework .NEt 4.0.
For my understanding "DataSet Designer" allow me to create automatically strictly Typed-DataSet with DataTable and Adapter, this consist in DAL directly in Visual Studio 2010.
I would like to know:
- If in real scenario "DataSet Designer" is working well, or is better write Custom Business Object.
- If exist other new solution introduced in .net 4.0
Thanks for your support! :-)
I have to work with typed datasets and it is a nightmare. If you have an option never use them. Everything is better.
With the advent of the .Net 4.0 framework and the introduction of LINQ to SQL, I've been adopting a customized DAL of strictly written business objects. We experimented with Entity Framework briefly, but ultimately concluded that it is very similar to DataSets in that the auto-generated code, while handy, is just too bloated with extra junk that we ultimately didn't use.
We've found that writing LINQ into our DAL and extracting data pulls into our custom classes, we are able to streamline our data access and control the usage of the data functionally. It has been a very handy process, but it has taken a little bit for the junior developers to grip onto it.
I would suggest a ORM like Entity Framework or Nhibernate.
Data Sets smells too much to database way of thinking and I personally had a lot of problems working with them. They just get broken quite often and throw weird errors that are hard to troubleshoot.
Some other related questions you may find interesting
What are the advantages of using an ORM?
ASP.NET DataSet vs Business Objects / ORM
Use ADO.NET Entity Framework, which is where the future of Microsoft's ORM is going. Or, consider an open-source one like NHibernate...
HTH.
At my company we've been using Typed DataSets for a little while now, and have had a generally positive experience. I understand that many people don't like DataSets, and there are certainly newer data access tools out there, but since you asked about a real-world scenario, here are some of my requirements and findings:
Need to be able to read SQL Server, MS Access, and FoxPro data sources
SQL Server access is only through SPROC calls (not my choice)
Relatively easy to learn, especially to developers new to ASP.NET
I've personally explored low level ado.net access, typed datasets, linq-to-sql, and simply writing custom data access classes. I have not looked at the Entity Framework yet, as the version included in VS2008 seemed to have some mixed reviews, and I did not have access to VS2010 until just recently(I do plan to review EF sometime this year yet).
We chose to use Typed DataSets because they seemed to offer faster development against SPROCS and we found a very comprehensive tutorial by Scott Mitchell on the asp.net site: http://www.asp.net/data-access/tutorials.
As to our experience thus far, it has mostly been good. The DataSet designer generates a huge amount of code even for small number of Tables (<20). Making changes in the SPROCS has caused a few headaches, but I'd like to be shown a tool that would make this easier.
One thing you might try to make your decision easier: Come up with a small domain problem like a customer edit page or order entry page, and implement it multiple times using a variety of technologies. It takes some time to do this, but it is a good way to learn and you can compare the technologies for yourself. We did this and it seemed to help a lot.
I will personally prefer custom business objects with their flexibility but its more work. Also look at with Entity Framework and Linq To Sql. Entity Fx has got a lot more flexibility in .NET 4.0. This article should get you started on Entity Fx.
If anything I think you should look into Entity Framework. There are lots of great tutorials out there to get you started.
I personally agree with Joel Etherton, conditionally.
If you have a small enough project that even with EF's bloat you're still not looking at too much shenanigan-code, I would say the expediency it offers is worthwhile. However in larger codebases, it can become a lot to get your hands around so much bloat.
The other benefit to EF vs older style business objects which goes unmentioned though, is with EF implementation you will probably get easier upgrades to newer .NET versions taking advantage of benefits in the next .NET without having to rewrite a bunch of code by hand. (This can also be a double-edged sword as upgrading to new .NET with EF may affect the behaviour of your dal as opposed to a hand-written dal is less likely to be so affected.)
That said, I agree with Joel Etherton, write the simplest smallest dal you can implementing LINQ, the dal is always too important to make overly-complex whenever it can be avoided.
If you do not want to waste you time do not learn DataSets. Study general concepts of object-relational mapping, their pros and cons. Look at projects like Hibernate for Java or Doctrine for PHP. Approaches behind DataTables and DataSets which provide just wrapping of database objects is over. Your framework should guide you to design you domain model, not the database schema.
NHibernate. Especially if you are using Oracle.
I have a sql database and I want to create a class for each table. I think a tool exists that allows me to extract information from a sql database and transform it into classes like "DataTable" or "DataRow". Afterwards, I could use those object in dataset.
Instead of Data Table and Data sets you can use your own objects to represent data in your applications and to do so you can use some persistence frameworks and OR mappers (object relational mappers).For example you can use "Linq to Sql","Microsoft Entity framework" or NHibernate.
There are some code generation tools that let you generate code for these frameworks.
MyGeneration and CodeSmith as two of them that I know.
Maybe T4 (Text Template Transformation Toolkit) ist your friend...
There is a whole world of tools out there that do things like this. It's called ORM. Josh mentioned Subsonic, which is a great free tool. There is also the Entity Framework which is part of Visual Studio 2008 SP1. If you would like an even better tool, the one I suggest you use is LLBLGen.
Hope this helps!
I'm the creator of SqlSharpener which is a light-weight tool that will parse your *.sql files (like in an SSDT project) and create DTO objects and stored procedure wrappers at design-time using T4 templates.
How about good old Linq? Start here.
We are moving in the direction of Entity Framework, but we are taking our time because we dont find EF ready for prime time and Linq2Sql is more or less a poor mans/hobbyists ORM tool. At present we use a combination of a custom homegrown framework (The Kinetic Framework) and Fluent NHibernate.
An option if you want to write your own code generator would be to use SMO objects if you are using MS Sql Server 2005/2008 and pull the information out of the Table/Stored Procedure objects.
Came across this:
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
And wondering if this is the right solution as I am not that big of a fan of creating a class for every stored procedure or do I use Enterprise Library for ASP.net 2.0 project.
You definitely shouldn't be creating a class for every stored procedure. There are a number of approaches you can take to handling your database interactions. You should have a good look at the major frameworks out there and decide which one best suits you. The Castle Project solution is great, and relies on nHibernate (nHibernate). LINQ is a similar offering by Mircrosoft (LINQ Project). Both of these solutions are full ORM frameworks (Object Relational Mapping) and will generate dynamic SQL to persist your objects in the database. Each also has it's own quirks and likes you to structure your objects in particular ways. If you don't want to manage the SQL your system uses, I would definitely recommend one of these approaches.
I come from a database background, and prefer a bit more control over my SQL. In particular I like to have my interractions handled by stored procedures. I find this enables me to control both the SQL better for optimisation, but helps me manage database security in a more friendly manner. To accommodate this approach, I recommend something like iBatis (iBatis). iBatis isn't a full ORM, but rather a simple SQL mapper. The downside to my approach is that you need to write a lot more code (SQL), but I don't mind the trade-off.
Is there any possibility of upgrading to framework 3.5? if so take a look at LINQ to SQL and Entity Framework as this will accomplish alot of this for you.
If not then as long as it generates standard code that doesnt tie you into 3rd party libraries then you could certainly use it. At my workplace we have our own generator similar to this and it works well although we will shortly be moving to LINQ to SQL.
There are many ways of wrapping a database table in a C# class; you probably want to investigate a few alternatives before choosing between the one you've linked to and the Entity Framework.
There's a software pattern called the "active record pattern" which describes exactly this approach - one C# class for each table, with load/save methods like Customer.GetById(), Customer.Save(), and so on.
For ASP.NET 2.0, check out the Castle Project's ActiveRecord implementation and a third-party Visual Studio plugin tool called ActiveWriter that lets you generate class wrappers for your tables using a drag'n'drop interface.
You will need to determine at what point you need sets of data that are composed from your tables, and whether you want SQL to produce these with stored procedures or if your business logic layer will handle these. As Dr8k says, nHibernate will create SQL for you, but there is a learning curve with nHibernate. The ORM will be in control of how you are getting the data and depending on your environment and DBA's conmfort level you may other issues to overcome.
If you more comfortable with SQL, then there is another tool called SubSonic that will create wrappers ala Active Record for you while offering you the ability to use stored procedures as well. There is also a nice query tool with a fluent interface that you can use if you are not able to use LINQ.