C# Sql Select Distinct Tutorial MS SQL Server
In this C# tutorial, we will select distinct records of users table from MS SQL Server using C# code sample below.
C# SQL Select Distinct Code
using System.Data.SqlClient;
using System.Data;
public DataSet GetUserCount()
{
string constr =
"Provider=sqloledb;Data Source=SqlServerIPAddress;Initial " +
"Catalog=DataBaseName;User Id=UserName;Password=UserPassword;";
string str_select = "SELECT DISTINCT Name FROM tbl_users";
SqlConnection con = new SqlConnection(constr);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(str_select, con);
da.Fill(ds);
return ds;
}
Note that you can also use this sql distinct code sample in ASP.NET.