Accessing Files in Dot.NET

From PeformIQ Upgrade
Revision as of 18:59, 30 January 2011 by PeterHarding (talk | contribs) (Created page with 'http://www.codeproject.com/KB/aspnet/ImportExportCSV.aspx =A Simplified SQL-CSV Import/Export Functionality= ==File Import== This event is mentioned here to give you an idea ...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

http://www.codeproject.com/KB/aspnet/ImportExportCSV.aspx

A Simplified SQL-CSV Import/Export Functionality

File Import

This event is mentioned here to give you an idea about how to pass the data to the functions and complete your task. You can see an instance of SetProperties() to set the various parameters for importing a file to a database. Check out this list of properties and their use:

  • TableName: Name of the table (new or existing) where the records will be stored.
  • FileInformation: Stores the details of the selected file.
  • DropExistingTable: Checks for the table in your database and deletes it only when this flag is set to true (default is false).
  • CsvDirOnServer: Name of the folder on your web server where the CSV/text file will be stored. This is useful only when you set SaveFileOnServer is set to true.

Note: Make sure that two of the properties TableName and FileInformation are provided. These two are necessary because without them your CSV/text file will not be exported.

private void Submit_Click(object sender, System.EventArgs e)
{
    try
    {
        FileInfo FICSV = new FileInfo(OFDSelectFile.PostedFile.FileName);
        SetProperties sp = new SetProperties();
    
        if (IsValidFile(FICSV))
        {
            sp.TableName = txtTableName.Text;
            sp.CsvDirOnServer = txtCSVDir.Text;
            sp.DropExistingTable=true;
            sp.SaveFileOnServer=true;
            sp.FileInformation = FICSV;

            switch(ddlDB.SelectedItem.Value)
            {
                case "SQL":    
                    ClsSQLCSV objSQL = new ClsSQLCSV();
                    importstatus.Text = objSQL.GenerateTable(sp);
                    break;
                case "MSA":
                    ClsMSACSV objMSA = new ClsMSACSV();
                    importstatus.Text = objMSA.GenerateTable(sp);
                    break;
            }
        }
    }
    catch(Exception ex)
    {
        importstatus.Text = ex.Message.ToString() + "<br>";
        importstatus.Text += "Error importing. Please try again";
    }
}

File Export

Submit_Click

This event gives you an idea about how to pass the data to the functions and complete your task of export. Here too you can see an instance of SetProperties() to set various parameters for importing a file to a database. Check out this list of properties and their use:

  • ExportTableName: Name of the database table (new or existing) from where the records will be fetched.
  • ExportCSVasName: Name of the CSV/text file to store the data.
  • ExportCSVDirOnServer: Name of the folder where the CSV/text file will be stored on the web server (note that the folder will take the root path of your application). Default is the root path.
  • ExportAsCsvOrText: Default will be "C". This means that the file will be stored with a .csv extension. Not setting this property will also save your file as .csv. You can just provide the name of the file without the extension in the textbox provided to save the file as .csv.

Note: Make sure that two of the properties ExportTableName and ExportCSVasName are provided. These two are necessary because without them your CSV/text file will not be generated.


private void Submit_Click(object sender, System.EventArgs e)
{
    try
    {                
        SetProperties sp = new SetProperties();
        sp.ExportTableName = txtTableName.Text;
        sp.ExportCSVasName = txtCSVFileName.Text;
        sp.ExportCSVDirOnServer = txtCSVDir.Text;
        sp.ExportAsCsvOrText = "T"; //"C" for CSV or "T" for Text

        switch(ddlDB.SelectedItem.Value)
        {
            case "SQL":
                ClsSQLCSV objSQL = new ClsSQLCSV();
                importstatus.Text = objSQL.GenerateCSVFile(sp);
                break;
            case "MSA":
                ClsMSACSV objMSA = new ClsMSACSV();
                importstatus.Text = objMSA.GenerateCSVFile(sp);
                break;
        }
    }
    catch(Exception ex)
    {
        importstatus.Text = ex.Message.ToString() + "<br>";
        importstatus.Text += "Error exporting. Please try again";
    }
}