本文介绍了如何使用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()); } 推荐答案 string selectcom1=@"select sum(Inv) As SumOfInv, StaffNumber, YEAR([Date]) AS YearFROM Grawu WHERE YEAR([Date])=@yearGROUP 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[^] 这篇关于如何使用c#在Windows窗体上显示t-sql聚合函数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 15:05