SqlBulkCopy fails during process exit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cwappy

    #1

    SqlBulkCopy fails during process exit

    I have a class that builds a DataTable and then, when the process is
    exiting, tries to write that DataTable to SQL Server 2000 via the
    SqlBulkCopy class. I know the process is exiting because I'm catching
    the ProcessExit event (this is a console app).

    The problem is that calling WriteToServer as seen below will
    immediately exit the program WITHOUT generating any exceptions or any
    other notices, including if I step through the code line-by-line while
    debugging in VS. See my code below. If I call SaveTable() on my own,
    before the process exits, then it executes just fine. Is there a
    problem doing this during ProcessExit? I need to do the SqlBulkCopy
    during process exit!

    Help!

    An abstraction of my code:

    DataTable myDataTable;

    static void Main(string[] args) {
    //Catch process exit
    AppDomain.Curre ntDomain.Proces sExit += new EventHandler(On Exit);

    //Build the datatable...

    //Process exits and OnExit is called...
    }

    void OnExit(object sender, EventArgs e) {
    SaveTable();
    }

    public void SaveTable() {
    //Create our bulk copy object that will import our data into SQL.
    SqlBulkCopy bulk = new SqlBulkCopy("co nnection string");
    bulk.Destinatio nTableName = "tablename" ;

    //Column mapping is done here...

    try {
    bulk.WriteToSer ver(myDataTable );
    }
    catch (Exception e) {
    TargetException (e);
    }
    finally {
    bulk.Close();
    }
    }

Working...