如何显示从DB到gridview的不同表的值 [英] How to display values of different tables from DB to gridview

查看:60
本文介绍了如何显示从DB到gridview的不同表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有49个不同的表,名称为Play_1到Play_49

所有表都有2列(number_i varchar(800),Date_(varchar 800))

其中i = 1到49

i必须在我的网格视图中调用最近4条记录的值

所以如何连接所有这些表以在一个网格视图中显示结果。按日期_。 br />
i必须加入49表。

结果应该像

Date_ Table1 Table2 ........ Table49

21/8/2016 23 55 66

20/8/2016 34 11 44​​

19/8/2016 12 65 22

18/8/2016 22 12 77



我的尝试:



string sqlQuery1 =从Play_1的INNER JOIN中选择顶部(4)Play_2 ON PLay_1.Number_1 = PLay_2.Number_2 ORDER BY convert(datetime,Date_,103)DESC;

SqlCommand command1 = new SqlCommand(sqlQuery1,con);

SqlDataAdapter da1 = new SqlDataAdapter(command1);



DataTable dt1 = new DataTable();

da1.Fill(dt1);

GridView1.DataSource = dt1 ;

GridView1.DataBind();

i have 49 different tables with name Play_1 to Play_49
all tables has 2 column (number_i varchar(800),Date_ (varchar 800))
where i=1to 49
i have to call values in my grid view with last 4 records
so how to join all this tables to display result in one grid view.order by Date_.
i have to join 49 table.
result should be like
Date_ Table1 Table2........Table49
21/8/2016 23 55 66
20/8/2016 34 11 44
19/8/2016 12 65 22
18/8/2016 22 12 77

What I have tried:

string sqlQuery1 = "SELECT top(4) from Play_1 s INNER JOIN Play_2 ON PLay_1.Number_1=PLay_2.Number_2 ORDER BY convert(datetime,Date_,103) DESC ";
SqlCommand command1 = new SqlCommand(sqlQuery1, con);
SqlDataAdapter da1 = new SqlDataAdapter(command1);

DataTable dt1 = new DataTable();
da1.Fill(dt1);
GridView1.DataSource = dt1;
GridView1.DataBind();

推荐答案

首先关闭,更改数据库。

如果是date - 它看起来很像 - 然后将它存储在DATE或DATETIME字段中,而不是VARCHAR中,特别是不存在于VARCHAR(800)中。 800字节来存储10位数的日期值?这两者都非常低效,而且是一个非常糟糕的主意 - 特别是当您想按日期顺序排列事物时 - VARCHAR数据按字符排序,第一个不同的字符决定整个列的顺序,这对于日期来说是无用的首先给出本月的所有10个月,然后是11个月,12个等等,然后是按月后的月份,所以你得到1月10日,然后是2月10日,...然后是1月11日,2月11日,等等。

这同样适用于数字:存储为VARCHAR(800)的简单数字同样愚蠢且难以使用。



更改数据库,然后使用GROUP BY和JOIN获得所需的结果。
First off, change your DB.
If that's a date - and it looks a lot like it - then store it in a DATE or DATETIME field, not a VARCHAR, and particularly not in a VARCHAR(800). 800 bytes to store a 10 digit date value? That's both really inefficient, and a very bad idea - particularly when you want to arrange things in date order - VARCHAR data is sorted character by character, with the first different character deciding the order of the whole column, which is useless for dates as it gives all the 10ths of the month first, then the 11th, 12th, and so on, and then orders by the month after that, so you get all the 10th Jan, then 10th Feb, ... then 11th Jan, 11th Feb, and so on.
The same applies to numbers: a simple number stored as a VARCHAR(800) is just as silly and hard to work with.

Change your DB, and then use GROUP BY and JOIN to get the results you need.


这篇关于如何显示从DB到gridview的不同表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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