public GetDateTime ( int i ) : System.DateTime | ||
i | int | |
return | System.DateTime |
using MySql.Data.MySqlClient; string connectionString = "server=localhost;database=myDb;user=root;password=root;"; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlCommand cmd = new MySqlCommand("SELECT myDate FROM myTable", connection); using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { DateTime myDate = reader.GetDateTime(0); Console.WriteLine(myDate.ToString()); } } }
using System; DateTime maxDate = DateTime.MaxValue; DateTime minDate = DateTime.MinValue; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlCommand cmd = new MySqlCommand("SELECT MAX(myDate), MIN(myDate) FROM myTable", connection); using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { if(!reader.IsDBNull(0)) { maxDate = reader.GetDateTime(0); } if(!reader.IsDBNull(1)) { minDate = reader.GetDateTime(1); } } } } Console.WriteLine("Max date: " + maxDate.ToString()); Console.WriteLine("Min date: " + minDate.ToString());This example retrieves the maximum and minimum DateTime values from the "myTable" table in the "myDb" database and displays them in the console. It also demonstrates how to handle null values using the IsDBNull method.