I have a table as follows
| name | date | book | amnt |
|---|---|---|---|
| Madhu | 01/01/2011 | year book | 100 |
| Anoop | 01/01/2011 | Manorama | 10 |
| Madhu | 05/01/2011 | Mathrubhimi | 100 |
| Dhanya | 10/01/2011 | Poompatta | 15 |
| Madhu | 10/01/2011 | Kavitha | 50 |
| Anoop | 15/01/2011 | Yearbook | 100 |
| Madhu | 16/01/2011 | Kayar | 500 |
| Madhu | 17/01/2011 | Asp.net | 550 |
| Anoop | 18/01/2011 | C# | 800 |
| Anoop | 20/01/2011 | Sql | 450 |
In this table1 name repeating in the name column. I want to take only one name representing the same name and so on. That is the answer is "Madhu, Anoop, Dhanya". Which method I use for taking this answer. Plz give the sql code in c#
Madhukumar viswanath
VulpesPosted Aug 30, 2011, 10:32 AM
List
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
string query = "SELECT DISTINCT name FROM table1";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read()) names.Add(reader["name"].ToString());
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
string nameStr = "";
if (names.Count > 0) nameStr = String.Join(", ", names.ToArray());
MessageBox.Show(nameStr);
Madhukumar ViswanathenPosted Aug 30, 2011, 10:43 AM
Guest UserPosted Aug 30, 2011, 10:32 AM
SELECT DISTINCT [name] FROM table1
Do you need the result in CSV format?