CSV import

Data import from a CSV file

This topic describes how to import data from a CSV file.

Note

Data Import Wizard pages can slightly differ due to the product you have been using.

  1. Decide what table to import the data to:

    For a new table:

    • On the Database menu, click mport Data. The Data Import wizard opens.

    For an existing table:

    • Right-click a table in Database Explorer and select Import Data from the shortcut menu. The Data Import wizard opens with predefined parameters: a Microsoft SQL Server connection, a database, and a table to import the data to.

    • Alternatively, on the Database menu click Import Data, and select a required table on the Destination table wizard page.

  2. Select CSV import format and specify a location of Source data. Click Next.

  3. Specify a Microsoft SQL Server connection, a database, a schema and a table to import the data to. If you selected a table in Database Explorer before opening the Data Import wizard, the wizard will open with the predefined connection parameters of the selected table. To create or edit Microsoft SQL Server connections, click the corresponding buttons. Click Next.

  4. Preview the Source data and specify additional options to customize the import:
    • Set a quantity of lines to skip during the import. Lines are counted from the top.
    • Select the line number where the table header is located. It will be highlighted with blue in the Preview section. If you don’t specify the header position, the imported columns will get default names - column1, column2, etc.
    • Specify a column delimiter. See the result in the Preview section.
  5. Click Next.

    Options

  6. Specify data formats for the Source data and click Next.
  7. Map the Source columns to the Target ones. If you are importing the data into a new table, dbForge Studio will automatically create and map all the columns. If you are importing into an existing table, only columns with the same names will be mapped, the rest should be mapped manually. (If no columns with the same name are found, they are mapped in succession - the 1st column in Source with the 1st column in Target, etc.)

    See the Target columns in the top and the Source columns at the bottom of the wizard page. Click Source column fields and select required columns from the drop-down list. (For more information about mapping, go to Mapping, Data Import Wizard topic.)

    Mapping

    Note

    To cancel mapping of all the columns, click Clear Mappings on the toolbar. To restore it, click Fill Mapping.

  8. If you are importing to a new table, you can edit the Target column properties by double-clicking them in the top grid. Select the Key check box for a column with a primary key and click Next.

    Note

    You should select at least one column with a primary key, otherwise some of import modes on the Modes wizard page will be disabled.

  9. Select an import mode to define how dbForge Studio should import the data. Click Next.
  10. Select how dbForge Studio should handle errors during import and whether you want to get a log file with details about the import session.
  11. Click Import and see the import progress. dbForge Studio will notify you whether the import completed successfully or failed. Click the Show log file button to open the log file.
  12. Click Finish to finish the import and close the wizard.

Note

You can save the import settings as a template for future uses. Click Save Template on any wizard page to save the selected settings. Next time you should only select a template and specify a location of the Source data - all the settings will be already set. For more information, go to the Using Templates topic.

Bulk import using a PowerShell script

You can use a PowerShell script to import data from multiple tables into new tables.

1. Create a folder on your computer for the CSV files that you want to import.

Note

If a file name contains a dot, it is replaced with an underscore in the table name after the bulk import.

2. Create a configuration file with the connection properties for the target database:

2.1. Open any text editor.

2.2. Enter the database connection properties for the target database. Separate each value with a comma.

2.3. Save the .txt file, for example, as databases_config_sqlserver.txt.

  • Server – The server name where the target database is located.

  • Database – The name of the target database.

  • Authentication – False for SQL Server authentication, and True for Windows authentication.

  • Login – The SQL Server login name.

  • Password – The SQL Server login password.

3. Create the import template file:

3.1. In the top menu, select Database > Tasks > Import Data.

3.2. Select CSV and load a CSV source file that you have already placed in the folder. Then click Next.

3.3. Optional: Configure data import options.

3.4. Click the arrow next to Save and select Save Template.

Save Template option

4. Open Windows PowerShell.

5. In the editor, enter the following script. Replace the parameters with your actual values.

# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

$configFile = "<path_to_config_file_with_database_connection_properties>"

$diffToolLocation = "<path_to_dbforge_studio_for_sql_server_file>"

$importPath = "<path_to_folder_with_csv_files>"

$importTemplate = "<path_to_import_template_file>"

$databases = Get-Content $configFile | ForEach-Object {
    $fields = $_ -split ","
    [PSCustomObject]@{
        Host     = $fields[0]
        Database = $fields[1]
        Security = $fields[2]
        User     = $fields[3]
        Password = $fields[4]
    }
}
# Extract database configuration
$MasterDB = $databases[0]


$fileNames = @(Get-ChildItem -Path "$importPath" -Filter *.csv | Select-Object -ExpandProperty Name)
Write-Output "File:" $filenames 
   
     foreach ($fileName in $fileNames) {  
              Write-Output "Processing file: $fileName"
              $inputfile = "$importPath$fileName"
              $table = $fileName.Substring(0, $fileName.Length - 4) 
              $tableName = $table.Replace(".", "_") 
 
              #Command-line string for importing data into your SQL Server database
              $process = Start-Process -FilePath $diffToolLocation -ArgumentList "/dataimport /templatefile:`"$importTemplate`" /connection:`"Data Source=$($MasterDB.Host);Initial Catalog=$($MasterDB.Database);Integrated Security=$($MasterDB.Security);User ID=$($MasterDB.User);Password=$($MasterDB.Password)`" /inputfile:`"$inputfile`" /table:`"$($MasterDB.Database).dbo.$tableName`" /errormode:abort /create " -PassThru -Wait  -windowstyle hidden     
         
               if ($process.ExitCode -eq 0) {
                    Write-Output "File import $fileName completed successfully into table $tableName"             
               } else {
                    Write-Output "Errors occurred while importing files $fileName"             
               }
              Write-Output "ExitCode:" $process.ExitCode            


    #If you need to process the tables one by one to reduce server load - uncomment it.
    #Start-Sleep -Seconds 10                        
      }
  • <path_to_config_file_with_database_connection_properties> – The path to the configuration file that contains the database connection properties.
  • <path_to_dbforge_studio_for_sql_server_file> – The path to the dbForge Studio for SQL Server configuration file.

    The default path to this executable depends on where dbForge Studio for SQL Server is installed. If you installed dbForge Studio:

    • As a standalone tool: C:\Program Files\Devart\dbForge Studio for SQL Server.
    • As a part of the dbForge Edge bundle: C:\Program Files\Devart\dbForge Edge\dbForge Studio for SQL Server.
  • <path_to_folder_with_csv_files> – The path to the folder that contains CSV files.
  • <path_to_import_template_file> – The path to the import template file.

6. Run the script.

Result of imported tables

Download dbForge Studio for SQL Server and try it absolutely free for 30 days!