从网格视图的值动态创建表。 (C#) [英] Dynamically create a table from the values of grid view. (C#)

查看:96
本文介绍了从网格视图的值动态创建表。 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中做了一些内部操作,现在我有一个网格视图,其中包含一定数量的值,如name,day1,day2,day3 ... day31。

假设,我有10行数据。现在我想动态创建一个表,将这些列作为标题,将休息行作为表数据,然后将月份名称保存在数据库中。

请注意,日期将按月更改。 />


我尝试了什么:



我不知道如何这样做。请帮我一些想法。

I have done some internal manipulations in the code and now i have a grid view with certain number of values like name,date of day1,day2,day3...day31.
So assume, i have 10 rows of data. Now i want to create a table dynamically with these columns as headers and rest rows as table data then save the table with month's name in database.
Note that the dates will change as per the month.

What I have tried:

I dont have any idea to how to do so. Please help me with some Ideas.

推荐答案

在数据库中创建一个具有所需模式的表。它不必包含任何数据。 (允许NULL值将容纳没有31天的月份。)

Create a table in your database that has the desired schema. It doesn't have to contain any data. (Allowing NULL values will accommodate months that don't have 31 days.)
CREATE TABLE SCHEMA_TEMPLATE
(
    NAME nvarchar(255) NULL,
    Day1 DATE NULL,
    ...
    Day31 DATE NULL
) ON PRIMARY;





然后,编写一个存储过程,它将创建与SCHEMA_TEMPLTE表具有相同模式的所需数据表。将所需的表名传递给它(例如,May2018):





Then, write a stored proc that will create the desired data table that has the same schema as the SCHEMA_TEMPLTE table. Pass the desired table name to it (for instance, "May2018"):

CREATE PROCEDURE dbo.sp_CreateTableForMonth
    @tableName nvarchar(50)
AS
BEGIN
    -- this query will duplicate the schema of the SCHEMA_TEMPLATE table.
    DECLARE @query nvarchar(MAX) =
    'SELECT TOP 0 * INTO '+@tableName+' FROM dbo.SCHEMA_TEMPLATE';
    EXEC sp_executesql @query;
END





之后,这是一件简单的事情将您的数据放入代码表中。



======================== ===========



我也会重新审视您的架构。在我看来,可能有更好的方式来表示数据,例如只是将所有数据放入同一个表中,并使用 MERGE 命令来更新表。这节拍每个月有一张桌子......



After that, it's a simple matter to put your data into the table from your code.

===================================

I would revisit your schema as well. Seems to me there might be a better way to represent the data, such as just putting all the data into the same table, and using a MERGE command to update the table. This beats having one table for every month...


这篇关于从网格视图的值动态创建表。 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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