Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int newID = 0;
- using (var con1 = new SqlConnection(conString1))
- using (var con2 = new SqlConnection(conString2))
- {
- con1.Open();
- con2.Open();
- using (var selectCommand = new SqlCommand(sqlSelect, con1))
- {
- using (var reader = selectCommand.ExecuteReader())
- {
- try
- {
- if (reader != null)
- if (reader.Read())
- {
- using (var insertCommand = new SqlCommand(sqlInsert, conTarget))
- {
- for (int i = 0; i < reader.FieldCount; i++)
- {
- insertCommand.Parameters.AddWithValue(
- "@" + reader.GetName(i), reader[i]);
- }
- newID = (int)insertCommand.ExecuteScalar();
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- reader.Close();
- }
- }
- }
- }
- using (var con1 = new SqlConnection(ConString1))
- {
- con1.Open();
- var cmd = new SqlCommand(query , con1);
- var reader = cmd.ExecuteReader();
- using (var con2 = new SqlConnection(conString2))
- {
- con2.Open();
- using (var sbc = new SqlBulkCopy(conString2))
- {
- sbc.BulkCopyTimeout = 2000;
- sbc.DestinationTableName = "student";
- try
- {
- if (reader != null)
- sbc.WriteToServer(reader);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- reader.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement