建议如何将html表保存到SQL服务器 [英] advice how to save html table to SQL server

查看:254
本文介绍了建议如何将html表保存到SQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将具有唯一名称的html表保存到sql server表中,并将其数据保存在数据库中。
例如,我有一个这样的html表格(它是由用户在浏览器中动态创建的)。

int varchar(20)http:// content .screencast.com / users / Telman / folders / Jing / media / 4605e7cc-d874-4ac4-ba30-13643fe22f3d / 2010-03-04_0211.png



我使用asp.net,C#,Sql Server 2008 express。
如何在单击保存按钮后创建具有唯一名称,2个colums int和varchar(40)类型以及插入数据的表?



我认为可以通过将表格转换为XML来实现,然后在C#类上使用这个xml,然后保存到数据库中。



你会唱什么?



添加后编辑?



我想保存这个:

 <表> 
< tr>
< td> int< / td>
< td> varchar(40)< / td>
< / tr>
< tr>
< td> 1< / td>
< td>美国< / td>
< / tr>
< tr>
< td> 2< / td>
< td>加拿大< / td>
< / tr>
< tr>
< td> 3< / td>
< td>墨西哥< / td>
< / tr>
< / table>

像这样:

  CREATE TABLE uniqueName 
(key int,
varchar(50))

INSERT INTO uniqueName(键值)
VALUES(1 ,'USA')
INSERT INTO唯一名称(键值)
VALUES(2,'加拿大')
INSERT INTO唯一名称(键值)
VALUES(3,''墨西哥')

将有助于和平的代码更多:)或链接

解决方案

您可以创建两个表格 - 一个用于Html表格的条目(例如 ID 保证是唯一的,并且是Html表的名称),另一个用于表中包含的数据:

  CREATE TABLE dbo.HtmlTables 
(ID INT NOT NULL IDENTITY(1,1)PRIMARY KEY,
HtmlTableName VARCHAR(100) - 或者您需要的任何长度


CREATE TABLE dbo.HtmlTableData
(ID INT NOT NULL IDENTITY(1,1)PRIMARY KEY,
HtmlT ableID INT NOT NULL,
序列INT NOT NULL,
键INT,
Value VARCHAR(500)

您需要在 HtmlTables.ID 字段上的两个表之间创建外键参照完整性:

  ALTER TABLE dbo.HtmlTableData 
ADD CONSTRAINT FK_HtmlTableData_HtmlTables
FOREIGN KEY(HtmlTableID)REFERENCES dbo.HtmlTables(ID )

并且您很可能还想确保每个HtmlTableID的每个序列号只显示一次,所以创建一个唯一的索引。

pre $ CREATE UNIQUE INDEX UIX01_HtmlTableData
ON dbo.HtmlTableData(HtmlTableID,Sequence)

现在您可以将每个HTML表格存储到 dbo.HtmlTables ,并且网格中的每一行都可以存储到 dbo.HtmlTableData 中的一行中,并通过外键关联与HTML表项相关联,通过 Sequence 字段正确排序。


I am trying to save html table to sql server table with unique name and save its data in a database. For example, I have a html table like this(it is dynamically created in browser by user).

int varchar(20) http://content.screencast.com/users/Telman/folders/Jing/media/4605e7cc-d874-4ac4-ba30-13643fe22f3d/2010-03-04_0211.png

I use asp.net, C#, Sql Server 2008 express. How can after clicking on save button, create table with unique name, 2 colums int and varchar(40) types, and insert data?

I think it is possible by rendering table to XML, and then work this xml on C# classes, then save in database.

What you sing about it?

Added after edit?

i want save this:

<table>
    <tr>
       <td>int</td>
       <td>varchar(40)</td>
    </tr>
    <tr>
       <td>1</td>
       <td>USA</td>
    </tr>
    <tr>
       <td>2</td>
       <td>Canada</td>
    </tr>
    <tr>
       <td>3</td>
       <td>Mexico</td>
    </tr>
</table>

like this:

 CREATE TABLE uniqueName
(key int,
values varchar(50)) 

INSERT INTO uniqueName (key, values)
       VALUES (1,  'USA') 
INSERT INTO uniqueName (key, values)
       VALUES (2,  'Canada') 
INSERT INTO uniqueName (key, values)
       VALUES (3,  'Mexico') 

will help peace of code more:) or links

解决方案

You could create two tables - one for the entry for the Html table as such (with an ID guaranteed to be unique, and a name for the Html table), and another one for the data contained in the table:

CREATE TABLE dbo.HtmlTables
(ID INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
 HtmlTableName VARCHAR(100)  -- or whatever length you need
)

CREATE TABLE dbo.HtmlTableData
(ID INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
 HtmlTableID INT NOT NULL,
 Sequence INT NOT NULL,
 Key INT,
 Value VARCHAR(500)
)

You'll want to create a foreign key referential integrity between the two tables on the HtmlTables.ID field:

ALTER TABLE dbo.HtmlTableData
  ADD CONSTRAINT FK_HtmlTableData_HtmlTables
  FOREIGN KEY(HtmlTableID) REFERENCES dbo.HtmlTables(ID)

and you most likely also want to make sure each sequence number shows up only once for each HtmlTableID, so create a unique index.

CREATE UNIQUE INDEX UIX01_HtmlTableData
  ON dbo.HtmlTableData(HtmlTableID, Sequence)

Now you could store each HTML table into an entry in dbo.HtmlTables, and each row in the grid can be stored into a row in dbo.HtmlTableData and be associated with the HTML table entry through the foreign key relationship, and it will be sequenced properly by means of the Sequence field.

这篇关于建议如何将html表保存到SQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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