简体   繁体   English

将DataTable存储到SQL Server列

[英]Store DataTable to SQL Server column

I'm trying to store a DataTable into a single column in a SQL Server table. 我正在尝试将DataTable存储到SQL Server表中的单个列中。 The idea behind this is a user runs a SQL command and the returned output is stored into a datatable, then I want that datatable to be stored into a SQL Server logging table. 这背后的想法是用户运行SQL命令并将返回的输出存储到数据表中,然后我希望将该数据表存储到SQL Server日志记录表中。 Later on I want to be able to retrieve that entire datatable back for displaying on a logging aspx page. 稍后,我希望能够检索整个数据表,以便在日志记录的aspx页面上显示。

Currently I'm just storing it as a big string but that doesn't give me column headers and the formatting is kinda funky as well as being inefficient. 目前我只是将它存储为一个大字符串,但这并没有给我列标题,格式有点时髦,效率低下。

TIA TIA

我可能会将数据表转换为XML并将其存储到XML字段类型中,如果我要执行您要执行的操作。

Hello you can try with WriteXml 你好,你可以试试WriteXml

this link give you sample interessant : http://msdn.microsoft.com/fr-fr/library/system.data.datatable.writexml.aspx 此链接为您提供示例interessant: http//msdn.microsoft.com/fr-fr/library/system.data.datatable.writexml.aspx

Another Idea is to create two tables in your database. 另一个想法是在数据库中创建两个表。 It is slightly complex. 它有点复杂。

One Table contains two columns, Let name the table PTable. 一个表包含两列,我们将表格命名为PTable。

Columns: 列:

ID and ColumnID ID is the primary key and ColumnID contains the name of your column in datatable ID and ColumnID ID是主键,ColumnID包含数据表中列的名称

After creating this table create another table. 创建此表后,创建另一个表。 It will consists of three fields. 它将包括三个领域。 Let name it STable. 我们把它命名为STable。 This table stores the columns of you datatable. 此表存储您的数据表的列。

Columns: 列:

stblID, PtblID and PtColumnID StbID is the primary key in this table, PtblID is the Primary key of PTable and PtColumnID is the ColumnID of PTable. stblID, PtblID and PtColumnID StbID是此表中的主键,PtblID是PTable的主键,PtColumnID是PTable的ColumnID。 This table stores the rows of table. 该表存储表的行。 Now store the data in this table and receive the data when you need it. 现在将数据存储在此表中,并在需要时接收数据。

and the simplest idea is to create a table in your datbabase having an xml column and store your datatable as an xml. 最简单的想法是在数据库中创建一个包含xml列的表,并将数据表存储为xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM