Returning the Country Names with no Repeats

Returning the Country Names with no Repeats

To return the country names with no repeats from the Customers table in the Northwind database, you can use the DISTINCT keyword in your SQL query. Here’s an example query:

SELECT DISTINCT Country FROM Customers ORDER BY Country

This query retrieves all distinct values of the “Country” column from the “Customers” table and orders them alphabetically by country name.

In ASP.NET, you can execute this query using a SqlCommand object and read the results using a SqlDataReader object. Here’s an example:

string connectionString = ConfigurationManager.ConnectionStrings[“NorthwindConnectionString”].ConnectionString;

string query = “SELECT DISTINCT Country FROM Customers ORDER BY Country”;

List<string> countryNames = new List<string>();

using (SqlConnection connection = new SqlConnection(connectionString))

{

    SqlCommand command = new SqlCommand(query, connection);

    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())

    {

        string countryName = reader.GetString(0);

        countryNames.Add(countryName);

    }

    reader.Close();

}

// Do something with the list of country names, such as binding it to a control

In this example, the query is executed using a SqlCommand object and the SqlDataReader object is used to read the results. The values of the “Country” column are added to a list of strings using the GetString method of the SqlDataReader object. Finally, the list of country names can be used in your application, such as by binding it to a control.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Share this post
[social_warfare]
Deleting a file in PHP
Investment Process

Get industry recognized certification – Contact us

keyboard_arrow_up