从数据库绑定数据并在C#中旋转/转置数据 [英] Bind data from database and rotate / transpose data in C#

查看:106
本文介绍了从数据库绑定数据并在C#中旋转/转置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



我想从SQL数据库中检索数据并绑定到控件(Datagridview / Datalist)。创建表格式输出,它将行显示为列,列显示为行。



我有一个表:tbl_CustData有两个字段,如 FixedDepDate(datetime数据类型),另一个是 Month_year(nvarchar数据类型) 的。值如下:



FixedDepDate | Month_year

2018-07-03 00:00:00.000 | 3月18日

2018-08-23 00:00:00.000 |八月 - 2018

2018-08-29 00:00:00.000 | 8月 - 2018

2018-07-04 00:00:00.000 | 7月 - 2018年

2018-07-10 00:00:00.000 | 7月 - 2018年

2018-07-25 00:00:00.000 | 7月 - 2018年



预期产出为:



3月 - 18 | 03

7月 - 18 | 04 | 10 | 25

8月 - 18 | 23 | 29







我已经尝试过以下方法,但没有得到我期待的输出。 />
1)使用Datalist / Gridview控件并尝试绑定数据

2)使用PIVOTE运算符但没有得到输出。



另外请知道哪个控件适合绑定数据并显示在我的网页上,或者任何技术来解决这个问题。

注意:我没有使用任何事件来显示我的输出。想要在运行项目后直接在我的页面上显示我的输出。



提前感谢您并等待您的宝贵解决方案。



我尝试了什么:



我想从数据库绑定数据并旋转/转置数据。

Hello Everyone,

I want to retrieve data from SQL database and bind to control(Datagridview / Datalist). Make a table format output and it will shows Rows as column and column as rows.

I have one table: tbl_CustData with two fields such as FixedDepDate(datetime datatype) and another is Month_year (nvarchar datatype). The values are as follows:

FixedDepDate | Month_year
2018-07-03 00:00:00.000 | March-18
2018-08-23 00:00:00.000 | August - 2018
2018-08-29 00:00:00.000 | August - 2018
2018-07-04 00:00:00.000 | July - 2018
2018-07-10 00:00:00.000 | July - 2018
2018-07-25 00:00:00.000 | July - 2018

Expected Output is :

March - 18 | 03
July - 18 | 04 | 10 | 25
August - 18 | 23 | 29



I already tried following methods but not getting output which is I am expecting.
1) Used Datalist/Gridview control and tried to bind data
2) Used PIVOTE operator but not getting output.

Also please give some idea which control is suitable to bind the data and show on my webpage or else any technique to resolve this problem.
Note: I am not using any events to show my output. Want to display my output directly on my page after run the project.

Thanking you in advance and waiting for your valuable solution.

What I have tried:

I want to Bind data from database and rotate / transpose data.

推荐答案

您需要检查将数值转换为您想要的数据库端的数据库
You need to check pivot which convert the value as same whateve you want on database side


我给您一个您需要的示例检查你的结果如何将这些东西合并到你的代码中



SELECT * FROM(选择年份(invoiceDate)为[年],左(datename(month,invoicedate) ),3)作为[月],_

InvoiceAmount作为发票金额)作为InvoiceResult



假设您有

年月金额

2010年1月100

2011年2月90

2010年2月110

然后在sql查询上使用数据透视表,如下面



SELECT *

FROM(

SELECT

年(invoiceDate)为[年],左(datename(month,invoicedate),3)as [month],

InvoiceAmount as amount

FROM Invoice

)as s

PIVOT



SUM(金额)

FOR [月] IN(jan,feb,mar,apr,

may,jun,jul,aug,sep,oct,nov,dec)

)AS pvt



输出将是



年1月2月

2010 100 110

2011 90



请在结束时查看我希望你能得到这方面的帮助。
im giving you one example you need to check at your end how to sonsolidate this things into your code

SELECT * FROM (SELECT year(invoiceDate) as [year], left(datename(month,invoicedate),3)as [month], _
InvoiceAmount as Amount FROM Invoice) as InvoiceResult

Suppose you have data with
Year month amount
2010 Jan 100
2011 Feb 90
2010 Feb 110
Then use pivot table on sql query like below

SELECT *
FROM (
SELECT
year(invoiceDate) as [year],left(datename(month,invoicedate),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS pvt

output will be

year Jan Feb
2010 100 110
2011 90

Please check at your end i hope you got help from this.


这篇关于从数据库绑定数据并在C#中旋转/转置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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