使用msbuild创建一个sql数据库

eiu*_*165 7 msbuild automated-deploy

有没有一个使用msbuild创建数据库的好教程?

jean paul boodhoo在这篇文章中使用了nant .他设置要在nant构建文件中使用的属性

<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>
Run Code Online (Sandbox Code Playgroud)

然后可以使用这样的命令行创建数据库..

c:\> build builddb
Run Code Online (Sandbox Code Playgroud)

我安装了MSBuild扩展包但我找不到输入连接字符串的位置来连接数据库

谢谢

解决

 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <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>
Run Code Online (Sandbox Code Playgroud)

和Constants.proj看起来像这样

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <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>
Run Code Online (Sandbox Code Playgroud)

然后在vs命令提示符下运行

msbuild db.targets/t:QueryDb

运行的命令是"C:\ Program Files\Microsoft SQL Server\90\Tools\Binn\osql.exe"-U someuser -P somepassword -d NorthwindTest -S(local)\ SQLEXPRESS -Q"SELECT*FROM UserProfile "

谢谢你说

Say*_*imi 3

如果您对该帖子中采用的方法感到满意,那么您也可以简单地遵循 MSBuild 中的方法。例如,创建文件constants.proj(您可以将其命名为任何您喜欢的名称)和db.targets(也可以将其命名为您想要的任何名称)。然后那些将包含类似的内容:

常量.proj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <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>
Run Code Online (Sandbox Code Playgroud)

然后在 db.targets 中,您只需使用这些属性构建命令行并使用Exec任务来执行它,如下所示。

数据库目标

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

  <Target Name="CreateDb">

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

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

  </Target>

</Project>
Run Code Online (Sandbox Code Playgroud)