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);
SqlDataReader reader = command.ExecuteReader();
string countryName = reader.GetString(0);
// 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!!