shanescott.sql-data-exporter
1.0.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet tool install --global shanescott.sql-data-exporter --version 1.0.1
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
dotnet tool install --local shanescott.sql-data-exporter --version 1.0.1
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=shanescott.sql-data-exporter&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
nuke :add-package shanescott.sql-data-exporter --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SQL Table Export CLI Tool
A .NET CLI tool that connects to SQL Server, inspects table structure, retrieves data, and exports everything to a structured JSON file with metadata and data rows.
Features
- 🔍 Complete Metadata Extraction: Captures table schema, column definitions, primary keys, foreign keys, and more
- 📊 Data Export: Exports table data as JSON with proper type conversion
- 🎯 Flexible Filtering: Support for row limits and WHERE clause filters
- ⚙️ Multiple Configuration Options: Connection string via CLI, environment variable, or config file
- 🚀 Easy to Use: Simple command-line interface with sensible defaults
Installation
Install as a Global Tool
# From NuGet.org (Recommended)
dotnet tool install --global shanescott.sql-data-exporter
# Or from local build
dotnet pack
dotnet tool install --global --add-source ./nupkg shanescott.sql-data-exporter
Build from Source
cd sql-table-export
dotnet build
dotnet run -- --help
# Or use the installed tool
shanescott.sql-data-exporter --help
Usage
Basic Usage
# Export a table (uses environment variable or config file for connection)
shanescott.sql-data-exporter --table users
# Specify connection string
shanescott.sql-data-exporter -c "Server=localhost;Database=MyDb;Integrated Security=true;" -t users
# With schema
shanescott.sql-data-exporter -t employees -s hr
# With custom output path
shanescott.sql-data-exporter -t customers -o ./exports/customers.json
Advanced Usage
# Limit number of rows
shanescott.sql-data-exporter -t orders -l 100
# With WHERE filter
shanescott.sql-data-exporter -t customers -w "country = 'USA' AND status = 'active'"
# Combined options
shanescott.sql-data-exporter \
-c "Server=prod-db;Database=Sales;User Id=sa;Password=***;" \
-t transactions \
-s sales \
-l 1000 \
-w "date >= '2025-01-01'" \
-o ./reports/sales-transactions.json
Connection String Options
1. CLI Option (Highest Priority)
shanescott.sql-data-exporter -c "Server=localhost;Database=MyDb;Integrated Security=true;" -t users
2. Environment Variable
# Windows PowerShell
$env:SQL_CONNECTION_STRING = "Server=localhost;Database=MyDb;Integrated Security=true;"
# Windows CMD
set SQL_CONNECTION_STRING=Server=localhost;Database=MyDb;Integrated Security=true;
# Linux/Mac
export SQL_CONNECTION_STRING="Server=localhost;Database=MyDb;User Id=sa;Password=***;"
# Then run
shanescott.sql-data-exporter -t users
3. Configuration File (Lowest Priority)
Edit appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=MyDb;Integrated Security=true;"
}
}
Command-Line Options
| Option | Alias | Description | Required | Default |
|---|---|---|---|---|
--connection |
-c |
SQL Server connection string | No | From env/config |
--table |
-t |
Table name | Yes | - |
--schema |
-s |
Schema name | No | dbo |
--limit |
-l |
Maximum number of rows | No | All rows |
--where |
-w |
WHERE clause filter | No | - |
--output |
-o |
Output file path | No | ./<schema>.<table>.json |
Output Format
The tool generates a JSON file with the following structure:
{
"metadata": {
"schema": "dbo",
"tableName": "users",
"exportedAt": "2025-11-25T21:04:00Z",
"rowCount": 150,
"totalRowsInTable": 1000,
"primaryKeys": ["userId"],
"foreignKeys": [
{
"columnName": "departmentId",
"referencedSchema": "dbo",
"referencedTable": "departments",
"referencedColumn": "id"
}
]
},
"columns": [
{
"name": "userId",
"dataType": "int",
"maxLength": null,
"precision": 10,
"scale": 0,
"isNullable": false,
"isPrimaryKey": true,
"isForeignKey": false,
"defaultValue": null,
"isIdentity": true
},
{
"name": "username",
"dataType": "nvarchar",
"maxLength": 50,
"precision": null,
"scale": null,
"isNullable": false,
"isPrimaryKey": false,
"isForeignKey": false,
"defaultValue": null,
"isIdentity": false
}
],
"data": [
{
"userId": 1,
"username": "john_doe",
"email": "john@example.com"
},
{
"userId": 2,
"username": "jane_smith",
"email": "jane@example.com"
}
]
}
Data Type Conversion
SQL Server types are converted to JSON-compatible types:
| SQL Server Type | JSON Type | Notes |
|---|---|---|
int, bigint, smallint |
Number | Direct conversion |
decimal, numeric, money |
Number | Preserves precision |
varchar, nvarchar, char |
String | Direct conversion |
datetime, datetime2, date |
String | ISO 8601 format |
bit |
Boolean | true/false |
uniqueidentifier |
String | GUID string |
varbinary, binary |
String | Base64 encoded |
NULL |
null |
JSON null |
Examples
Export User Table
shanescott.sql-data-exporter -t users
Output: dbo.users.json
Export with Filter
shanescott.sql-data-exporter -t orders -w "status = 'pending' AND created_date > '2025-01-01'"
Export Limited Rows
shanescott.sql-data-exporter -t logs -l 500 -o ./recent-logs.json
Export from Different Schema
shanescott.sql-data-exporter -t employees -s hr -o ./hr-data/employees.json
Troubleshooting
Connection Issues
Problem: "No connection string provided"
# Solution: Provide connection string via any method
shanescott.sql-data-exporter -c "Server=localhost;..." -t users
# OR
$env:SQL_CONNECTION_STRING = "Server=localhost;..."
Problem: "Cannot open database"
# Solution: Check connection string and permissions
# Verify with sqlcmd:
sqlcmd -S localhost -d MyDb -E -Q "SELECT 1"
Table Not Found
Problem: "Table [dbo].[users] does not exist"
# Solution: Check table name and schema
shanescott.sql-data-exporter -t users -s dbo # Specify correct schema
Permission Denied
Problem: "SELECT permission denied"
# Solution: Ensure SQL user has SELECT permission
# Grant permission:
GRANT SELECT ON [schema].[table] TO [username]
Requirements
- .NET 8.0 or later
- SQL Server 2012 or later (or Azure SQL Database)
- Appropriate database permissions (SELECT on target tables)
Development
Build
dotnet build
Run Tests
dotnet test
Create Package
dotnet pack -c Release
Install Locally
dotnet tool install --global --add-source ./nupkg shanescott.sql-data-exporter
Uninstall
dotnet tool uninstall --global shanescott.sql-data-exporter
Project Structure
sql-table-export/
├── Commands/
│ └── ExportCommand.cs # CLI command implementation
├── Configuration/
│ └── ConnectionStringProvider.cs # Connection string resolution
├── Models/
│ ├── ColumnMetadata.cs # Column metadata model
│ ├── ForeignKeyInfo.cs # Foreign key model
│ ├── TableMetadata.cs # Table metadata model
│ └── ExportResult.cs # Export result model
├── Services/
│ ├── ISqlMetadataService.cs # Metadata service interface
│ ├── SqlMetadataService.cs # Metadata extraction
│ ├── IDataExportService.cs # Export service interface
│ └── DataExportService.cs # Data export logic
├── Program.cs # Entry point
├── appsettings.json # Configuration file
└── sql-table-export.csproj # Project file
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Future Enhancements
- Support for multiple tables in one export
- Export to CSV, XML, or Parquet formats
- Support for PostgreSQL and MySQL
- Incremental exports
- Data masking/anonymization
- Progress bars for large exports
- Parallel processing
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
v1.0.1: Updated documentation with correct command name and NuGet installation instructions. Updated LICENSE file with author information.