如何使用c#在Windows窗体上显示t-sql聚合函数结果 [英] How to display t-sql aggregate function result on windows form using c#

查看:124
本文介绍了如何使用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) [ ^ ]

< a href =https://msdn.microsoft.com/en-us/library/yy6y35y8%28v=vs.110%29.aspx> 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聚合函数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆