【发布时间】:2015-05-19 12:06:58
【问题描述】:
我需要获取员工的开始日期。我想要做的是当一个复选框被选中时调用数据库获取日期并将其返回到我的 C# 代码中。我认为我的所有设置都接近完美,但我的调用过程返回错误。有人可以帮助我设置语法,以便在选中复选框时调用数据库,运行存储过程,并将存储过程中的结果(作为日期时间)返回到我的 C# 语法中?
EDIT --- 抛出的错误是: 无法将类型“System.DateTime”隐式转换为“字符串”
//Calling Procedure
this.txtStartDate.Text = getHireDate(Constants.Database, employeename);
//Actual Procedure
private static string getHireDate(string Database, string employeename)
{
SqlConnection connection = new SqlConnection(Database);
SqlCommand command = new SqlCommand("_uspGetHireDate", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
//This line throws an error of can not implicitly convert 'System.DateTime' to 'string'
return Convert.ToDateTime(returnValue.Value);
}
//Stored Procedure Being Called
alter procedure [dbo].[_uspGetHireDate]
(
@employeename varchar(100)
)
as
declare @StartDate datetime
set NOCOUNT ON
Set @StartDate = (SELECT CONVERT(VARCHAR(10), HireDate, 101)
FROM tbl_employeeinformation
where hiredate is not null
AND active = 1
AND employeename = @employeename
return @StartDate
【问题讨论】:
-
你为什么要把
HireDate(我假设是datetime列)转换成varchar(10),然后再设置成datetime变量?? -
您已将参数定义为 Int
SqlParameter("returnVal", SqlDbType.Int)。但是您的 SP 正在返回字符串SELECT CONVERT(VARCHAR(10), HireDate, 101) -
@aSharma - 实际上,它返回一个
datetime但您对SqlParameter类型的观察是正确的
标签: c# asp.net sql-server-2008 stored-procedures