Last active
August 29, 2015 14:24
-
-
Save mattjohnsonpint/548a4308dab8e52f5097 to your computer and use it in GitHub Desktop.
Testing MySql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MySql.Data.MySqlClient; | |
using MySql.Data.Types; | |
namespace TestMySql | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var connection = new MySqlConnection("server=localhost;userid=root;password=xxxxxx;database=test;AllowZeroDateTime=True")) | |
{ | |
var sql = "SELECT * FROM testdatetime;"; | |
using (var command = new MySqlCommand(sql, connection)) | |
{ | |
connection.Open(); | |
using (var reader = command.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
MySqlDateTime mdt = (MySqlDateTime)reader["testdt"]; | |
Console.WriteLine(mdt.ToString()); | |
Console.WriteLine(mdt.ToString("M/d/yyyy HH:mm:ss")); | |
} | |
} | |
} | |
} | |
} | |
} | |
public static class Extensions | |
{ | |
public static string ToString(this MySqlDateTime mdt, string format) | |
{ | |
return mdt.ToString(format, null); | |
} | |
public static string ToString(this MySqlDateTime mdt, string format, IFormatProvider provider) | |
{ | |
return mdt.IsValidDateTime | |
? mdt.GetDateTime().ToString(format, provider) | |
: new DateTime(1, 1, 1).ToString(format, provider).Replace('1', '0'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `test`.`testdatetime` (`testdt` datetime NOT NULL) ; | |
INSERT INTO `test`.`testdatetime` (`testdt`) VALUES ('0000-00-00'); | |
INSERT INTO `test`.`testdatetime` (`testdt`) VALUES ('2015-07-14'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0/0/0000 12:00:00 AM | |
0/0/0000 00:00:00 | |
7/14/2015 12:00:00 AM | |
7/14/2015 00:00:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment