Javatpoint Logo
Javatpoint Logo

ADO.NET SQL Server Connection

To connect with SQL Server, we must have it installed in our system. We are using Microsoft SQL Server Management Tool to connect with the SQL Server. We can use this tool to handle database. Now, follow the following steps to connect with SQL Server.

  1. Open Microsoft SQL Server Management Tool
  2. It will prompt for database connection. Provide the server name and authentication.

    ADO Net SQL Server Connection 1

    After successful connection, it displays the following window.

    ADO Net SQL Server Connection 2
  3. Creating Database
  4. Now, create database by selecting database option then right click on it. It pops up an option menu and provides couple of options.

    ADO Net SQL Server Connection 3

    Click on the New Database then it will ask for the database name. Here, we have created a Student database.

    ADO Net SQL Server Connection 4

    Click on the Ok button then it will create a database that we can see in the left window of the below screenshot.

    ADO Net SQL Server Connection 5
  5. Establish connection and create a table
  6. After creating database, now, let's create a table by using the following C# code. In this source code, we are using created student database to connect.

    In visual studio 2017, we created a .NET console application project that contains the following C# code.

    // Program.cs

    Execute this code using Ctrl+F5. After executing, it displays a message to the console as below.

    ADO Net SQL Server Connection 6

    We can see the created table in Microsoft SQL Server Management Studio also. It shows the created table as shown below.

    ADO Net SQL Server Connection 7

    See, we have a table here. Initially, this table is empty so we need to insert data into it.

  7. Insert Data into the Table
  8. // Program.cs

    Execute this code by using Ctrl+F5 and it will display the following output.

    ADO Net SQL Server Connection 8
  9. Retrieve Record
  10. Here, we will retrieve the inserted data. Look at the following C# code.

    // Program.cs

    Execute this code by Ctrl+F5 and it will produce the following result. This displays two records, one we inserted manually.

    Output:

    ADO Net SQL Server Connection 9
  11. Deleting Record

This time student table contains two records. The following C# code delete one row from the table.

// Program.cs

Output:

It displays the following output.

ADO Net SQL Server Connection 10

We can verify it by retrieving data back by using SqlDataReader.


Next TopicADO.NET Connection




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA