获取服务器时间(存储过程)
CREATE PROCEDURE LR_GetClientTime_Now
@ServerTimeNow varchar(30) out
AS
SELECT @ServerTimeNow= getdate()
GO
==============================================
这样写对不对?怎么转换为程序中的时间为string类型
public string GetServerTime()
{
SqlConnection connec=new SqlConnection (SqlHelper.ConnOnline);
SqlCommand comm = new SqlCommand();
SqlParameter parameter = new SqlParameter( "@ServerTimeNow ",SqlDbType.NVarChar);
parameter.Direction = ParameterDirection.Output;
comm.Connection = connec;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "LR_GetServerTime_Now "; //存储过程名
comm.ExecuteNonQuery();
return parameter.Value.ToString(); //这样获取值对不对?
}
不知道哪里出错了
==================================================
servertime = cus.GetServerTime().ToString( "yyyy-MM-dd HH:mm ");//需要将时间转换为这样的格式
====================================================
获取OUTPUT类型参数的代码还不太清楚,麻烦大虾给的解释详细点
[解决办法]
存储过程里的@ServerTimeNow 定义为DateTime output
[解决办法]
CREATE PROCEDURE LR_GetClientTime_Now
@ServerTimeNow varchar(30) out
AS
SELECT @ServerTimeNow = Convert(varchar(20), getdate())
GO
[解决办法]
SqlParameter parameter = new SqlParameter( "@ServerTimeNow ",SqlDbType.DateTime);
[解决办法]
楼上正解
[解决办法]
设置参数为output
[解决办法]
set @ServerTimeNow = cast(getdate() as varchar(20))
[解决办法]
SELECT @ServerTimeNow= convert(nvarchar(10),getdate(),102)
[解决办法]
CONVERT(CHAR,getdate() ,111)
[解决办法]
真晕了,弄个服务器时间有这么麻烦,还得用存储过程??别以为用存储过程提高效率,有的情况下他们效率很底的
[解决办法]
CREATE PROCEDURE LR_GetClientTime_Now
@ServerTimeNow varchar(30) out
AS
SELECT @ServerTimeNow= CONVERT(CHAR,getdate() ,111)--此处的111可换做其他
GO
[解决办法]
SqlParameter parameter = new SqlParameter( "@ServerTimeNow ",SqlDbType.DateTime);
[解决办法]
CREATE PROCEDURE LR_GetClientTime_Now
@ServerTimeNow varchar(30) out
AS
SELECT @ServerTimeNow = Convert(varchar(20), getdate())
GO
[解决办法]
CREATE PROCEDURE LR_GetClientTime_Now
@ServerTimeNow datetime output
AS
SET @ServerTimeNow= getdate()
GO
==============================================
public string GetServerTime()
{
SqlConnection connec=new SqlConnection (SqlHelper.ConnOnline);
SqlCommand comm = new SqlCommand();
SqlParameter parameter = new SqlParameter( "@ServerTimeNow ",SqlDbType.DateTime);
parameter.Direction = ParameterDirection.Output;
comm.Connection = connec;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "LR_GetServerTime_Now "; //存储过程名
comm.ExecuteNonQuery();
return parameter.Value.ToString(); //这样获取值对不对?
}