简体   繁体   English

ASP.NET无法将参数值从字符串转换为DateTime

[英]ASP.NET Failed to convert parameter value from a String to a DateTime

I am inserting data into a database stored procedure like so: 我将数据插入数据库存储过程中,如下所示:

SqlParameter parameter2 = new SqlParameter("@actualFinish", SqlDbType.DateTime);
                            parameter2.Value = sortedCells[i].finishedDate;
                            parameter2.Direction = ParameterDirection.Input;
                            command.Parameters.Add(parameter2);

My issue I am having is when the I try to insert an empty date "" I get this error: 我遇到的问题是,当我尝试插入空日期""此错误:

Failed to convert parameter value from a String to a DateTime

the column I am inserting into can allow NULLs....so how would I say if this is "" then give it a NULL 我要插入的列可以允许NULL ....所以我怎么说如果这是""然后给它一个NULL

You may want to consider explicitly parsing your date prior to passing it in as a parameter and do a check to see if it contains a value to determine if you should pass the DateTime object or DBNull.Value : 您可能需要考虑在将日期作为参数传递之前进行显式解析,并进行检查以查看其是否包含值,以确定是否应传递DateTime对象或DBNull.Value

DateTime finishDate = DateTime.MinValue;
// This will attempt to parse the value if possible
DateTime.TryParse(sortedCells[i].finishedDate, out finishDate);

// Build your parameter here
SqlParameter parameter2 = new SqlParameter("@actualFinish", SqlDbType.DateTime); 
parameter2.Direction = ParameterDirection.Input;
// If you are using a nullable field, you may want to explicitly indicate that
parameter2.IsNullable = true;

// Then when setting the value, check if you should use the value or null
if(finishDate == DateTime.MinValue)
{
     parameter2.Value = DBNull.Value;
}
else
{
     parameter2.Value = finishDate;
}

// Finally add your parameter
command.Parameters.Add(parameter2);

Check for an empty string and substitute DBNull.Value. 检查一个空字符串并替换DBNull.Value。

parameter2.Value = string.IsNullOrWhiteSpace(sortedCells[i].finishedDate) 
    ? DBNull.Value 
    : sortedCells[i].finishedDate;

Alternatively, give the parameter a default value of NULL in the stored procedure definition, then only set the value when it's not empty. 或者,在存储过程定义中为参数指定默认值NULL,然后仅在不为空时设置该值。

For the stored procedure: 对于存储过程:

CREATE PROCEDURE [YourSchema].[YourProcedure]
...snip...
@actualFinish DATETIME = NULL
...snip...

Then: 然后:

if(!String.IsNullOrWhiteSpace(sortedCells[i].finishedDate)
    parameter2.Value = sortedCells[i].finishedDate;

Editing to add: 编辑添加:

Using a nullable DateTime would be clearer, I think, then letting the implicit string conversion do its thing. 我认为,使用可为空的DateTime会更清楚,然后让隐式字符串转换完成它的工作。

DateTime? finishedDate = null;
if(!String.IsNullOrWhiteSpace(sortedCells[i].finishedDate))
        finishedDate = DateTime.Parse(sortedCells[i].finishedDate);

Then finishedDate is always your parameter value. 然后,finishDate始终是您的参数值。 You could be even safer by using TryParse and informing the user when the date fails parsing that way. 通过使用TryParse并在日期解析失败时通知用户,您甚至可以更加安全。 TryParse doesn't take nullable types, though, so you could use two variables or you could use a regular DateTime variable and use DateTime.Min as a sentinel value for "no date" and use the ternary operator to switch that to null instead. 但是,TryParse不采用可为空的类型,因此您可以使用两个变量,也可以使用常规的DateTime变量,并将DateTime.Min用作“无日期”的前哨值,然后使用三元运算符将其切换为null。 Lots of ways to skin this cat. 这只猫有很多种皮的方法。

You should always convert it to the correct type before you pass the value. 在传递值之前,应始终将其转换为正确的类型。

var actualFinishParameter = new SqlParameter("@actualFinish", SqlDbType.DateTime);
Object actualFinish = DBNull.Value;
DateTime finishDate;
if(DateTime.TryParse(sortedCells[i].finishedDate, out finishDate))
    actualFinish = finishDate;
actualFinishParameter.Value = actualFinish;
command.Parameters.Add(actualFinishParameter);

In the appropriate table, Add NULL to the appropriate date column. 在适当的表中,将NULL添加到适当的日期列。

[FinishedDate] [datetime] NULL. [FinishedDate] [datetime] NULL。

Hope this will work. 希望这会起作用。

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

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