English 中文(简体)
• 如何在网上表格中动态创建/编辑/编外服务器表?
原标题:How to create/edit/delete SQL server tables dynamically in asp.net web forms?

我正在使用VS 2008(asp.net with c#)SQL服务器2005

http://www.hchr.org。

create table subject
(subid varchar(10),
subname varchar(20))

样本数据:

subid           subname

1               subject1
2               subject2
3               subject3

=> 它包括每一行的所有主题细节,例如:

<>Result table: Sampledata

studentid     subid        subname             marks

1             1            subject1            12
1             2            subject2            23
1             3            subject3            22
1             4            subject4            20

I want something like this:

studentid     subject1    subject2    subject3    subject4

1             12          23          22           20

2

3

因此,我想在英字母栏中用<代码>表示每个主题。

If I create a static table like...

create table subject    
(subject1 varchar(20),
 subject2 varchar(20),
 subject3 varchar(20))

NOTE: Then it s not possible because the subjects might change per semester (in subject table). That s why I want to create/edit/delete tables dynamically within an ASP.NET web form.

最佳回答

两件事。 首先,在results的表格中,主题名称并不必要,实际上使数据库脱节。 也应如此:

studentid     subid        marks

1             1            12
1             2            23
1             3            22
1             4            20

Now, to query:

SELECT s.subjname, r.studentid, r.marks
FROM subject s
    INNER JOIN result r on s.subid = r.subid

找到你想要的桌子,列出每个学生的分数,使用像样的 p子:

SELECT studentid, 
[Subject1], [Subject2], [Subject3], [Subject4]
FROM
(SELECT s.subjname, r.studentid, r.marks
    FROM subject s
        INNER JOIN result r on s.subid = r.subid
) AS SourceTable
PIVOT
(
AVG(marks)
FOR subjname IN ([Subject1], [Subject2], [Subject3], [Subject4])
) AS PivotTable;

You can make the pivot query dynamic as needed. Not necessary to construct dynamic tables on the fly.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签