SqlBulkCopy.cs 20 KB

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