Inserting a dataset into a database table - asp.net

I have table on a database on a server, that is identical to a table on my local. I have click once application that needs to download the version of records on the server down to my local.
At the moment i have webservice that pulls back the records on the server in batches, using asp.net datasets as containers. How do i commit the whole dataset to the table in my local? The table on my local is empty.
Cheers in advance!

If you already have a DataSet, containing one or several DataTables, why don't you just use the SqlDataAdapter and call its ".Update()" method with your DataSet?
In the SqlDataAdapter, you can define an InsertCommand, an UpdateCommand, a DeleteCommand which will take care of the three basic insert/update/delete statements for your rows. All you need to do is define / write those three SQL Statements once, and the SqlDataAdapter will do the rest for you (looping through the rows, figuring out whether to insert, update or delete etc.).
If you want, you can even use your basic SELECT statement from the SelectCommand in your DataSet and use the SqlCommandBuilder to build the INSERT, UPDATE and DELETE statements based on your SELECT.
MSDN Library doc on SqlDataAdapter
SQL Data Adapter without SqlCommandBuilder
MSDN Library doc on SqlCommandBuilder
Marc

There are several options. Here are the first two that come to mind.
One would be to loop through the DataTable and build an SQL Insert statement on each loop and then execute the Insert statement against the local.
Another would be to use the SQL Bulk Copy to insert the data

Related

Dynamic Sql VS Temporary Tables

I have a stored procedure, in which temporary tables "on delete preserve rows" are created dynamically and data was inserted, when i try to execute any other dynamic sql statement, data in the temporary tables are deleted. But I need a data for further process.
Can any one tell me why data is losing, and what is the solution for this.
Thank you.
Three possible reasons for this:
There is an explicit commit.
There is an implicit commit (a DDL statement, typically).
You are are closing the session and starting a new one.
If you cannot avoid these then you'll have to create a permanent table.

Is it necessary to do select command before update command in OracleDataClient

I've recently started working in ASP.NET and RDBMS.I'm making a test form where I can do basic add,update,delete operations.I'm using OracleDataClient to do the DB operations.I was able to populate a listbox using OracleDataAdapter.
Now after clicking on update button,I intend to Update in DB.I've Dataadapter with it's update property.But the update query is not happening.The examples I saw over net all have Select command before Update.Is it actually like that or am I missing some point.
How does Oracle DataClient work with Insert,Update,Delete Commands.
keep it in datalist or temporary table like data table or Hash table....display data in datalist and for update operation use (1)the proper query (2) command Behaviour (3) check the connection and Dml command operation (4) CommandBehavior

ASP.NET SqlDataSource update and create FK reference

The short version:
I have a grid view bound to a data source which has a SelectCommand with a left join in it because the FK can be null. On Update I want to create a record in the FK table if the FK is null and then update the parent table with the new records ID. Is this possible to do with just SqlDataSources?
The detailed version:
I have two tables: Company and Address. The column Company.AddressId can be null. On my ascx page I am using a SqlDataSource to select a left join of company and address and a GridView to display the results. By having my UpdateCommand and DeleteCommand of the SqlDataSource execute two statements separated by a semi-colon I am able to use the GridView's Edit and Delete functionality to update both table simultaneously.
The problem I have is when the Company.AddressId is null. What I need to have happen is have the data source create a record in the Address table and then update the Company table with the new Address.ID then proceed with the update as usual. I would like to do this with just data sources if possible for consistency/simplicity sake. Is it possible to have my data source do this, or perhaps add a second data source to the page to handle some of this?
Once I have that working I can probably figure out how to make it work with the InsertCommand as well but if you are on a roll and have an answer for how to make that fly as well feel free to provide it.
Thanks.
execute two statements separated by a
semi-colon
I don't see any reason why it wouldn't be possible to do both an INSERT and UPDATE in two statements with SqlDataSource just like you are doing here.
However, just so you know, if you have a lot of traffic or users using the application at the same time, you can run into concurrently issues where one user does something that affects another user and unexpected results can cascade and mess up your data. In general, for things like what you are doing - INSERT and UPDATE involving primary or foreign keys, usually SQL TRANSACTIONs are used. But, you must execute them as SQL stored procedures (or functions), on your SQL database. You are still able to call them from your SqlDataSource however by simply telling it that you are calling a stored procedure.

binding different tables from DB into gridview!

how do i dynamically bind different tables from DB with different columns into gridview?
Actually, i was using OleDbDataAdapter to to join the SQL statement and put in a DataTable but my question is, let says, i execute 1st SQL statement and when i execute 2nd SQL statement, the data adapter will use back the 1st SQL statement. So, i was thinking how to make the data adapter to clear off the 1st SQL statement before executing the 2nd statement???
Here are three possible solutions from easiest to hardest:
Join the tables in a view and select against the view for your data source.
Join the tables in a SQL statement and use that as your data source.
Create a new bindable collection (probably a DataTable) and dynamically add data from the two tables to it. Use that as your data source.
You always have
dtbl1.Merge(dtbl2);

Using SQLBulkCopy to Insert/Update database

I have a datatable with the records.I'm inserting records into Sql table using SqlBulkCopy.It works fine.Next time when get the datatable with same records with few changed values SqlBulkCopy is inserting another set of records without updating the previous details.How can I update the Sql table using SqlBulkCopy ?? Please help.
Thanks,
Vix
SqlBulkCopy is only used for inserting records, not updating them as explained here. You'd need to use a different technique to do bulk updates.
e.g. you could SqlBulkCopy into a staging table, then run some SQL to update from there to the main table.
Truncate the table and perform Bulkcopy.
Avoid Truncate table and Create a new temporary table, which BTW consume more space and memory.
I created a Trigger with INSTEAD OF INSERT and use inside MERGE statement.
But don't forget add the parameter SqlBulkCopyOptions.FireTriggers in the SqlBulkCopy.
This is my two cents.
Like mentioned by AdaTheDev, SqlBulkCopy can only insert however there is an alternative library which allow to perform Upsert operations.
Disclaimer: I'm the owner of the project Bulk Operations
The Bulk Operations library has a method "BulkMerge" which Insert or Update rows based on the specified key.
var bulk = new BulkOperation(connection);
bulk.ColumnMappings.Add("ID", true);
bulk.ColumnMappings.Add("Column1");
bulk.ColumnMappings.Add("Column2");
bulk.ColumnMappings.Add("Column3");
bulk.BulkMerge(dt);

Resources