Hello I have a project that is linked to a SQL Compact database (xxx.sdf file) and need to properly transfer data from my datagridview and a table within it. This may not be how I should do it but I’ve spent hours trying to parametrize a whole bunch of queries to do this individually to the table (without success). I’ve been reading article after article and maybe I’m searching for the wrong thing.
My grid is bound to tableNameBindingSource. I cannot programmatically add rows to the grid because it is bound. I’m looking for the easiest way to add hundreds of rows to my SQL table at once and wanted to try to use the datagridview to accomplish that.
Can I (unbind the SQL table and) add the rows as needed to the datagridview table then force its contents to become the contents of the database table? Is there some kind of translator (I’ve used the SQLDataAdaptor to go from SQL table to datagridview)?
As long as your grid view table (source data) has the same structure (columns, data types) as your SQL table you should be able to use SQLBulkCopy to quickly copy the data.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx
I’ve used this to copy 100,000s of rows in a relatively small amount of time.
Check out this CodeProject and the link above:
http://www.codeproject.com/Articles/18418/Transferring-Data-Using-SqlBulkCopy
(edit) I just noticed you said SQLCompact. I’m not sure that SqlBulkCopy will work with the compact edition. There is a CodePlex project though called SQLCEBulkCopy.