Store DataTable to SQL Server column - c#

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

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.

Hello you can try with WriteXml
this link give you sample 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.
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.

Related

Best way to save Excel row reference

I have a C# application with a SQL Server database.
One functionality of the application is to read a big Excel file and go row by row trying to match an ID column with an ID in the database. Once a row matches, then some of the values are stored in a table.
Later the user is able to change the value on that table but I would like have a way to audit the original values. So I was thinking on creating an XML column in that table that will store the original values of the whole Excel row.
I don't need to implement audit trail to the table, just want to keep the Excel reference row in my SQL Server table for future audit.
Any advice?
External storage of the old value is too hard to maintain, in the long run.
All you need to do is add an "OriginalValue_[fieldname]" column in the SQL table for each value that the user can change. When you populate the table, put the same value in [fieldname] and [OriginalValue_fieldname]. When the user makes changes, you only change [fieldname] but the old value is right in the table.
If this won't work (i.e. you're not allowed to make schema changes), make another table with the ID fields and the original values. Same approach -- no external data.

Need to create a SSIS package which inserts data in destination column

I need to transform one single column values(columnA) from TABLE A in one database to another column B in TABLE B in some other database.
When i transfer columnA for example has (Employee names) but in the destination TABLE B it should be the (Employee ID). I have a lookup table which has the employeeID for the matching employee name.
Conditions:
I need only one single column gets updated in the destination TABLE B. without affecting any other columns.(Is it possible to insert in that way ? i know insertion involves all the columns to be involved)
I know this can be done in SSIS and i have created
* source oledb
*Lookup Transform
*Destination OLEDB
But the problem is at the destination output TABLE B the lookup transform is inserting NULL values to the unmatched column.
Can someone please guide me which is the best way to do this ?
Similarly i need to include various flows for the destination table from various databases.
You need to use a SQL Command as your destination, and have it do an UPDATE command to update your destination table to update the value of the EmployeeID column with the ID from the lookup table. If they are both on the same database, you can do a join in the command, otherwise you should use a Lookup transformation to get the ID based on the name.

Adding Data To DataTable not knowing Primary Key Value

I've written a program in VB (although I'm equally comfortable with C#, so please answer in either language) that is used to insert a large amount of data into a table in my SQL server DB.
The way I've planned on doing it is:
To get the table's Schema from the DB using the SqlDataAdapter.FillSchema method.
Add the many rows of data to my DataTable
Use SqlBulkCopy (.WriteToServer(dtToCopy.CreateDataReader)) to load in the data
I've done this many times before and it's proven very efficient. My problem is that the table I'm writing to has an auto-incrementing primary key column, RowNum.
Now I know I could re-query my table after getting the schema and get the latest value of RowNum and simply write in the values myself into the corresponding column in my DataTable, but my fear is that the table may change between the time of my query and the time of my write, in which case, I could have un-desirable results.
I've seen posts relating to MissingSchemaAction > AddWithKey, but I'm not sure exactly how I could implement it in my case?
Would I just remove the primary key and delete the RowNum column from my DataTable?
And, if so, at what point do I set that property? It doesn't seem to be a property of the DataTable and I don't see where to add it in the SqlBulkCopy.
Thanks!!!
After a lot of searching, I found the solution myself.
Basically, I had to:
Set the PrimaryKey property of my DataTable to Nothing
Remove the RowNum column from my DataTable
Use ColumnMappings for my SqlBulkCopy so every other column is mapped and the identity is left out
Then, since it is a primary key and set as auto-increment in the DB, it gets automatically assigned the correct values at the time of load.
Hope this helps others!!!
Main Source:
SqlBulkCopy Insert with Identity Column

Updating DataTable column from another DataTable

Using .NET 4 C#, I have a DataSet containing two DataTables generated from imported csv files, created from form registration data. One contains a record ID and a timestamp. The other is a list of product registrations (name, address, etc.), with the corresponding record ID (about 10,000 records)
How can I insert the timestamp from the first datatable into the second datatable matching the correct record ID? Can I attach a DataAdapter to existing DataSet and query against them that way? I was hoping I could add a timestamp column to me second datatable and update that from the first.
I know this doesn't make sense (why not include the timestamp when exporting the form data?). I'm using a Joomla form module for registrations, and the export does not include the timestamp. From the DB, I can export the timestamp and record ID data that matches, but the DataBase structure splits the record fields into separate tables, and I can't figure out how to merge the entire record back together into usable content, so I'm stuck with two csv's displaying data for the same record).
Any nudge in the right direction greatly appreciated.
You can of course just iterate all records yourself and match them up. But you can also set up a relationship between the DataTables in the dataset, then create a computed column in the records datatable that pulls a parent column's value. See this example: http://windowsdevcenter.com/dotnet/2003/05/26/datacolumn_expressions.html
See this article on how to actually add relationships: http://msdn.microsoft.com/en-us/library/ay82azad%28v=vs.71%29.aspx
Toward the bottom:
Parent.ColumnName Column in a parent table
So after you create the relationship, you'd do something like: ds["Table2"].Columns.Add("Timestamp", typeof(DateTime), "Parent.Timestamp")
Try this:
add new column to 2nd datatable
DataColumn dc = new DataColumn("DateCol", typeof(System.DateTime));
dt2.Columns.Add(dc);
Now update this datecol column in 2nd datatable using first datatable
dt2.AsEnumerable().Join( dt1.AsEnumerable(),
dt2Rows => dt2Rows.ItemArray["record_id"],
dt1Rows => dt1Rows.ItemArray["record_id"],
(dt2Rows, dt1Rows) => new { dt2Rows, dt1Rows })
.ToList()
.ForEach(i => i.dt2Rows.SetField("DateCol", i.dt1Rows.ItemArray["DateCol"]));
To use above linq query you have to Import the Systems.Linq namespace in your class file

Clone DB table row through MVC in SQL Server

Is there a simple solution for duplicating table rows in SQL Server as well as all table rows with foreign keys pointing to the cloned table row? I've got a "master" table and a bunch of "child" tables which have a foreign key into the ID of the master table. I need to not only create a perfect copy of the master table, but clone each and every child table referencing the master table. Is there a simpler way to do this than creating a new row in the master table, copying in the information from the row to be cloned, then going through each child table and doing the same with each row pointing to the cloned row in the master table?
I'm using a SQL Server 2005 Database accessed through C# ASP.net MVC 1.0.
If by "simple" you mean is there is a procedure that can be called to do it, no there is not. However, you can use the INFORMATION_SCHEMA views such as INFORMATION_SCHEMA.COLUMNS and INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS to query for the columns in a table or the list of related tables and dynamically build your INSERT statements to copy one row to another. Of course, this does not account for other uniqueness constraints that might be on the tables (e.g. a table with a Name column which requires that the values be unique).

Categories

Resources