Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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# 如何将SQL日期转换为日期时间?_C#_Asp.net_Sql Server 2008_Date_Datetime - Fatal编程技术网

C# 如何将SQL日期转换为日期时间?

C# 如何将SQL日期转换为日期时间?,c#,asp.net,sql-server-2008,date,datetime,C#,Asp.net,Sql Server 2008,Date,Datetime,我的SQL数据库中有一个日期类型为的列。如何将其转换为C日期时间,然后再转换回SQL日期?C 从读者那里得到日期 DateTime date1; DateTime.TryParse(reader["datecolumn"], out date1); Dim date1 as Date=CType(reader("dateColumn"),Date) 插入日期 string date1="2013-12-12"; DateTime date2; DateTime.TryParse(reade

我的SQL数据库中有一个日期类型为的列。如何将其转换为C日期时间,然后再转换回SQL日期?

C 从读者那里得到日期

DateTime date1;
DateTime.TryParse(reader["datecolumn"], out date1);
Dim date1 as Date=CType(reader("dateColumn"),Date)
插入日期

string date1="2013-12-12"; 
DateTime date2;
DateTime.TryParse(reader["datecolumn"],out date2);

SqlCommand cmd= new SqlCommand("Insert into table (dateColumn) Values(@date2)",connection);
cmd.Parameters.AddWithValue("@date2",date2.Date);
 Dim date1 as String="2013-12-12" 'you can get this date from an html input of type date

 Dim cmd As New SqlCommand("Insert into table (dateColumn) Values(@date1)",connection)
 cmd.Parameters.AddWithValue("@date1",CType(date1, Date))
TryParse在成功施放false时返回true,否则返回false

VB

从读者那里得到日期

DateTime date1;
DateTime.TryParse(reader["datecolumn"], out date1);
Dim date1 as Date=CType(reader("dateColumn"),Date)
插入日期

string date1="2013-12-12"; 
DateTime date2;
DateTime.TryParse(reader["datecolumn"],out date2);

SqlCommand cmd= new SqlCommand("Insert into table (dateColumn) Values(@date2)",connection);
cmd.Parameters.AddWithValue("@date2",date2.Date);
 Dim date1 as String="2013-12-12" 'you can get this date from an html input of type date

 Dim cmd As New SqlCommand("Insert into table (dateColumn) Values(@date1)",connection)
 cmd.Parameters.AddWithValue("@date1",CType(date1, Date))
注意:代码是用VB编写的。您可以轻松地编写上述代码的c等价物。函数名在VB和c中保持不变。此外,CType在CType中不可用,尽管它与显式强制转换变量相同,例如date1=DateTimereaderdateColumn;我建议使用TryParse,它不会对不成功的解析/转换抛出任何异常

语法

Date.TryParse(reader("dateColumn"), date1)
sql日期可以直接转换为.net日期时间,反之亦然

要获得它,请使用

要设置它,只需将其指定给的值,并使用DateTime的.Date属性

 string mydate= Convert.ToDateTime(reader["dateColumn"]).ToShortDateString().ToString());

这些代码对我很有用。试试这些

你是如何检索记录的?我想使用类似“从新闻中选择*的内容,其中添加_date<@selected_date”并使用SqlDataReader@newStackExchangeInstance在投票否决之前你读过我的答案吗??你知道VB和C是什么吗?它们之间几乎没有什么区别吗?@thunderbird Macintosh苹果不是Granny Smith苹果,尽管它们都是苹果。如果你想要一个,而别人又给了你一个,你可能会很恼火,或者至少会感到困惑/惊讶。@哎呀,我已经给出了如何将它转换为c的说明。此外,VB和C类似于爪哇和C++的编码。如果你知道一个,你就知道另一个。无论如何,我会把它翻译成c-1:你应该提供c示例。如果你觉得vb.net和c是如此相似,为什么你不麻烦提供一个c示例呢?@thunderbird我是这么认为的,我只是认为如果OP要求c,他应该得到CThx。当msdn说没有进行转换时,我感到困惑;因此,检索到的数据必须已经是DateTime对象。