How to work with the database in C #?

For example, how to make one connection to the database, and then receive information from it? How to query tables? How to handle query results? How to display tables from DB in DataGridView ? And so on...

There are a lot of different information on the Internet, but I really don't understand anything. Explain on fingers what and how to do.

Closed due to the fact that the question is too general a participant PashaPash 8 Oct '16 at 9:27 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

    4 answers 4

    I advise you to read for example Working with databases in C #. Technology ADO .NET: Tutorial

    as well as an introduction to working with databases

    If you are interested in the criterion for choosing a technology, can you find out which database to choose? The shortest FAQ , the topic is old, but at least you will have an idea, I would recommend ADO.NET from myself

    Examples of various connections, etc. ADO.Net

      Look towards LINQ .

      • one
        If you need to work with a DataGridView, it is better to look towards the classic ADO.Net, although I agree with the use of LINQ. - Specter

      I will give an example of a program I once wrote for the examination of exam tickets from the database:

       using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Data.OleDb;//дата провайдер для работы с необходимой нам базой namespace MdbToTxt { class Program { static void Main(string[] args) { string[] questions = new string[100]; using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=I:\экз\db1.mdb"))//это строка соединения с БД { OleDbCommand command = new OleDbCommand("SELECT * FROM Билеты ORDER BY [Номер билета]", conn);//Создаём SQL-запрос conn.Open();//открываем соединение, должно закрыться само, но хз OleDbDataReader reader = command.ExecuteReader();//Выполняем запрос, в данном случе на чтение questions[0] = reader.GetName(0) + "\t" + reader.GetName(1) + "\t" + reader.GetName(2);//здесь будут хранится именя полей таблицы int i = 1; while(reader.Read())//а здесь собственно записи полей { questions[i] = reader[0] + "\t\t" + reader[1] + "\t\t" + reader[2];// записи я записывал в строковый массив, хотя с ними можно выполнять всё, что душе угодно i++; } } File.WriteAllLines("Questions.txt", questions); } } } 

      want to know more => Working with Connected ADO .NET Level in C #

        If you use Windows Application, you can first use the basic functionality (from the Data menu). You can start learning from this.