SQL Server: Database Export-Import Guide
db sql sqlsvr sql-svr
This guide covers exporting data from Azure SQL to a local SQL Server instance, exporting to SQLite, and importing back.
Nb: Some scripts placed in GitHub/djaus2 repository: djaus2/SQLScripts_:
Table of Contents
- Prerequisites
- Azure SQL to Local SQL Server
- Local SQL Server to SQLite
- SQLite to Local SQL Server
- Connection String Configuration
- Troubleshooting
Prerequisites
Required Software
- SQL Server (local instance named such as MYSQLSVR)
- SQL Server Management Studio (SSMS) - for database management
- SQL Server Data Tools (SSDT) - includes SqlPackage.exe (included with Visual Studio 2022)
- sqlite3 command-line tool - for SQLite operations
- PowerShell - for running automation scripts
Verify Installation
Check if SqlPackage.exe is installed:
Get-ChildItem "C:\Program Files\Microsoft Visual Studio" -Recurse -Filter "SqlPackage.exe"
Check if sqlite3 is installed:
sqlite3 --version
Azure SQL to Local SQL Server
Method 1: Automated Script (Recommended)
Use the provided PowerShell script to automate the entire process.
File: BackupRemoteToLocal.ps1
Usage:
cd <To the scripts directory>
.\BackupRemoteToLocal.ps1
What the script does:
- Exports Azure SQL database to a .bacpac file
- Drops the existing local database (if exists)
- Imports the .bacpac to local SQL Server
- Cleans up temporary files
Script Configuration:
The script uses these configuration variables (modify as needed):
Assuming same settings local and remote for simplicity, but you can change them as needed.
$remoteServer = "mydatabase.database.windows.net,1433"
$remoteDatabase = ""
$remoteUser = ""
$remotePassword = ""
$localServer = "127.0.0.1\MYSQLSVR"
$localDatabase = $remoteDatabase
$localUser = $remoteUser
$localPassword = $remotePassword
- Change
mydatabaseto your Azure SQL server name. - Change
MYSQLSVRto your local SQL Server instance name if different.
Method 2: Manual Export/Import
Export from Azure SQL to create .bacpac file
Using SqlPackage.exe:
SqlPackage.exe /Action:Export `
/SourceServerName:$remoteServer `
/SourceDatabaseName:$remoteDatabase `
/SourceUser:$remoteUser `
/SourcePassword:$remotePassword `
/TargetFile:$remoteDatabase.bacpac
Import to Local SQL Server
Using SqlPackage.exe:
SqlPackage.exe /Action:Import `
/SourceFile:$remoteDatabase.bacpac `
/TargetServerName:$localServer `
/TargetDatabaseName:$localDatabase `
/TargetUser:$localUser `
/TargetPassword:$localPassword `
/TargetConnectionString:"Data Source=$localServer;Initial Catalog=$localDatabase;User ID=$localUser;Password=$localPassword;TrustServerCertificate=True;"
Local SQL Server to SQLite
Method 1: SQL Script Generation (Recommended)
Use the provided PowerShell script to generate a SQL script file.
File: ExportToSqliteSimple.ps1
Usage:
cd <to scripts folder>
.\ExportToSqliteSimple.ps1
What the script does:
- Connects to local SQL Server
- Exports all table schemas
- Converts SQL Server data types to SQLite equivalents
- Generates INSERT statements for all data
- Creates a .sql file with all statements
Output: A timestamped .sql file (e.g., MyDatabase_20260715_184041.sql)
Import to SQLite
Using PowerShell:
Get-Content MyDatabase_20260715_184041.sql | sqlite3 output.db
Using Command Prompt:
sqlite3 output.db < MyDatabase_20260715_184041.sql
Using DB Browser for SQLite (GUI):
- Download from: https://sqlitebrowser.org/
- Open DB Browser for SQLite
- File → Open Database → Create new database
- Database → Execute SQL → Select the .sql file
- Click Execute
Method 2: Direct .NET Export (Advanced)
For more control, use the ExportToSqlite.ps1 script which requires System.Data.SQLite.
Prerequisites:
- Install System.Data.SQLite from: https://system.data.sqlite.org/
Usage:
.\ExportToSqlite.ps1
This creates a direct .sqlite file without needing to import via sqlite3.
SQLite to Local SQL Server
Method 1: Using DB Browser for SQLite
- Export from SQLite to SQL:
- Open SQLite database in DB Browser
- File → Export → Database to SQL file
- Save as .sql file
- Modify SQL for SQL Server:
- Open the exported .sql file in a text editor
- Replace SQLite-specific syntax with SQL Server syntax:
INTEGER PRIMARY KEY AUTOINCREMENT→INT IDENTITY(1,1) PRIMARY KEYTEXT→NVARCHAR(MAX)or appropriate data typeREAL→DECIMALorFLOAT- Remove SQLite-specific pragmas
- Import to SQL Server:
- Open SSMS
- Connect to local SQL Server
- File → Open → Open the modified .sql file
- Execute the script
Method 2: Manual Data Transfer
For small datasets, you can manually transfer data using SSMS:
- Generate CREATE TABLE scripts from SQLite:
SELECT sql FROM sqlite_master WHERE type='table' AND name='YourTableName'; -
Convert to SQL Server syntax and create tables
- Export data from SQLite to CSV:
sqlite3 yourdb.db ".headers on" ".mode csv" ".output data.csv" "SELECT * FROM YourTableName;" ".quit" - Import CSV to SQL Server:
BULK INSERT YourTableName FROM 'C:\path\to\data.csv' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', FIRSTROW = 2 );
Connection String Configuration
appsettings.json
The project uses two connection strings for flexibility:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=127.0.0.1\\MYSQLSVR;Initial Catalog=<local db name>;User ID=<local db user>;Password=<local db pwd>;TrustServerCertificate=True;",
"ConnectionStringRemote": "Data Source=tcp:<remote db svr name>.database.windows.net,1433;Initial Catalog=<remote db name>;User ID=<remote db user name>;Password=<remote db pwd>;"
}
}
Switching Between Local and Remote
For Local Development:
- Use
DefaultConnection(points to local SQL Server)
For Production/Remote:
- Update code to use
ConnectionStringRemote - Or swap the connection string values in appsettings.json
Connection String Parameters
Local SQL Server:
Data Source=127.0.0.1\MYSQLSVR2- Server address and instance nameInitial Catalog=HelperLog200- Database nameUser ID=djaus- SQL Server authentication usernamePassword=Amyliz2o24sql- SQL Server authentication passwordTrustServerCertificate=True- Bypass SSL certificate validation (for development)
Azure SQL:
Data Source=tcp:sportronics.database.windows.net,1433- Azure SQL server with TCP protocolInitial Catalog=HelperLog200- Database nameUser ID=djaus- Azure SQL usernamePassword=Amyliz2o24sql- Azure SQL password
Troubleshooting
SQL Server Connection Issues
Error: “A network-related or instance-specific error occurred”
Solutions:
- Ensure SQL Server service is running:
Get-Service 'MSSQL$MYSQLSVR2' - Ensure TCP/IP is enabled (requires SQL Server Configuration Manager):
- Open SQL Server Configuration Manager
- SQL Server Network Configuration → Protocols for MYSQLSVR2
- Enable TCP/IP
- Restart SQL Server service
- Ensure SQL Server Browser service is running:
Set-Service SQLBrowser -StartupType Automatic Start-Service SQLBrowser
Error: “The certificate chain was issued by an authority that is not trusted”
Solution: Add TrustServerCertificate=True to connection string.
SqlPackage Issues
Error: “SqlPackage.exe not found”
Solution: The script searches multiple paths. If not found:
- Install SQL Server Data Tools (SSDT)
- Or update the
$sqlPackagePathsarray in the script with your installation path
Error: “‘TrustServerCertificate’ is not a valid argument for the ‘Import’ action”
Solution: Use /TargetConnectionString instead of individual parameters:
/TargetConnectionString:"Data Source=server;Initial Catalog=db;User ID=user;Password=pass;TrustServerCertificate=True;"
SQLite Import Issues
Error: “ParserError” when using < redirection in PowerShell
Solution: Use PowerShell syntax:
Get-Content file.sql | sqlite3 output.db
Or use cmd.exe:
cmd /c "sqlite3 output.db < file.sql"
Error: Data type conversion issues
Solution: The script automatically converts SQL Server types to SQLite equivalents. If you encounter issues:
- Check the type mapping in
ExportToSqliteSimple.ps1 - Modify the mapping as needed for your specific data types
Database Already Exists
When importing to local SQL Server, if the database already exists:
The BackupRemoteToLocal.ps1 script automatically drops the existing database before import. If you need to preserve data:
- Rename the existing database in SSMS
- Or modify the script to skip the drop step
- Or manually drop the database before running the script
Automation Scripts Reference
BackupRemoteToLocal.ps1
Purpose: Export Azure SQL database to local SQL Server
Location: Project root directory
Key Features:
- Automatic SqlPackage.exe detection
- SSL certificate handling
- Automatic database cleanup
- Progress reporting
ExportToSqliteSimple.ps1
Purpose: Export local SQL Server to SQL script for SQLite
Location: Project root directory
Key Features:
- No additional dependencies required
- Automatic type conversion
- Batch writing for large datasets
- Handles NULL values and string escaping
ExportToSqlite.ps1
Purpose: Direct export to .sqlite file (requires System.Data.SQLite)
Location: Project root directory
Key Features:
- Creates SQLite database directly
- No intermediate SQL file
- Requires System.Data.SQLite installation
Best Practices
- Backup Before Operations: Always backup your databases before export/import operations
- Test in Development: Test scripts on non-production databases first
- Verify Data Integrity: After import, verify row counts and key data
- Secure Credentials: Store passwords securely (consider using environment variables or Azure Key Vault for production)
- Document Changes: Keep track of schema changes between exports
- Schedule Regular Backups: Set up automated backups for critical data
Additional Resources
- SQL Server Documentation
- Azure SQL Documentation
- SQLite Documentation
- SqlPackage Documentation
- DB Browser for SQLite
Quick Reference
Export Azure to Local:
.\BackupRemoteToLocal.ps1
Export Local to SQLite:
.\ExportToSqliteSimple.ps1
Get-Content output.sql | sqlite3 output.db
Check SQL Server Service:
Get-Service 'MSSQL$MYSQLSVR2'
Restart SQL Server Service:
Restart-Service 'MSSQL$MYSQLSVR2'
Check SQL Browser Service:
Get-Service SQLBrowser
Start SQL Browser Service:
Start-Service SQLBrowser
| Topic | Subtopic | |
| < Prev: | IoT Hub | Complete navigation guide for Softata Pico W firmware, configuration, and deployment |
| This Category Links | ||
| Category: | Database Index: | Database |