If you just need to transfer the data, and you need to do it through C #, then resorting to using the DataTable optional.
Streaming data loading in SqlServer is done using SqlBulkCopy , streaming reading from Access is OleDbDataReader using OleDbDataReader .
SqlBulkCopy can accept not only DataRow[] and DataTable , but also DbDataReader and IDataReader as DbDataReader . Because OleDbDataReader is one and the other; it is enough to submit the OleDbDataReader to the WriteToServer(...) method of the WriteToServer(...) instance:
SqlConnection sqlConnection = ...; OleDbConnection oledbConnection = ...; using (var oledbCmd = new OleDbCommand(@" select Column1, Column2, ... from Table_Name", oledbConnection)) using (var oledbReader = oledbCmd.ExecuteReader()) using (var bulkCopy = new SqlBulkCopy(sqlConnection)) { bulkCopy.DestinationTableName = "dbo.TableName"; bulkCopy.EnableStreaming = true; bulkCopy.WriteToServer(oledbReader); }
A target table must of course be created before this.
Another option is to do without C #, using tools exclusively SqlServer. To do this, you need to install the Access DBMS driver in the same place where the SqlServer instance is installed and configure its use.
Then, using the OPENROWSET construct, OPENROWSET can access the Access database file and import data from the selected table with the SELECT ... INTO command (or INSERT INTO ... SELECT if the target table has already been created):
SELECT t.Column1, t.Column2, ... INTO dbo.TableName FROM OPENROWSET( 'Microsoft.ACE.OLEDB.12.0', 'N:\AccessDbFiles\MsAccessDatabaseFile.accdb'; 'admin';'', [Table_Name]) AS t
Or through linked-server . We connect Access-base:
EXEC sp_addlinkedserver @server = 'AccessDbName', -- алиас @srvproduct = 'Access', @provider = 'Microsoft.ACE.OLEDB.12.0', @datasrc = 'N:\AccessDbFiles\MsAccessDatabaseFile.accdb'
then we transfer the data to the necessary table:
SELECT t.Column1, t.Column2, ... INTO dbo.TableName FROM [AccessDbName]...[Table_Name] AS t
Three points in [AccessDbName]...[AccessDb_Table_Name] - this is how it should be ( four-part naming , where the directory and schema are not specified).
Note that in this case the path to the Access DB file ( N:\AccessDbFiles\MsAccessDatabaseFile.accdb in the examples) is the path in the coordinates of the SqlServer instance (the path can be networked), and not in the coordinates of the host from which it is connected .