A couple of handy MSBuild tasks for Microsoft SQL Server scripting. Works with .NET 2.0+. Available under the Apache License 2.0.
The latest SqlMsBuildTasks is now available in NuGet here, or as a zip from the downloads page.
Contributions and new ideas are welcome!
MSBuild task to drop a Microsoft SQL Server database.
To drop the AdventureWorks database in a local SQL Server Express instance (using Windows Authentication):
<SqlDropDatabase ConnectionString="Server=.\SQLEXPRESS;Database=AdventureWorks;Integrated Security=SSPI;"
Database="AdventureWorks" />
Database
: name of the database to be dropped.ConnectionString
: the connection string to use. Note any InitialCatalog will be ignored -- this task always uses themaster
database.
MSBuild task to create a Microsoft SQL Server database.
To create an empty database called AdventureWorks in the local SQL Server Express instance (using Windows Authentication):
<SqlCreateDatabase ConnectionString="Server=.\SQLEXPRESS;Database=AdventureWorks;Integrated Security=SSPI;"
Database="AdventureWorks" />
Database
: name of the database to be created.ConnectionString
: the connection string to use. Note any InitialCatalog will be ignored -- this task always uses themaster
database.
MSBuild task to parse individual keys (e.g. server or port) out of a SQL Server connection string into separate properties. Parsed values can be read via MSBuild Output Paramters.
Parsing the server and database name out of a SQL connection string:
<SqlParseConnectionString ConnectionString="Server=.\SQLEXPRESS;Database=AdventureWorks;Integrated Security=SSPI;">
<Output PropertyName="myDb" TaskParameter="InitialCatalog" />
<Output PropertyName="myServer" TaskParameter="DataSource" />
</SqlParseConnectionString>
<Message Text="Parsed the $(myDb) database on server $(myServer)" />
ConnectionString
: the connection string to parse.
The task uses SqlConnectionStringBuilder internally and can parse any key it supports:
DataSource
(akaserver
,address
,addr
,network address
)InitialCatalog
(akadatabase
)ApplicationName
(akaapp
)AsynchronousProcessing
AttachDBFilename
ConnectTimeout
(akatimeout
)ContextConnection
CurrentLanguage
Encrypt
Enlist
FailoverPartner
IntegratedSecurity
LoadBalanceTimeout
MaxPoolSize
MinPoolSize
MultipleActiveResultSets
NetworkLibrary
PacketSize
Password
(akapwd
)PersistSecurityInfo
Pooling
Replication
TransactionBinding
TrustServerCertificate
TypeSystemVersion
UserID
(akaUser ID
,uid
,user
)UserInstance
WorkstationID
See MSDN for a detailed description of the purpose of each of these keys.
To use the tasks below, you will need to include a reference to the SqlMsBuildTasks.targets file in your MSBuild script:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="path\to\SqlMsBuildTasks.targets" />
<Target ...>
<SqlCreateDatabase ... />
</Target>
</Project>
...alternatively, you can simply reference the individual tasks directly:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask AssemblyFile="path\to\SqlMsBuildTasks.dll" TaskName="SqlMsBuildTasks.SqlCreateDatabase" />
<Target ...>
<SqlCreateDatabase ... />
</Target>
</Project>