SqlBulkCopy.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //
  2. // System.Data.SqlClient.SqlBulkCopy.cs
  3. //
  4. // Author:
  5. // Nagappan A ([email protected])
  6. //
  7. // (C) Novell, Inc 2007
  8. //
  9. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Threading;
  34. using System.Threading.Tasks;
  35. using Mono.Data.Tds;
  36. using Mono.Data.Tds.Protocol;
  37. namespace System.Data.SqlClient {
  38. /// <summary>Efficient way to bulk load SQL Server table with several data rows at once</summary>
  39. public sealed class SqlBulkCopy : IDisposable
  40. {
  41. #region Constants
  42. private const string transConflictMessage = "Must not specify SqlBulkCopyOptions.UseInternalTransaction " +
  43. "and pass an external Transaction at the same time.";
  44. private const SqlBulkCopyOptions insertModifiers =
  45. SqlBulkCopyOptions.CheckConstraints | SqlBulkCopyOptions.TableLock |
  46. SqlBulkCopyOptions.KeepNulls | SqlBulkCopyOptions.FireTriggers;
  47. #endregion
  48. #region Fields
  49. private int _batchSize = 0;
  50. private int _notifyAfter = 0;
  51. private int _bulkCopyTimeout = 0;
  52. private SqlBulkCopyColumnMappingCollection _columnMappingCollection = new SqlBulkCopyColumnMappingCollection ();
  53. private string _destinationTableName = null;
  54. private bool ordinalMapping = false;
  55. private bool sqlRowsCopied = false;
  56. private bool isLocalConnection = false;
  57. private SqlConnection connection;
  58. private SqlTransaction externalTransaction;
  59. private SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default;
  60. #endregion
  61. #region Constructors
  62. public SqlBulkCopy (SqlConnection connection)
  63. {
  64. if (connection == null) {
  65. throw new ArgumentNullException("connection");
  66. }
  67. this.connection = connection;
  68. }
  69. public SqlBulkCopy (string connectionString)
  70. {
  71. if (connectionString == null) {
  72. throw new ArgumentNullException("connectionString");
  73. }
  74. this.connection = new SqlConnection (connectionString);
  75. isLocalConnection = true;
  76. }
  77. [MonoTODO]
  78. public SqlBulkCopy (string connectionString, SqlBulkCopyOptions copyOptions)
  79. {
  80. if (connectionString == null) {
  81. throw new ArgumentNullException ("connectionString");
  82. }
  83. this.connection = new SqlConnection (connectionString);
  84. isLocalConnection = true;
  85. if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction)
  86. throw new NotImplementedException ("We don't know how to process UseInternalTransaction option.");
  87. this.copyOptions = copyOptions;
  88. }
  89. [MonoTODO]
  90. public SqlBulkCopy (SqlConnection connection, SqlBulkCopyOptions copyOptions, SqlTransaction externalTransaction)
  91. {
  92. if (connection == null) {
  93. throw new ArgumentNullException ("connection");
  94. }
  95. this.connection = connection;
  96. this.copyOptions = copyOptions;
  97. if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction) {
  98. if (externalTransaction != null)
  99. throw new ArgumentException (transConflictMessage);
  100. }
  101. else
  102. this.externalTransaction = externalTransaction;
  103. if ((copyOptions & SqlBulkCopyOptions.UseInternalTransaction) == SqlBulkCopyOptions.UseInternalTransaction)
  104. throw new NotImplementedException ("We don't know how to process UseInternalTransaction option.");
  105. this.copyOptions = copyOptions;
  106. }
  107. #endregion
  108. #region Properties
  109. public int BatchSize {
  110. get { return _batchSize; }
  111. set { _batchSize = value; }
  112. }
  113. public int BulkCopyTimeout {
  114. get { return _bulkCopyTimeout; }
  115. set { _bulkCopyTimeout = value; }
  116. }
  117. public SqlBulkCopyColumnMappingCollection ColumnMappings {
  118. get { return _columnMappingCollection; }
  119. }
  120. public string DestinationTableName {
  121. get { return _destinationTableName; }
  122. set { _destinationTableName = value; }
  123. }
  124. [MonoTODO]
  125. public bool EnableStreaming {
  126. get { throw new NotImplementedException (); }
  127. set { throw new NotImplementedException (); }
  128. }
  129. public int NotifyAfter {
  130. get { return _notifyAfter; }
  131. set {
  132. if (value < 0)
  133. throw new ArgumentOutOfRangeException ("NotifyAfter should be greater than or equal to 0");
  134. _notifyAfter = value;
  135. }
  136. }
  137. #endregion
  138. #region Methods
  139. public void Close ()
  140. {
  141. if (sqlRowsCopied == true) {
  142. throw new InvalidOperationException ("Close should not be called from SqlRowsCopied event");
  143. }
  144. if (connection == null || connection.State == ConnectionState.Closed) {
  145. return;
  146. }
  147. connection.Close ();
  148. }
  149. private DataTable [] GetColumnMetaData ()
  150. {
  151. DataTable [] columnMetaDataTables = new DataTable [2];
  152. SqlCommand cmd = new SqlCommand ("select @@trancount; " +
  153. "set fmtonly on select * from " +
  154. DestinationTableName + " set fmtonly off;" +
  155. "exec sp_tablecollations_90 '" +
  156. DestinationTableName + "'",
  157. connection);
  158. if (externalTransaction != null)
  159. cmd.Transaction = externalTransaction;
  160. SqlDataReader reader = cmd.ExecuteReader ();
  161. int i = 0; // Skipping 1st result
  162. do {
  163. if (i == 1) {
  164. columnMetaDataTables [i - 1] = reader.GetSchemaTable ();
  165. } else if (i == 2) {
  166. SqlDataAdapter adapter = new SqlDataAdapter ();
  167. adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  168. columnMetaDataTables [i - 1] = new DataTable (DestinationTableName);
  169. adapter.FillInternal (columnMetaDataTables [i - 1], reader);
  170. }
  171. i++;
  172. } while (reader.IsClosed == false && reader.NextResult());
  173. reader.Close ();
  174. return columnMetaDataTables;
  175. }
  176. private string GenerateColumnMetaData (SqlCommand tmpCmd, DataTable colMetaData, DataTable tableCollations)
  177. {
  178. bool flag = false;
  179. string statement = "";
  180. int i = 0;
  181. foreach (DataRow row in colMetaData.Rows) {
  182. flag = false;
  183. foreach (DataColumn col in colMetaData.Columns) { // FIXME: This line not required, remove later
  184. object value = null;
  185. if (_columnMappingCollection.Count > 0) {
  186. if (ordinalMapping) {
  187. foreach (SqlBulkCopyColumnMapping mapping
  188. in _columnMappingCollection) {
  189. if (mapping.DestinationOrdinal == i) {
  190. flag = true;
  191. break;
  192. }
  193. }
  194. } else {
  195. foreach (SqlBulkCopyColumnMapping mapping
  196. in _columnMappingCollection) {
  197. if (mapping.DestinationColumn == (string) row ["ColumnName"]) {
  198. flag = true;
  199. break;
  200. }
  201. }
  202. }
  203. if (flag == false)
  204. break;
  205. }
  206. if ((bool)row ["IsReadOnly"]) {
  207. if (ordinalMapping)
  208. value = false;
  209. else
  210. break;
  211. }
  212. SqlParameter param = new SqlParameter ((string) row ["ColumnName"],
  213. ((SqlDbType) row ["ProviderType"]));
  214. param.Value = value;
  215. if ((int)row ["ColumnSize"] != -1) {
  216. param.Size = (int) row ["ColumnSize"];
  217. }
  218. short numericPresision = (short)row ["NumericPrecision"];
  219. if (numericPresision != 255) {
  220. param.Precision = (byte) numericPresision;
  221. }
  222. short numericScale = (short)row ["NumericScale"];
  223. if (numericScale != 255) {
  224. param.Scale = (byte) numericScale;
  225. }
  226. param.IsNullable = (bool)row ["AllowDBNull"];
  227. tmpCmd.Parameters.Add (param);
  228. break;
  229. }
  230. i++;
  231. }
  232. flag = false;
  233. bool insertSt = false;
  234. foreach (DataRow row in colMetaData.Rows) {
  235. SqlDbType sqlType = (SqlDbType) row ["ProviderType"];
  236. if (_columnMappingCollection.Count > 0) {
  237. i = 0;
  238. insertSt = false;
  239. foreach (SqlParameter param in tmpCmd.Parameters) {
  240. if (ordinalMapping) {
  241. foreach (SqlBulkCopyColumnMapping mapping
  242. in _columnMappingCollection) {
  243. if (mapping.DestinationOrdinal == i && param.Value == null) {
  244. insertSt = true;
  245. }
  246. }
  247. } else {
  248. foreach (SqlBulkCopyColumnMapping mapping
  249. in _columnMappingCollection) {
  250. if (mapping.DestinationColumn == param.ParameterName &&
  251. (string)row ["ColumnName"] == param.ParameterName) {
  252. insertSt = true;
  253. param.Value = null;
  254. }
  255. }
  256. }
  257. i++;
  258. if (insertSt == true)
  259. break;
  260. }
  261. if (insertSt == false)
  262. continue;
  263. }
  264. if ((bool)row ["IsReadOnly"]) {
  265. continue;
  266. }
  267. int columnSize = (int)row ["ColumnSize"];
  268. string columnInfo = "";
  269. if (columnSize >= TdsMetaParameter.maxVarCharCharacters && sqlType == SqlDbType.Text)
  270. columnInfo = "VarChar(max)";
  271. else if (columnSize >= TdsMetaParameter.maxNVarCharCharacters && sqlType == SqlDbType.NText)
  272. columnInfo = "NVarChar(max)";
  273. else if (IsTextType(sqlType) && columnSize != -1) {
  274. columnInfo = string.Format ("{0}({1})",
  275. sqlType,
  276. columnSize.ToString());
  277. } else {
  278. columnInfo = string.Format ("{0}", sqlType);
  279. }
  280. if ( sqlType == SqlDbType.Decimal)
  281. columnInfo += String.Format("({0},{1})", row ["NumericPrecision"], row ["NumericScale"]);
  282. if (flag)
  283. statement += ", ";
  284. string columnName = (string) row ["ColumnName"];
  285. statement += string.Format ("[{0}] {1}", columnName, columnInfo);
  286. if (flag == false)
  287. flag = true;
  288. if (IsTextType(sqlType) && tableCollations != null) {
  289. foreach (DataRow collationRow in tableCollations.Rows) {
  290. if ((string)collationRow ["name"] == columnName) {
  291. statement += string.Format (" COLLATE {0}", collationRow ["collation"]);
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. return statement;
  298. }
  299. private void ValidateColumnMapping (DataTable table, DataTable tableCollations)
  300. {
  301. // So the problem here is that temp tables will not have any table collations. This prevents
  302. // us from bulk inserting into temp tables. So for now we will skip the validation and
  303. // let SqlServer tell us there is an issue rather than trying to do it here.
  304. // So for now we will simply return and do nothing.
  305. // TODO: At some point we should remove this function if we all agree its the right thing to do
  306. return;
  307. // foreach (SqlBulkCopyColumnMapping _columnMapping in _columnMappingCollection) {
  308. // if (ordinalMapping == false &&
  309. // (_columnMapping.DestinationColumn == String.Empty ||
  310. // _columnMapping.SourceColumn == String.Empty))
  311. // throw new InvalidOperationException ("Mappings must be either all null or ordinal");
  312. // if (ordinalMapping &&
  313. // (_columnMapping.DestinationOrdinal == -1 ||
  314. // _columnMapping.SourceOrdinal == -1))
  315. // throw new InvalidOperationException ("Mappings must be either all null or ordinal");
  316. // bool flag = false;
  317. // if (ordinalMapping == false) {
  318. // foreach (DataRow row in tableCollations.Rows) {
  319. // if ((string)row ["name"] == _columnMapping.DestinationColumn) {
  320. // flag = true;
  321. // break;
  322. // }
  323. // }
  324. // if (flag == false)
  325. // throw new InvalidOperationException ("ColumnMapping does not match");
  326. // flag = false;
  327. // foreach (DataColumn col in table.Columns) {
  328. // if (col.ColumnName == _columnMapping.SourceColumn) {
  329. // flag = true;
  330. // break;
  331. // }
  332. // }
  333. // if (flag == false)
  334. // throw new InvalidOperationException ("ColumnName " +
  335. // _columnMapping.SourceColumn +
  336. // " does not match");
  337. // } else {
  338. // if (_columnMapping.DestinationOrdinal >= tableCollations.Rows.Count)
  339. // throw new InvalidOperationException ("ColumnMapping does not match");
  340. // }
  341. // }
  342. }
  343. private void BulkCopyToServer (DataTable table, DataRowState state)
  344. {
  345. if (connection == null || connection.State == ConnectionState.Closed)
  346. throw new InvalidOperationException ("This method should not be called on a closed connection");
  347. if (_destinationTableName == null)
  348. throw new ArgumentNullException ("DestinationTableName");
  349. if (isLocalConnection && connection.State != ConnectionState.Open)
  350. connection.Open();
  351. if ((copyOptions & SqlBulkCopyOptions.KeepIdentity) == SqlBulkCopyOptions.KeepIdentity) {
  352. SqlCommand cmd = new SqlCommand ("set identity_insert " +
  353. table.TableName + " on",
  354. connection);
  355. cmd.ExecuteScalar ();
  356. }
  357. DataTable [] columnMetaDataTables = GetColumnMetaData ();
  358. DataTable colMetaData = columnMetaDataTables [0];
  359. DataTable tableCollations = columnMetaDataTables [1];
  360. if (_columnMappingCollection.Count > 0) {
  361. if (_columnMappingCollection [0].SourceOrdinal != -1)
  362. ordinalMapping = true;
  363. ValidateColumnMapping (table, tableCollations);
  364. }
  365. SqlCommand tmpCmd = new SqlCommand ();
  366. TdsBulkCopy blkCopy = new TdsBulkCopy ((Tds)connection.Tds);
  367. if (((Tds)connection.Tds).TdsVersion >= TdsVersion.tds70) {
  368. string statement = "insert bulk " + DestinationTableName + " (";
  369. statement += GenerateColumnMetaData (tmpCmd, colMetaData, tableCollations);
  370. statement += ")";
  371. #region Check requested options and add corresponding modifiers to the statement
  372. if ((copyOptions & insertModifiers) != SqlBulkCopyOptions.Default) {
  373. statement += " WITH (";
  374. bool commaRequired = false;
  375. if ((copyOptions & SqlBulkCopyOptions.CheckConstraints) == SqlBulkCopyOptions.CheckConstraints) {
  376. if (commaRequired)
  377. statement += ", ";
  378. statement += "CHECK_CONSTRAINTS";
  379. commaRequired = true;
  380. }
  381. if ((copyOptions & SqlBulkCopyOptions.TableLock) == SqlBulkCopyOptions.TableLock) {
  382. if (commaRequired)
  383. statement += ", ";
  384. statement += "TABLOCK";
  385. commaRequired = true;
  386. }
  387. if ((copyOptions & SqlBulkCopyOptions.KeepNulls) == SqlBulkCopyOptions.KeepNulls) {
  388. if (commaRequired)
  389. statement += ", ";
  390. statement += "KEEP_NULLS";
  391. commaRequired = true;
  392. }
  393. if ((copyOptions & SqlBulkCopyOptions.FireTriggers) == SqlBulkCopyOptions.FireTriggers) {
  394. if (commaRequired)
  395. statement += ", ";
  396. statement += "FIRE_TRIGGERS";
  397. commaRequired = true;
  398. }
  399. statement += ")";
  400. }
  401. #endregion Check requested options and add corresponding modifiers to the statement
  402. blkCopy.SendColumnMetaData (statement);
  403. }
  404. blkCopy.BulkCopyStart (tmpCmd.Parameters.MetaParameters);
  405. long noRowsCopied = 0;
  406. foreach (DataRow row in table.Rows) {
  407. if (row.RowState == DataRowState.Deleted)
  408. continue; // Don't copy the row that's in deleted state
  409. if (state != 0 && row.RowState != state)
  410. continue;
  411. bool isNewRow = true;
  412. int i = 0;
  413. foreach (SqlParameter param in tmpCmd.Parameters) {
  414. int size = 0;
  415. object rowToCopy = null;
  416. if (_columnMappingCollection.Count > 0) {
  417. if (ordinalMapping) {
  418. foreach (SqlBulkCopyColumnMapping mapping
  419. in _columnMappingCollection) {
  420. if (mapping.DestinationOrdinal == i && param.Value == null) {
  421. rowToCopy = row [mapping.SourceOrdinal];
  422. SqlParameter parameter = new SqlParameter (mapping.SourceOrdinal.ToString (),
  423. rowToCopy);
  424. if (param.MetaParameter.TypeName != parameter.MetaParameter.TypeName) {
  425. parameter.SqlDbType = param.SqlDbType;
  426. rowToCopy = parameter.Value = parameter.ConvertToFrameworkType (rowToCopy);
  427. }
  428. string colType = string.Format ("{0}", parameter.MetaParameter.TypeName);
  429. if (colType == "nvarchar" || colType == "ntext" || colType == "nchar") {
  430. if (row [i] != null && row [i] != DBNull.Value) {
  431. size = ((string) parameter.Value).Length;
  432. size <<= 1;
  433. }
  434. } else if (colType == "varchar" || colType == "text" || colType == "char") {
  435. if (row [i] != null && row [i] != DBNull.Value)
  436. size = ((string) parameter.Value).Length;
  437. } else {
  438. size = parameter.Size;
  439. }
  440. break;
  441. }
  442. }
  443. } else {
  444. foreach (SqlBulkCopyColumnMapping mapping
  445. in _columnMappingCollection) {
  446. if (mapping.DestinationColumn == param.ParameterName) {
  447. rowToCopy = row [mapping.SourceColumn];
  448. SqlParameter parameter = new SqlParameter (mapping.SourceColumn, rowToCopy);
  449. if (param.MetaParameter.TypeName != parameter.MetaParameter.TypeName) {
  450. parameter.SqlDbType = param.SqlDbType;
  451. rowToCopy = parameter.Value = parameter.ConvertToFrameworkType (rowToCopy);
  452. }
  453. string colType = string.Format ("{0}", parameter.MetaParameter.TypeName);
  454. if (colType == "nvarchar" || colType == "ntext" || colType == "nchar") {
  455. if (row [mapping.SourceColumn] != null && row [mapping.SourceColumn] != DBNull.Value) {
  456. size = ((string) rowToCopy).Length;
  457. size <<= 1;
  458. }
  459. } else if (colType == "varchar" || colType == "text" || colType == "char") {
  460. if (row [mapping.SourceColumn] != null && row [mapping.SourceColumn] != DBNull.Value)
  461. size = ((string) rowToCopy).Length;
  462. } else {
  463. size = parameter.Size;
  464. }
  465. break;
  466. }
  467. }
  468. }
  469. i++;
  470. } else {
  471. rowToCopy = row [param.ParameterName];
  472. string colType = param.MetaParameter.TypeName;
  473. /*
  474. If column type is SqlDbType.NVarChar the size of parameter is multiplied by 2
  475. FIXME: Need to check for other types
  476. */
  477. if (colType == "nvarchar" || colType == "ntext" || colType == "nchar") {
  478. size = ((string) row [param.ParameterName]).Length;
  479. size <<= 1;
  480. } else if (colType == "varchar" || colType == "text" || colType == "char") {
  481. size = ((string) row [param.ParameterName]).Length;
  482. } else {
  483. size = param.Size;
  484. }
  485. }
  486. if (rowToCopy == null)
  487. continue;
  488. blkCopy.BulkCopyData (rowToCopy, isNewRow, size, param.MetaParameter);
  489. if (isNewRow)
  490. isNewRow = false;
  491. } // foreach (SqlParameter)
  492. if (_notifyAfter > 0) {
  493. noRowsCopied ++;
  494. if (noRowsCopied >= _notifyAfter) {
  495. RowsCopied (noRowsCopied);
  496. noRowsCopied = 0;
  497. }
  498. }
  499. } // foreach (DataRow)
  500. blkCopy.BulkCopyEnd ();
  501. }
  502. private bool IsTextType(SqlDbType sqlType)
  503. {
  504. return (sqlType == SqlDbType.NText ||
  505. sqlType == SqlDbType.NVarChar ||
  506. sqlType == SqlDbType.Text ||
  507. sqlType == SqlDbType.VarChar ||
  508. sqlType == SqlDbType.Char ||
  509. sqlType == SqlDbType.NChar);
  510. }
  511. public void WriteToServer (DataRow [] rows)
  512. {
  513. if (rows == null)
  514. throw new ArgumentNullException ("rows");
  515. if (rows.Length == 0)
  516. return;
  517. DataTable table = new DataTable (rows [0].Table.TableName);
  518. foreach (DataColumn col in rows [0].Table.Columns) {
  519. DataColumn tmpCol = new DataColumn (col.ColumnName, col.DataType);
  520. table.Columns.Add (tmpCol);
  521. }
  522. foreach (DataRow row in rows) {
  523. DataRow tmpRow = table.NewRow ();
  524. for (int i = 0; i < table.Columns.Count; i++) {
  525. tmpRow [i] = row [i];
  526. }
  527. table.Rows.Add (tmpRow);
  528. }
  529. BulkCopyToServer (table, 0);
  530. }
  531. public void WriteToServer (DataTable table)
  532. {
  533. BulkCopyToServer (table, 0);
  534. }
  535. public void WriteToServer (IDataReader reader)
  536. {
  537. DataTable table = new DataTable ("SourceTable");
  538. SqlDataAdapter adapter = new SqlDataAdapter ();
  539. adapter.FillInternal (table, reader);
  540. BulkCopyToServer (table, 0);
  541. }
  542. public void WriteToServer (DataTable table, DataRowState rowState)
  543. {
  544. BulkCopyToServer (table, rowState);
  545. }
  546. [MonoTODO]
  547. public void WriteToServer (DbDataReader reader)
  548. {
  549. throw new NotImplementedException ();
  550. }
  551. [MonoTODO]
  552. public Task WriteToServerAsync (DbDataReader reader)
  553. {
  554. throw new NotImplementedException ();
  555. }
  556. [MonoTODO]
  557. public Task WriteToServerAsync (DbDataReader reader, CancellationToken cancellationToken)
  558. {
  559. throw new NotImplementedException ();
  560. }
  561. private void RowsCopied (long rowsCopied)
  562. {
  563. SqlRowsCopiedEventArgs e = new SqlRowsCopiedEventArgs (rowsCopied);
  564. if (null != SqlRowsCopied) {
  565. SqlRowsCopied (this, e);
  566. }
  567. }
  568. #endregion
  569. #region Events
  570. public event SqlRowsCopiedEventHandler SqlRowsCopied;
  571. #endregion
  572. void IDisposable.Dispose ()
  573. {
  574. //throw new NotImplementedException ();
  575. if (isLocalConnection) {
  576. Close ();
  577. connection = null;
  578. }
  579. }
  580. }
  581. }