如何:在项目中使用 Microsoft SQL Server 2012 对象How to: Use Microsoft SQL Server 2012 Objects in Your Project

在此示例中,你将向一个面向 Microsoft SQL Server 2012 的数据库项目中添加序列对象。In this example, you will add a sequence object to a database project targeting Microsoft SQL Server 2012.

Microsoft SQL Server 2012 中引入了序列。Sequences are introduced in Microsoft SQL Server 2012. 序列是一种用户定义的架构绑定对象,它根据创建该序列时采用的规范生成一组数值。A sequence is a user-defined schema-bound object that generates a sequence of numeric values according to the specification with which the sequence was created. 这组数值以定义的间隔按升序或降序生成,并且可根据要求循环(重复)。The sequence of numeric values is generated in an ascending or descending order at a defined interval and may cycle (repeat) as requested. 有关序列对象的详细信息,请参阅序列号For more information on sequence objects, see Sequence Numbers. 有关 Microsoft SQL Server 2012 新增功能的信息,请参阅 SQL Server 2012 中的新增功能For information on what's new in Microsoft SQL Server 2012, see What's New in SQL Server 2012.

警告

以下过程利用在连接的数据库开发面向项目的脱机数据库开发部分中的之前过程中创建的实体。The following procedures utilize entities created in previous procedures in the Connected Database Development and Project-Oriented Offline Database Development sections.

向您的项目添加一个新的序列对象To add a new Sequence object to your project

  1. 在“解决方案资源管理器” 中,右键单击“TradeDev” 数据库项目,选择“添加” ,然后选择“新项” 。Right-click the TradeDev database project in Solution Explorer, select Add, then New Item.

  2. 在左窗格上单击“可编程性” ,然后选择“序列” 。Click Programmability on the left pane, and select Sequence. 单击“添加” ,将新对象添加到项目中。Click Add to add the new object to the project.

  3. 用以下内容替换默认代码。Replace the default code with the following.

    CREATE SEQUENCE [dbo].[Seq1]  
    AS INT  
    START WITH 1  
    INCREMENT BY 1  
    MAXVALUE 1000  
    NO CYCLE  
    CACHE 10  
    
  4. 如果项目的目标平台未设置为 Microsoft SQL Server 2012,则“错误列表”将显示 CREATE SEQUENCE 语句的语法错误 。If your project's target platform is not set to Microsoft SQL Server 2012, the Error List will show a syntax error for the CREATE SEQUENCE statement. 若要更正此问题,请按照如何:更改目标平台并发布数据库项目主题所述相应地更改目标平台。To correct this, follow the How to: Change Target Platform and Publish a Database Project topic to change the target platform accordingly.

  5. 请按照如何:更改目标平台并发布数据库项目主题所述,将项目发布到已连接的 Microsoft SQL Server 2012 服务器中的数据库。Follow the How to: Change Target Platform and Publish a Database Project topic to publish the project to a database in your connected Microsoft SQL Server 2012 server.

使用新的序列对象To use the new Sequence object

  1. 在 SQL Server 对象资源管理器中,右击你在上一个过程中发布到的数据库,然后选择“新建查询” 。In SQL Server Object Explorer, right-click database you have published to in the previous procedure, and select New Query.

  2. 将以下代码粘贴到查询窗口中。Paste the following code to the query window.

    DECLARE @counter INT  
    SET @counter=0  
    WHILE @counter<10  
    BEGIN  
        SET @counter = @counter +1  
         INSERT dbo.Products (Id, Name, CustomerId) VALUES (NEXT VALUE FOR dbo.Seq1, 'ProductItem'+cast(@counter as varchar), 1)  
    END   
    GO  
    
  3. 按下“执行查询” 按钮。Press the Execute Query button.

  4. 在 SQL Server 对象资源管理器中,导航到数据库中的 Products 表。In SQL Server Object Explorer, navigate to the Products table in the database. 右键单击并选择“查看数据” ,以便检查新添加的行。Right-click and select View Data to examine the newly added rows.