Connecting to SQL Server with a username and password in c#- Tutorial on C# language
Автор: ادد المطيري
Загружено: 2023-01-01
Просмотров: 1206
To access the full course in order, click on the link below:-
• how to select file in c# - C# Desktop appl...
How to Connect Sql Server DataBase Using C#
Connection string using Windows
How to connect to sql-server with windows
Connection String in c#
How to Connect to MSSQL SERVER in C# | C# SQL SERVER
SQL connection string C#
.Net framework connection string
Connection string SQL Server
MySQL connection string
SqlClient Connection Strings
Connect C# with SQL Server
SQL connection C# code project
SQL Server connection string c
Get data from SQL Server C#
C# with SQL Server tutorial
SQL Server connection string
How to connect to a SQL database in C#?
How to pass SQL connection string in C#?
How to connect to SQL Server in Visual Studio C#?
How to connect to database in Access C#?
How to connect C# to Database
Let’s now look at the code, which needs to be kept in place to create a connection to a database. In our example, we will connect to a database which has the name of Demodb. The credentials used to connect to the database are given below
Username – sa
Password – demo123
We will see a simple Windows forms application to work with databases. We will have a simple button called “Connect” which will be used to connect to the database.
So let’s follow the below steps to achieve this
Step 1) The first step involves the creation of a new project in Visual Studio. After launching Visual Studio, you need to choose the menu option New Project.
Step 2) The next step is to choose the project type as a Windows Forms application. Here, we also need to mention the name and location of our project.
In the project dialog box, we can see various options for creating different types of projects in Visual Studio. Click the Windows option on the left-hand side.
When we click the Windows options in the previous step, we will be able to see an option for Windows Forms Application. Click this option.
We then give a name for the application which in our case is “DemoApplication”. We also need to provide a location to store our application.
Finally, we click the ‘OK’ button to let Visual Studio to create our project.
Step 3) Now add a button from the toolbox to the Windows form. Put the text property of the Button as Connect. This is how it will look like
C# Access Database
Step 4) Now double click the form so that an event handler is added to the code for the button click event. In the event handler, add the below code.
Code Explanation:-
The first step is to create variables, which will be used to create the connection string and the connection to the SQL Server database.
The next step is to create the connection string. The connecting string needs to be specified correctly for C# to understand the connection string. The connection string consists of the following parts
Data Source – This is the name of the server on which the database resides. In our case, it resides on a machine called WIN- 50GP30FGO75.
The Initial Catalog is used to specify the name of the database
The UserID and Password are the credentials required to connect to the database.
Next, we assign the connecting string to the variable cnn. The variable cnn, which is of type SqlConnection is used to establish the connection to the database.
Next, we use the Open method of the cnn variable to open a connection to the database. We then just display a message to the user that the connection is established.
Once the operation is completed successfully, we then close the connection to the database. It is always a good practice to close the connection to the database if nothing else is required to be done on the database.
When the above code is set, and the project is executed using Visual Studio, you will get the below output. Once the form is displayed, click the Connect button.
source:-
https://www.guru99.com/c-sharp-access...
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: