jclifford90
jclifford90

Reputation: 35

Store DataTable to SQL Server column

I'm trying to store a DataTable into a single column in a SQL Server table. 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. Later on I want to be able to retrieve that entire datatable back for displaying on a logging aspx page.

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

Upvotes: 3

Views: 799

Answers (3)

Waqar Janjua
Waqar Janjua

Reputation: 6123

Another Idea is to create two tables in your database. It is slightly complex.

One Table contains two columns, Let name the table PTable.

Columns:

ID and ColumnID ID is the primary key and ColumnID contains the name of your column in datatable

After creating this table create another table. It will consists of three fields. Let name it 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. 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.

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

Hello you can try with WriteXml

this link give you sample interessant : http://msdn.microsoft.com/fr-fr/library/system.data.datatable.writexml.aspx

Upvotes: 0

E.J. Brennan
E.J. Brennan

Reputation: 46879

I would probably convert the datatable to XML and store it into an XML field type if I was going to do what you are trying to do.

Upvotes: 1

Related Questions