4

I've made a local database for a C# project: This is the local database I mean.

I know basic SQL commands, but haven't worked with databases in C#. What I'd like to know specifically is:

  • How to read from the database (query)
  • How to add and update rows

The database only consists of 3 tables, so I don't think anything fancy is needed.

2
  • stackoverflow.com/questions/1121917/… Commented Oct 20, 2011 at 15:28
  • 3
    The problem with this question is that it shows little to no effort in research. There is a myriad of ways you can access db data from c#, from the good 'ol open connection-query to ORM tools like EF or Hibernate. You should first figure out which technology you want to use and then come back here if you have trouble implementing it. Commented Oct 20, 2011 at 15:30

4 Answers 4

8

First, you should learn a bit about various technologies and APIs for connecting with a database.

The more traditional method is ADO.NET, which allows you to define connections and execute SQL queries or stored procedures very easily. I recommend digging up a basic tutorial on ADO.NET using Google, which may differ depending on what type of project you're creating (web app, console, WinForms, etc).

Now days, ORMs are becoming increasingly popular. They allow you to define your object model in code (such as every database table would be a class, and columns would be properties on that class) and bind to an existing database. To add a new row to a table, you'd just create an instance of a class and call a "Save" method when you're done.

The .NET framework has LINQ to SQL and the Entity Framework for this sort of pattern, both of which have plenty of tutorials online. An open source project I really like is Castle Active Record, which is built on top of NHibernate. It makes defining ORMs quite easy.

If you have specific questions about any of the above, don't hesitate to post a new question with more specific inquiries. Good luck!

Update:

I thought I'd also put in one last reference as it seems you might be interested in working with local database stores rather than building a client/server app. SQLite allows you to interact with local stores on the file system through SQL code. There's also a .NET binding maintained by the SQLite guys (which would in theory allow you to work with the other platforms I mentioned): http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

Sign up to request clarification or add additional context in comments.

1 Comment

I second the use of Castle ActiveRecord. Have been using it for a couple of years and it allows me to develop more closely to the business domain.
2

You can use SQLCE.

This blog will give you a good start.

http://weblogs.asp.net/scottgu/archive/2011/01/11/vs-2010-sp1-and-sql-ce.aspx

Comments

0

Here is a small tutorial that should be helpful to you.

You can make use of the SqlDataReader to read data

and the SqlCommand to Insert Update Delete rows from your tables.

http://www.dotnetperls.com/sqlclient

Comments

0

You could use it by adding following to your Startup.cs

services.AddDbContext<DemoDbContext>(options => options.UseSqlite("Filename=data.db"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.