views:

549

answers:

2

Is there a good tutorial for Creating a database using msbuild?

jean paul boodhoo does it using nant in this post. he sets properties to be used in an nant build file

<properties>
  <property name="sqlToolsFolder" value="C:\Program Files\Microsoft SQL Server\90\Tools\Binn"/>
  <property name="osql.ConnectionString" value="-E"/>
  <property name="initial.catalog" value="Northwind"/>
  <property name="config.ConnectionString" value="data source=(local);Integrated Security=SSPI;Initial Catalog=${initial.catalog}"/> 
  <property name="database.path" value="C:\root\development\databases" />
  <property name="osql.exe"  value="${sqlToolsFolder}\osql.exe" />
</properties>

then can create the database using the command line like this..

c:\> build builddb

I installed the MSBuild Extension pack but I could not find where to enter the connection string to connect to the database

Thanks

RESOLVED

 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
  <Import Project="constants.proj"/>
  <Target Name="QueryDb">
    <PropertyGroup>
      <_Command>-Q  "SELECT * FROM Users"</_Command>
      <_Command2>-i  test.sql</_Command2>
    </PropertyGroup>
    <Exec Command="$(sqlcmd) $(_Command)" /><!---->
  </Target>
</Project>

and Constants.proj looks like this

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
  <PropertyGroup>
    <sqlToolsFolder>C:\Program Files\Microsoft SQL Server\90\Tools\Binn</sqlToolsFolder>
    <initialCatalog>NorthwindTest</initialCatalog>
    <serverInstance>(local)\SQLEXPRESS</serverInstance> 
    <configConnectionString>data source=$(serverInstance);Integrated Security=SSPI;Initial Catalog=$(initialCatalog)</configConnectionString>
    <osqlExe>"$(sqlToolsFolder)\osql.exe"</osqlExe>
    <sqlcmd>$(osqlExe) -U someuser -P somepassword -d $(initialCatalog) -S (local)\SQLEXPRESS</sqlcmd>
    <!--<sqlcmd>$(osqlExe) -E  -d $(initialCatalog) -S (local)\SQLEXPRESS</sqlcmd>-->
  </PropertyGroup>
</Project>

then at the vs command prompt run

msbuild db.targets /t:QueryDb

the command that runs is this "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\osql.exe" -U someuser -P somepassword -d NorthwindTest -S (local)\SQLEXPRESS -Q "SELECT * FROM UserProfile"

Thank you Sayed

+3  A: 

The MSBuild Extension pack contains tasks (namely MSBuild.ExtensionPack.Sql2005 and MSBuild.ExtensionPack.Sql2008) to manipulate SQL databases and the following example:

<!-- Create a database -->
<MSBuild.ExtensionPack.Sql2005.Database TaskAction="Create" DatabaseItem="ADatabase2" MachineName="MyServer\SQL2005Instance"/>
<!-- Create the database again, using Force to delete the existing database -->
<MSBuild.ExtensionPack.Sql2005.Database TaskAction="Create" DatabaseItem="ADatabase2" Force="true" Collation="Latin1_General_CI_AI" MachineName="MyServer\SQL2005Instance"/>
KMoraz
+2  A: 

If you are comfortable with the approach followed in that post then you can simply follow that from MSBuild as well. For example create the file constants.proj (you can name it whatever you like) and db.targets (also name it whatever you want). And then those would contain something like:

constants.proj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
  <PropertyGroup>
    <sqlToolsFolder>C:\Program Files\Microsoft SQL Server\90\Tools\Binn</sqlToolsFolder>
    <osqlConnectionString>-E</osqlConnectionString>
    <initialCatalog>Northwind</initialCatalog>
    <configConnectionString>data source=(local);Integrated Security=SSPI;Initial Catalog=$(initialCatalog)</configConnectionString>
    <databasePath>C:\root\development\databases</databasePath>
    <osqlExe>$(sqlToolsFolder)\osql.exe</osqlExe>
  </PropertyGroup>
</Project>

And then in db.targets you would just build the command line with those properties and use the Exec task to execute it, like the following.

db.targets

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
  <Import Project="constants.targets"/>

  <Target Name="CreateDb">

    <PropertyGroup>
      <_Command> ... FILL IN HERE ... </_Command>
    </PropertyGroup>

    <Exec Command="$(_Command)" />

  </Target>

</Project>
Sayed Ibrahim Hashimi