ASP.NET MVC EF调用SQLServer的存储过程报错。
报错信息:从字符串转换日期/时间失败。
原文如下:
Conversion failed when converting date and/or time from character string.
我那个存储过程比较长,开始也没仔细看,拿到报错信息就百度了,结果折腾一大堆方法都不行。
我的存储过程有一个参数是传的text,然后需要在里面截取某个位置的4个字符串作为时间的年(如:2020),问题就出在这里,我传进来的text格式不对,指定位置截取的字符串跟'-03-31'拼起来无法转成时间,就报上面那个错误了。
其实报错信息已经说得很明白了,就是字符串无法转成时间,遇到报错信息类似的,先仔细看一遍存储过程,是否有字符串拼接然后转时间的,然后看注意一下字符串格式就完了。
类似这样 @Text.substring(10, 4) + '-01-31'
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE SP_UpdateEmployee
-- Add the parameters for the stored procedure here
@EmployeeId int,
@Text nvarchar(255)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
update Employee set ReleaseTime = @Text.substring(10, 4) + '-01-31' where Id = @EmployeeId
GO