I have designed a Form in VS 2008. I have a database table containing the following fields:
Fname (char)
MailFrom (char)
MailTo (char)
Subject (char)
Body (char)
MailID (int)
Now I to extract the data from the database and display it on the form in their respective fields.
my code behind is:
SqlConnection conn = new SqlConnection(
"Data Source=PTZ1\\SQLEXPRESS;Initial Catalog = test; Integrated Security=SSPI;User ID=sa; Password=sa@; Trusted_Connection=True;");
SqlDataReader rdr = null;
try
{
// Open the connection
conn.Open();
// Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from testing", conn);
//
// Use the connection
//
// get query results
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// Close the connection
if (conn != null)
{
conn.Close();
}
}
How do I store and display the data on the console
You can store the result of the query in memory using
DataTablefor the storage and displaying of that data onConsole:OR
Definite your custom class, e.g. ‘Email` this way:
Read the values in your custom class collection: