dir.by  
  Search  
Programming, development, testing
ADO.NET (working with the database on C#)
Read (select) data from the Database using SqlDataAdapter and DataSet | ADO.NET, C#
  Looked at 5993 times    
 Read (select) data from the Database using SqlDataAdapter and DataSet | ADO.NET, C# 
last updated: 22 July 2025
The SqlDataAdapter class with the SQL query Select reads the data from the Database and puts it in DataSet.
Step 1. Creating a new application
Step 2. Let's write the code
  C#     В файле Program.cs напишем код:
using System;

// подключаем SQL ADO.NET
using Microsoft.Data.SqlClient;

// подключаем DataSet
using System.Data;

namespace ConsoleApplication1
{
     class Program
     {
          static void Main(string[] args)
          {
               // connection string
               string connectionString = @"Data Source=EVGENI;Initial Catalog=MyDatabase1;Integrated Security=True";

               // connect to db
               using (SqlConnection connection = new SqlConnection(connectionString))
               {
                    // open db
                    connection.Open();

                    // получаем данные из таблицы
                    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Books", connection);
                    DataSet dataset = new DataSet();
                    int rows = adapter.Fill(dataset, "Books");

                    // построчно считываем данные
                    foreach (DataRow row in dataset.Tables["Books"].Rows)
                    {
                         int id = (int)row["Id"];
                         string name = (string)row["Name"];
                         int price = (int)row["Price"];

                         // выводим на экран
                         Console.WriteLine("Id={0}, Name={1}, Price={2}", id, name, price);
                    }
               }
          }
     }
}
Example result
 
← Previous topic
Read the data (select) from the Database using SqlCommand and SqlDataReader in ADO.NET C#. Database Type Mapping
 
Next topic →
Get a scalar (single) value: Count, Min, Max, Sum, etc. | ADO.NET, C#
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is ADO.NET?
Let's create a ADO.NET console application (C#, database)
Working with data in ADO.NET
Read the data (select) from the Database using SqlCommand and SqlDataReader in ADO.NET C#. Database Type Mapping
Read (select) data from the Database using SqlDataAdapter and DataSet | ADO.NET, C#
Get a scalar (single) value: Count, Min, Max, Sum, etc. | ADO.NET, C#
Add data (insert into) to the Database using SqlCommand | ADO.NET, C#
Change the data (update) in the Database using SqlCommand | ADO.NET, C#
Delete data in the Database using SqlCommand | ADO.NET, C#
Stored procedures in ADO.NET
Stored procedures in ADO.NET | C#
Transactions in ADO.NET
Transactions in ADO.NET | Database in the C# console application
Reading the picture from the database ADO.NET
Read the image from the database and save it to a file | ADO.NET, C#
Additional topics, questions
Database connection string | ADO.NET, C#

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика