使用msbuild创建sql数据库

发布于 2024-08-16 13:43:02 字数 2574 浏览 5 评论 0原文

有没有关于使用 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(本地)\ SQLEXPRESS -Q“从用户配置文件中选择*”

谢谢 赛义德

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

五里雾 2024-08-23 13:43:02

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

constants.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>

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">
  <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">
  <Import Project="constants.targets"/>

  <Target Name="CreateDb">

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

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

  </Target>

</Project>
梦魇绽荼蘼 2024-08-23 13:43:02

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"/>

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"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文