dir.by  
  Search  
Programming, development, testing
ADO.NET (working with the database on C#)
Let's create a ADO.NET console application (C#, database)
  Looked at 7852 times    
 Let's create a ADO.NET console application (C#, database) 
last updated: 22 July 2025
ADO.NET is a database technique using C# code.
Plan (4 steps)
Step 1. Populate the Microsoft SQL Server database
Step 2. Creating a new console application in C#
If you do not have Visual Studio installed you need install Visual Studio...
Open Visual Studio 2022
or
Open Visual Studio 2019
Step 3. Let's add the library Microsoft.Data.SqlClient
Note! I'm adding Microsoft.Data.SqlClient versions of 5.0.0 because it's compatible with .Net 5, .Net 6, .Net 7, .Net 8
Step 4. Create a connection to the Microsoft SQL Server database
 Database connection string | ADO.NET, C# 
To make a connection string, do the following:
In Visual Studio click ToolsConnect to Database... in the menu
In the "Add Connection" window, select
Server name:
Select the computer where the server is located for the database.
The server for the database is the program SQl Server
1) can be selected from the list [v]
2) You can write local, which means we will use SQL Server on your computer (locally).
Select or enter a database name:
Choose a database name
1) can be selected from the list [v]
Click Test Connection to see if there is a connection to the database (i.e. if we have specified server name and database name correctly)
Press the "Advanced..." button
Let's copy the text:
  C#     In the C# code, we will use it to connect to the database
string connectionString = @"Data Source=localhost;Initial Catalog=MyDatabase1;Integrated Security=True;Trust Server Certificate=True";
Step 5. Write C# code in the console application
This code, when executed, makes a connection to the database
  C#  
using Microsoft.Data.SqlClient;

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

               // connect to db
               using (SqlConnection connection = new SqlConnection(connectionString))
               {
                    // open db
                    connection.Open();
               }
          }
     }
}
 
← Previous topic
What is ADO.NET?
 
Next topic →
Read the data (select) from the Database using SqlCommand and SqlDataReader in ADO.NET C#. Database Type Mapping
 
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  
Яндекс.Метрика