简体   繁体   English

日期时间字符串转换C#SQL输出参数

[英]Datetime String conversion c# sql output parameters

I have a sql procedure which is outputting the following @startdate as a datetime output. 我有一个SQL过程,它将以下@startdate输出为datetime输出。 I am trying to get these values into my C# aspx.cs code. 我试图将这些值添加到我的C#aspx.cs代码中。

C# Output parameters C#输出参数

 cmd5.Parameters.Add(new SqlParameter("@startdate", SqlDbType.DateTime, 100, ParameterDirection.Output, false, 0, 10, "startdate", DataRowVersion.Default, null));

I have a public variable called startdate and trying to assign the above output parameter to the variable as seen below: 我有一个名为startdate的公共变量,并尝试将上述输出参数分配给该变量,如下所示:

startdate = DateTime.Parse(cmd5.Parameters["@startdate"].Value.ToString()); //output is {01/12/2017 00:00:00} (correct date coming from sql, wrong format)

I need to parse this variable into another procedure from the front end. 我需要将此变量从前端解析为另一个过程。 This procedure expects nvarchar(35) in sql format: yyyy-MM-dd hh:MM:ss 此过程需要sql格式的nvarchar(35):yyyy-MM-dd hh:MM:ss

How do i parse the above startdate global variable to the sql format in nvarchar(35) from here? 我如何从这里将上述startdate全局变量解析为nvarchar(35)中的sql格式?

Try with 试试看
DateTime.TryParseExact(


In TryParseExact method you need to pass a string and expected format. TryParseExact方法中,您需要传递字符串和期望的格式。

you can find more samples and description here https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=netframework-4.7.2 您可以在此处找到更多示例和说明https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.tryparseexact?view=netframework-4.7.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM