Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. int newID = 0;
  2. using (var con1 = new SqlConnection(conString1))
  3. using (var con2 = new SqlConnection(conString2))
  4. {
  5. con1.Open();
  6. con2.Open();
  7. using (var selectCommand = new SqlCommand(sqlSelect, con1))
  8. {
  9. using (var reader = selectCommand.ExecuteReader())
  10. {
  11. try
  12. {
  13. if (reader != null)
  14. if (reader.Read())
  15. {
  16. using (var insertCommand = new SqlCommand(sqlInsert, conTarget))
  17. {
  18. for (int i = 0; i < reader.FieldCount; i++)
  19. {
  20. insertCommand.Parameters.AddWithValue(
  21. "@" + reader.GetName(i), reader[i]);
  22. }
  23. newID = (int)insertCommand.ExecuteScalar();
  24. }
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  30. }
  31. finally
  32. {
  33. reader.Close();
  34. }
  35. }
  36. }
  37. }
  38.  
  39. using (var con1 = new SqlConnection(ConString1))
  40. {
  41. con1.Open();
  42. var cmd = new SqlCommand(query , con1);
  43. var reader = cmd.ExecuteReader();
  44.  
  45. using (var con2 = new SqlConnection(conString2))
  46. {
  47. con2.Open();
  48.  
  49. using (var sbc = new SqlBulkCopy(conString2))
  50. {
  51. sbc.BulkCopyTimeout = 2000;
  52. sbc.DestinationTableName = "student";
  53. try
  54. {
  55. if (reader != null)
  56. sbc.WriteToServer(reader);
  57. }
  58. catch (Exception ex)
  59. {
  60. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  61. }
  62. finally
  63. {
  64. reader.Close();
  65. }
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement