Adb ODBC Provider for SQL Server
Note
If possible, use the Adb SqlClient Provider, since it has more features and typically higher performance compared to the ODBC provider.
You can use this database (among others) in your ETL:
- Databases: Microsoft SQL Server® 2008 onwards
- Features:
- Includes all optional provider services
- AdbSqlClientTableInformationService supports tables (including temporary), views, and table-valued functions
- Includes all optional provider services
- Adb provider and overload:
- .NET provider: "System.Data.Odbc", included with operating system (external documentation)
- Type enumeration: OdbcType
- Client namespace: System.Data.Odbc
- ODBC driver: Install Microsoft ODBC Driver for SQL Server
- Connection strings:
Data Type Mappings
This section lists which database types map to or from which .NET types when querying an ODBC SQL Server and working with its database parameters.
Unless otherwise noted, all the below .NET types can be round-tripped to the database, i.e. written out with one of the database target workers, and read back with AdbDataReaderSource<TOutput>.
Note
Mappings tagged with AdbColumnSchema
below depend on whether or not an
AdbColumnSchema is supplied to
SetMappedType(AdbParameter, Type, AdbColumnSchema, ParameterDirection).
Of the out of box workers, this is only supplied by AdbInsertTarget<TInputError>.
Provider-Independent .NET Types
.NET types below with Shallow or SingleShallowThenDeep copy policy support multi-copy, the others (with SingleShallow) do not.
.NET Type Display Name | DbType (or OdbcType) enum |
SQL Type Example | Copy Policy |
---|---|---|---|
Any enum or nullable enum |
Byte, Int16, Int32, Int64 | tinyint , smallint , int , bigint |
Shallow |
System.Boolean, System.Boolean? |
Boolean | bit |
Shallow |
System.Byte, System.Byte? |
Byte | tinyint |
Shallow |
System.Byte[] |
Binary | binary(7) , image , varbinary(10) , varbinary(max) |
SingleShallowThenDeep |
System.DateTime, System.DateTime? |
OdbcType.DateTime | datetime . Without AdbColumnSchema also: date , datetime2 , smalldatetime . |
Shallow |
System.DateTime, System.DateTime? |
OdbcType.Date, OdbcType.DateTime, OdbcType.SmallDateTime | With AdbColumnSchema: date , datetime2 , smalldatetime |
Shallow |
System.Decimal, System.Decimal? |
Decimal | decimal(38,0) , money , numeric(10,2) , smallmoney |
Shallow |
System.Double, System.Double? |
Double | float |
Shallow |
System.Guid, System.Guid? |
Guid | uniqueidentifier |
Shallow |
System.Int16, System.Int16? |
Int16 | smallint |
Shallow |
System.Int32, System.Int32? |
Int32 | int , integer |
Shallow |
System.Int64, System.Int64? |
Int64 | bigint |
Shallow |
System.Single, System.Single? |
Single | real |
Shallow |
System.String | String | char(32) , nchar(32) , ntext , nvarchar(max) , text , varchar(max) |
Shallow |
Provider-Specific .NET Types
This provider does not map any provider-specific .NET types. It does however use OdbcType
when mapping System.DateTime
above.
Parameters
Use positional parameters of the format ?
. You can use
GetPlaceholder(String) to get the placeholder
in a portable manner.
Stored Procedures
Here we execute a stored procedure with parameters. Even building an executable program (or utility method) for executing this single query can be very worthwhile since actionETL adds configuration, error handling, and logging.
We create the command builder outside the worker system to make it easy to return a parameter value to the caller. More commonly, commands and connections are created inside the worker system, as and when they are needed.
using actionETL;
using actionETL.Adb;
using actionETL.Adb.OdbcExternal;
using System.Data;
public static partial class ExecuteStoredProcedureOdbc
{
public static DateTime GetMaxDateTime()
{
// Get a command builder using "actionetl.aconfig.json" connection details.
// StoredProcedure(string) sets both CommandText and CommandType properties.
var cb = AdbOdbcProvider.GetSqlServer()
.CreateCommandBuilderFromDefaultAConfig("OdbcSqlServerTester")
.StoredProcedure("dbo.GetMaxDateTime ?, ?, ?");
// Many methods return an instance and allow method chaining.
// Whenever possible, set the database type explicitly.
cb.Parameters.Add("countryId").SetDbValue(42, DbType.Int32);
// Database type not set, provider has to guess the type (which is less good)
cb.Parameters.Add("category").SetValue("Batteries");
// Save the parameter reference, since we retrieve the returned value later
var maxDateTime = cb.Parameters.Add("maxDateTime", ParameterDirection.Output)
.SetDbType(DbType.DateTime);
new WorkerSystem()
// Curly braces and semi-colon not needed in 1-line lambda
.Root(ws => _ = new AdbExecuteNonQueryWorker(ws, "Call GetMaxDateTime", cb))
.Start()
.ThrowOnFailure();
// Value is of type object, and must be cast
return (DateTime)maxDateTime.DbValue;
}
}
/* The example assumes the following stored procedure already exists:
CREATE PROCEDURE dbo.GetMaxDateTime
@countryId int,
@category nvarchar(50),
@maxDateTime DateTime output
AS
BEGIN
SET NOCOUNT ON;
-- Use input parameters...
SELECT @maxDateTime = '2017-03-13';
END
*/
Adb Information
The Adb Information example program dumps the below information with this provider querying a SQL Server. See that example for more information.
=== AdbConnectionBuilder ===
Provider=System.Data.Odbc:SQL Server:
=== AdbConnection ===
DataSourceName=MYHOST\LOCALDB#6A3842B2
DatabaseName=actionetl_tester
NumberOfColumnIdentifierParts=4
NumberOfColumnRestrictions=4
ServerVersion=13.00.4001
=== AdbConnection.Information ===
CompositeIdentifierSeparator=.
CompositeIdentifierSeparatorPattern=\.
DataSourceProductName=Microsoft SQL Server
DataSourceProductVersion=13.00.4001
GetPlaceholder("MyName")=?
GroupByBehavior=MustContainAll
IdentifierCase=Insensitive
IdentifierPattern=
IdentifierQuotePrefix=[
IdentifierQuotePrefixes=["
IdentifierQuoteSuffix=]
IdentifierQuoteSuffixes=]"
OrderByColumnsInSelect=False
ParameterMarkerFormat=?
ParameterMarkerPattern=\?
ParameterNameMaxLength=0
ParameterNamePattern=
QuotedIdentifierCase=Insensitive
QuotedIdentifierPattern="(([^"]|"")*)"
QuoteIdentifierPart("My Table")=[My Table]
StatementSeparatorPattern=
StringLiteralPattern=
SupportedJoinOperators=Inner, LeftOuter, RightOuter, FullOuter
UsesPositionalParameters=True
=== AdbConnection.TableInformation.CreateTableIdentifier([My Database].[My Schema].MyTable) ===
OriginalCompositeName=[My Database].[My Schema].MyTable
OriginalTableName=MyTable
OriginalSchemaName=[My Schema]
OriginalCatalogName=[My Database]
QuotedCompositeName=[My Database].[My Schema].[MyTable]
QuotedTableName=[MyTable]
QuotedSchemaName=[My Schema]
QuotedCatalogName=[My Database]
UnquotedTableName=MyTable
UnquotedSchemaName=My Schema
UnquotedCatalogName=My Database
=== AdbConnection.SchemaInformationCollection ===
Note: The metadata schemas can be retrieved with AdbConnection.GetSchema().
The returned information is provided from the .NET provider as-is. Some collections
can be inaccurate, and different values can also be needed for different purposes,
which is why database specific actionETL providers can be created that replace
individual values with the desired ones. See the vendor provider documentation for
details on the returned information.
CollectionName=MetaDataCollections
IsCommon=True
NumberOfRestrictions=0
NumberOfIdentifierParts=0
CollectionName=DataSourceInformation
IsCommon=True
NumberOfRestrictions=0
NumberOfIdentifierParts=0
CollectionName=DataTypes
IsCommon=False
NumberOfRestrictions=0
NumberOfIdentifierParts=0
CollectionName=Restrictions
IsCommon=False
NumberOfRestrictions=0
NumberOfIdentifierParts=0
CollectionName=ReservedWords
IsCommon=False
NumberOfRestrictions=0
NumberOfIdentifierParts=0
CollectionName=Columns
IsCommon=False
NumberOfRestrictions=4
NumberOfIdentifierParts=4
CollectionName=Indexes
IsCommon=False
NumberOfRestrictions=4
NumberOfIdentifierParts=4
CollectionName=Procedures
IsCommon=False
NumberOfRestrictions=4
NumberOfIdentifierParts=3
CollectionName=ProcedureColumns
IsCommon=False
NumberOfRestrictions=4
NumberOfIdentifierParts=4
CollectionName=ProcedureParameters
IsCommon=False
NumberOfRestrictions=4
NumberOfIdentifierParts=4
CollectionName=Tables
IsCommon=False
NumberOfRestrictions=3
NumberOfIdentifierParts=3
CollectionName=Views
IsCommon=False
NumberOfRestrictions=3
NumberOfIdentifierParts=3