我有一个秒表和一个数据库。我希望能够报告in total on a given date
在整个数据库中记录了多少时间。
x日期由datetimepicker指定。
当我在C#程序中单击相对按钮以汇总“ SumOnDateReport”存储过程的时间时,出现此错误:
其他信息:子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
我如何做到这一点:
您所需的信息:
我的表格(带有预先输入的数据):
id TimeSpanColumn TimeStampColumn TimeStringColumn TotalSecColumn
------------------------------------------------------------------
1 23:12:50 2011/07/12 12:00:00 NULL NULL
2 2:07:24 2012/03/09 12:00:00 NULL NULL
3 54:17:52 2013/03/09 12:00:00 NULL NULL
我的addtime C#代码(通过存储过程):
DateTime dateTime = DateTime.Today;
timer1.Stop();
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("AddTime", sqlcon);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@QTimeSpan", label1.Text + ":" + label3.Text + ":" + label5.Text);
sqlcmd.Parameters.AddWithValue("@QTimeStamp", dateTime);
sqlcmd.Parameters.AddWithValue("@QTimeString", label1.Text + label3.Text + label5.Text);
sqlcmd.ExecuteNonQuery();
SqlDataAdapter sqlda = new SqlDataAdapter("GetSavedInfo", sqlcon);
DataTable dt = new DataTable();
sqlda.Fill(dt);
dataGridView1.DataSource = dt;
sqlcon.Close();
我的addTime存储过程:
CREATE PROCEDURE AddTime
@QTimeSpan NVARCHAR(50),
@QTimeStamp DATETIME,
@QTimeString NVARCHAR(50)
AS
INSERT INTO InfoTable (TimeSpanColumn, TimeStampColumn, TimeStringColumn)
VALUES (@QTimeSpan, @QTimeStamp, @QTimeString)
RETURN
关于日期C#代码的报告:
SqlDataAdapter sqlda = new SqlDataAdapter("SumOnDateReport", sqlcon);
sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlda.SelectCommand.Parameters.AddWithValue("@QueryDate", dateTimePicker1.Value.ToString("MM/dd/yyyy"));
DataTable dt = new DataTable();
sqlda.Fill(dt);
dataGridView2.DataSource = dt;
我的SumOnDateReport存储过程:
CREATE PROCEDURE SumOnDateReport
@QueryDate DATETIME
AS
DECLARE @total_sec INT
DECLARE @HourTime INT
DECLARE @MinTime INT
DECLARE @SecTime INT
SET @HourTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 1, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @MinTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 3, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @SecTime = (SELECT CONVERT(INT, SUBSTRING(TimeStringColumn, 5, 2))
FROM InfoTable
WHERE TimeStampColumn = @QueryDate)
SET @total_sec = @HourTime * 3600 + @MinTime * 60 + @SecTime
INSERT INTO InfoTable(TotalSecColumn)
VALUES (@total_sec)
SELECT TotalSecColumn
FROM InfoTable
WHERE TimeStampColumn = @QueryDate
答案 0 :(得分:0)
如果InfoTable
中有多个记录,而TimeStampColumn
中的值与@QueryDate
的值匹配,则会出现此错误。您将需要更改这些子查询,以确保它们返回单个记录。在这种情况下,我认为您想SUM
这些值。
set @HourTime = (select SUM(CONVERT(int,substring(TimeStringColumn,1,2)))
from InfoTable WHERE TimeStampColumn=@QueryDate)
set @MinTime = (select SUM(CONVERT(int,substring(TimeStringColumn,3,2)))
from InfoTable WHERE TimeStampColumn=@QueryDate)
set @SecTime = (select SUM(CONVERT(int,substring(TimeStringColumn,5,2)))
from InfoTable WHERE TimeStampColumn=@QueryDate)