如何使用c#在Windows窗体上显示t-sql聚合函数结果
大家好,
我试图使用C#在Windows窗体上显示T-SQL聚合函数(例如sum)的结果但是我得到了错误。任何人都可以帮忙。下面是我试过的代码。
我尝试过:
Hi Everyone,
I am trying to display the result of T-SQL aggregate function(e.g. sum) on a windows form using C# but I'm getting errors. could anybody help. below is the codes I've tried.
What I have tried:
string selectcom1=select sum(Inv),StaffNumber, DATEPART(year,Date) from Grawu where datepart(YEAR,[Date])='2015'
group by StaffNumber, DATEPART(YEAR,[Date])
SqlCommand comm1 = new SqlCommand(selectcom1, connection);
comm1.CommandType = CommandType.StoredProcedure;
comm1.Parameters.AddWithValue("@year", cmbyear.SelectedItem);
SqlDataReader dr1 = comm1.ExecuteReader();
if (dr1.Read())
{
year = dr1["year"].ToString();
Messagebox.Show("Anwer"+ dr1["inv"].ToString());
}
请阅读我对该问题的评论。
检查:
Please, read my comment to the question.
Check this:
string selectcom1=@"select sum(Inv) As SumOfInv, StaffNumber, YEAR([Date]) AS Year
FROM Grawu
WHERE YEAR([Date])=@year
GROUP BY StaffNumber, YEAR([Date])";
其余代码,您应该分别更改为MSDN文档:
SqlCommand.CommandType属性(System.Data.SqlClient) [ ^ ]
Con计算参数和参数数据类型 [ ^ ]
SqlCommand.ExecuteReader方法(System.Data.SqlClient) [ ^ ]
使用DataReader检索数据 [ ^ ]
The rest of your code, you should change respectivelly to the MSDN documentation:
SqlCommand.CommandType Property (System.Data.SqlClient)[^]
Configuring Parameters and Parameter Data Types[^]
SqlCommand.ExecuteReader Method (System.Data.SqlClient)[^]
Retrieving Data Using a DataReader[^]