Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CDate的行为有多奇怪?_C#_Sql_Database_Ms Access_Date Conversion - Fatal编程技术网

C# CDate的行为有多奇怪?

C# CDate的行为有多奇怪?,c#,sql,database,ms-access,date-conversion,C#,Sql,Database,Ms Access,Date Conversion,我发现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.md

我发现
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,则cDe>/Case>函数将其视为日期的“天”部分,而另一部分将被视为日期的“月”部分。 源代码可以从()下载

规则是什么


为什么微软没有在MSDN中解释这一点?

是的,你是对的。CDate尝试将大于12的月值视为日值

你可以试试这个

private bool IsDate(String inputDate)
{
  DateTime dt;


  Return DateTime.TryParse(inputDate,out dt);

}

也许这只是VB.NET和SQL之间的一个奇怪的巧合,但是不划界日期文字吗?因此,
CDate
与此无关;日期文字会自动更改格式。“无论如何都有效”:PAgree with@minitech如果您想要CDate,请使用字符串。此外,年、月、日也可以避免区域设置方面的问题。