Reputation: 89
So let's say I have this select : SELECT TITLES FROM DOCUMENTS WHERE AC_DATE = GETDATE()
In my .cs code it will be like this
con.Open();
string query = "SELECT TITLES FROM DOCUMENTS WHERE AC_DATE = GETDATE()"
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader sdr = cmd.ExecuteReader();
It will return some Titles from my table. I need to insert all these records that came from my select on mailMessage.Body = "";
To send to some people via Email, but how would I do that?
Upvotes: 1
Views: 163
Reputation: 35554
You can loop the results in a SqlDataReader and add the titles to a string.
//create a stringbuilder
StringBuilder sb = new StringBuilder();
//create a connection to the database
using (SqlConnection connection = new SqlConnection(Common.connectionString))
using (SqlCommand command = new SqlCommand("SELECT TITLES FROM DOCUMENTS WHERE AC_DATE = GETDATE()", connection))
{
//open the connection
connection.Open();
//create a reader
SqlDataReader reader = command.ExecuteReader();
//loop all the rows in the database
while (reader.Read())
{
//add the title to the stringbuilder
sb.AppendLine(reader["TITLES"].ToString());
}
}
//create the mail message with the titles
mailMessage.Body = sb.ToString();
Upvotes: 2