7

是否有使用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>

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

c:\> build builddb

我安装了 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>

和 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>

然后在 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 "

谢谢你说

4

2 回答 2

3

MSBuild 扩展包包含操作 SQL 数据库的任务(即MSBuild.ExtensionPack.Sql2005MSBuild.ExtensionPack.Sql2008)和以下示例:

<!-- 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"/>
于 2009-12-31T09:35:37.747 回答
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>

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

db.targets

<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>
于 2010-01-02T14:11:00.260 回答