我发现该CDate
函数在以下代码中有一个奇怪的行为:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" + Application.StartupPath + @"\db.mdb" + ";Persist Security Info=False");
{
OleDbDataAdapter adapterA = new OleDbDataAdapter("SELECT CDate(#01/05/2013#) AS TheDate", connection);
DataTable dataTableA = new DataTable();
adapterA.Fill(dataTableA);
DateTime dateTimeA = (DateTime)dataTableA.Rows[0]["TheDate"]; // get 1
MessageBox.Show(dateTimeA.Month.ToString());
} //these to ensure that I did not use the variables in the next block /^-^\ .
{
OleDbDataAdapter adapterB = new OleDbDataAdapter("SELECT CDate(#13/05/2013#) AS TheDate", connection);
DataTable dataTableB = new DataTable();
adapterB.Fill(dataTableB);
DateTime dateTimeB = (DateTime)dataTableB.Rows[0]["TheDate"]; // get 5
MessageBox.Show(dateTimeB.Month.ToString());
}
}
我知道如果该值大于 12,该CDate
函数会将其视为日期的“日”部分,而另一部分将被视为日期的“月”部分。
源代码可以从(链接)下载。
这是什么规则?
为什么微软没有在 MSDN 中解释这一点?