SqlTask Class

Summary

A task to execute arbitrary SQL statements against a OLEDB data source.
Assembly
NAnt.Contrib.dll
Namespace
NAnt.Contrib.Tasks
Base Types
  • Task
graph BT Type-->Base0["Task"] Type["SqlTask"] class Type type-node

Syntax

[TaskName("sql")]
public class SqlTask : Task

Examples

Execute a set of statements inside a transaction.

    <sql
    connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
    transaction="true"
    delimiter=";"
    delimstyle="Normal"
>
    INSERT INTO jobs (job_desc, min_lvl, max_lvl) VALUES('My Job', 22, 45);
    INSERT INTO jobs (job_desc, min_lvl, max_lvl) VALUES('Other Job', 09, 43);
    SELECT * FROM jobs;
</sql>

Execute a set of statements from a file and write all query results to a file.

    <sql
    connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
    transaction="true"
    delimiter=";"
    delimstyle="Normal"
    print="true"
    source="sql.txt"
    output="${outputdir}/results.txt"
/>

Execute a SQL script generated by SQL Server Enterprise Manager.

    <sql
    connstring="Provider=SQLOLEDB;Data Source=localhost; Initial Catalog=Pruebas; Integrated Security=SSPI"
    transaction="true"
    delimiter="GO"
    delimstyle="Line"
    print="true"
    source="pubs.xml"
    batch="false"
    output="${outputdir}/results.txt"
/>

Remarks

You can specify a set of sql statements inside the sql element, or execute them from a text file that contains them. You can also choose to execute the statements in a single batch, or execute them one by one (even inside a transaction, if you want to).

Attributes

Type Description
TaskName

Fields

Name Constant Value Summary
_embeddedSqlStatements

Properties

Name Value Summary
Append bool
Whether output should be appended to or overwrite an existing file. The default is false.
Batch bool
If true, the statements will be executed as a single batch. If false, they will be executed one by one. Default is true.
CommandTimeout int
Command timeout to use when creating commands.
ConnectionString string
Connection string used to access database. This should be an OleDB connection string.
Delimiter string
String that separates statements from one another.
DelimiterStyle DelimiterStyle
Kind of delimiter used. Allowed values are Normal or Line.
Encoding Encoding
The encoding of the files containing SQL statements. The default is the system's current ANSI code page.
ExpandProperties bool
If true, the any nant-style properties on the sql will be expanded before execution. Default is true.
Output string
If set, the results from the statements will be output to the specified file.
OutputWriter TextWriter
Gets the underlying System.IO.TextWriter to which output will be written if NAnt.Contrib.Tasks.SqlTask.Output is set.
Print bool
If set to true, results from the statements will be output to the build log.
QuoteChar string
The character(s) to surround result columns with when printing, the default is an empty string.
ShowHeaders bool
If set to true, prints headers for result sets. The default is true.
Source string
File where the sql statements are defined.
UseTransaction bool
If set to true, all statements will be executed within a single transaction. The default is true.

Methods

Name Value Summary
ExecuteTask() void
This is where the work is done.
Initialize() void
Initializes task and ensures the supplied attributes are valid.