Best Practices for uploading files to database

前端 未结 4 546
暖寄归人
暖寄归人 2020-12-03 16:19

I am looking for any best practices or ideas on how you would create an interface with a DB from a .NET web application to upload data from Excel files Should I use a mechan

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 16:20

    You should upload the data and then flag it if it fails validation checks. For actually loading the data, you have a few options:

    • The ADO.Net bulk-load API - use the bulk load API to put it in a staging table. The snippet below shows a process to open a .CSV file and programatically load it into a staging table.

    .

      public void Load() {
            bool OK = File.Exists(_filename);
            if (OK) {
                string sql = String.Format("Select * from {0}", FileName);
                OleDbConnection csv = new OleDbConnection();
                OleDbCommand cmd = new OleDbCommand(sql, csv);
                OleDbDataReader rs = null;
                SqlConnection db = null;
                SqlCommand clear = null;
    
                SqlBulkCopy bulk_load = null;
                try {
                        // Note two connections: one from the csv file
                        // and one to the database;
                        csv = new OleDbConnection();
                        csv.ConnectionString = ConnectionString;
                        csv.Open();
                        cmd = new OleDbCommand(sql, csv);
                        rs = cmd.ExecuteReader();
    
                        // Dung out the staging table
                        db = // [Create A DB conneciton Here]
                        clear = new SqlCommand("Truncate table Staging", db); // Left to the reader
                        clear.ExecuteNonQuery();
    
                       // Import into the staging table
                        bulk_load = new SqlBulkCopy(db);
                        bulk_load.DestinationTableName = Destination; // Actually an instance var
                        bulk_load.WriteToServer(rs);
                    } catch (Exception ee) {
                        string summary = ee.Message;
                        string detail = ee.StackTrace;
                        //Notify(DisplayType.error, summary, detail);
                    } finally {
                        if (rs != null) rs.Close();
                        if (csv != null) csv.Close();
                        if (bulk_load != null) bulk_load.Close();
                    }
                }
            }
    
    • Use BCP or SSIS to import it, either directly from the spreadsheet or from a .CSV file.

提交回复
热议问题