尝试从C#Winforms项目执行以下过程时,我收到以下错误:
从字符串
转换日期和/或时间时转换失败
这是我的存储过程:
CREATE PROCEDURE DisplayTime
@name VARCHAR(50),
@fromdate date,
@todate date
AS
BEGIN
DECLARE @VTIME TIME(7)
SELECT
@VTIME = CAST(SUM(DATEDIFF(SECOND, 0, time)) /60/60 AS TIME)
+':'+CAST(SUM(DATEDIFF(SECOND, 0, time)) /60%60 AS TIME)
+':'+ CAST(SUM(DATEDIFF(SECOND, 0, time)) % 60 AS TIME)
FROM
(SELECT time
FROM time
WHERE [user] = '@name'
AND date BETWEEN '@fromdate' AND '@todate') AS T
SELECT @VTIME AS count_down_sec
END
这是我的C#代码:
private void button1_Click(object sender, EventArgs e)
{
if (txt_UserName.Text != "")
{
con.Open();
cmd = new SqlCommand("DisplayTime",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", txt_UserName.Text);
cmd.Parameters.AddWithValue("@fromdate",dateTimePicker1.Value.Date);
cmd.Parameters.AddWithValue("@todate",dateTimePicker2.Value.Date);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(txtTotal.Text);
}
}
答案 0 :(得分:0)
不确定您在此过程中尝试执行的操作。但是,除非您使用,否则参数名称不必用单引号括起来 Execute()。删除@ name,@ fromdate和@todate
周围的引号此外,您需要提供正确的命名,以便其他开发人员不会与数据类型名称和列/表名称混淆。
CREATE PROCEDURE DisplayTime
@name VARCHAR(50),
@fromdate date,
@todate date
AS
BEGIN
DECLARE @VTIME TIME(7)
SELECT
@VTIME = CAST(SUM(DATEDIFF(SECOND, 0, time)) /60/60 AS TIME)
+':'+CAST(SUM(DATEDIFF(SECOND, 0, time)) /60%60 AS TIME)
+':'+ CAST(SUM(DATEDIFF(SECOND, 0, time)) % 60 AS TIME)
FROM
(SELECT time
FROM time
WHERE [user] = @name
AND date BETWEEN @fromdate AND @todate) AS T
SELECT @VTIME AS count_down_sec
END