如何:使用 Power Buffer 更新连接的数据库How to: Update a Connected Database with Power Buffer

SQL Server Data Tools Power Buffer 技术可通过存储你在当前会话中所做的所有编辑工作,方便你将更改应用于连接的数据库。SQL Server Data Tools Power Buffer technology makes it easy for you to apply changes to your connected database by storing all your edits in the current session. 由于在 Power Buffer 窗口(在 Transact-SQL 编辑器或表设计器中)中进行编辑所导致的任何错误都将立即显示在“错误列表”窗格中,这使你可以按照这些标识的错误来进一步进行故障排除。Any errors caused by editing in Power Buffer window (in either the Transact-SQL Editor or Table Designer) immediately show up in the Error List pane, which enables you to follow the errors identified for further troubleshooting. 您可以验证挂起的更改,直到可供将其应用于您的数据库。You can verify your pending changes until you are ready to apply them to your database. 在更新过程中,SSDT 会基于您的编辑自动创建 ALTER 脚本,并提醒您任何潜在的问题。During the update process, SSDT automatically creates an ALTER script based on your edits, and alerts you of any potential issues. 然后,您可以将在所有打开的 Power Buffer 窗口上累积的所有更改应用于相同的数据库,或者保存该 ALTER 脚本以便在以后部署。You can then apply all the changes that have accumulated across all open Power Buffer windows to the same database, or save the ALTER script to be deployed later.

SSDT 还知道您在 Visual Studio 外对您的数据库架构所做的任何更改。SSDT is also aware of any changes made to your database schema outside Visual Studio. 例如,如果你将一个新表添加到 SQL Server Management Studio 中的现有数据库,则此类更改会立即显示在 Visual Studio 的“SQL Server 对象资源 管理器”中,而无需手动刷新它。For example, if you add a new table to an existing database in SQL Server Management Studio, such change will immediately show up in the SQL Server Object Explorer in Visual Studio without manually refreshing it. 偏差检测功能可确保你始终在“SQL Server 对象资源管理器”中查看数据库的最新架构定义。The drift detection feature ensures that you are always viewing the latest schema definition of a database in SQL Server Object Explorer. 请注意,在表设计器或 Transact-SQL 编辑器中打开的供编辑的任何数据库对象都将不会刷新以显示 Visual Studio 外的更改。Notice that any database objects opened in Table Designer or Transact-SQL Editor for editing will not be refreshed to show changes outside Visual Studio.

下面的过程利用在连接的数据库开发一节的前面的过程中创建的实体。The following procedures utilize entities created in previous procedures in the Connected Database Development section.

应用在之前的过程中进行的更改To apply the changes made in the previous procedures

  1. 单击工具栏上的绿色“更新”按钮(如果将鼠标指针悬停在该按钮上,则会显示“更新数据库”工具提示)。Click the green Update button on the toolbar ("Update Database" tooltip is displayed if you hover over the button). 工具栏位于表设计器的列网格上方。The toolbar is above the Columns Grid of the Table Designer.

  2. 此时将出现“预览数据库更新”对话框。The Preview Database Updates dialog box appears. 基于您的更改的部署脚本将在后台生成。A deployment script based on your changes is generated in the background. 然后,该对话框将显示 SSDT 将要执行的操作的摘要(例如,创建或删除数据库实体),同时还将显示已发现的潜在问题(这不适用于我们的过程,但在您的数据库定义包含在解决前阻止更新操作运行的错误时会给您带来便利)。The dialog box then shows a summary of the actions SSDT is going to take (e.g., creating or dropping database entities), together with potential issues it has identified (this is not applicable to our procedure, but will come in handy when your database definition contains errors that prevent an update action until resolved).

  3. 如果此时不想更新数据库,则单击“取消”按钮退出“预览数据库更新”对话框。If you do not want to update the database at this moment, click the Cancel button to exit the Preview Database Updates dialog box.

  4. 如果对截止到目前所做的更改满意,则在“预览数据库更新”对话框中单击“更新数据库”按钮。If you are comfortable with the changes so far, click the Update Database button in the Preview Database Updates dialog box. 该部署脚本将代表您来执行,并且您的累积更改现在将应用于数据库。The deployment script is executed on your behalf, and your accumulated changes are now applied to the database.

  5. 如果要查看部署脚本以便进行验证,或者要在更新前进行某些更改,则在“预览数据库更新”对话框中单击“生成脚本”按钮。If you want to view the deployment script to verify or make some changes before updating, click the Generate Script button in the Preview Database Updates dialog box. 生成的脚本将会在一个新的 Transact-SQL 编辑器窗口中打开。The generated script will open in a new Transact-SQL editor window. 可以按 Transact-SQL 编辑器工具栏中的“执行查询”按钮以便运行此查询。You can press the Execute Query button in the Transact-SQL editor toolbar to run this query. 这类似于在步骤 4 中按下“更新数据库”按钮所完成的工作。This is similar to what the Update Database button has done for you in Step 4.

    警告

    如果您对部署脚本进行任何更改并且执行该脚本,则此类更改将不会显示在任何打开的数据库实体中。If you make any changes to the deployment script and execute it, such changes will not show up in any opened database entities. 例如,如果在部署脚本中重命名 Customers 表的某一列并执行该脚本以便更新数据库,并且如果 Customers 表在表设计器中打开,则在单击“更新数据库”按钮时,该列名仍将是旧名称。For example, if you rename a column of the Customers table in the deployment script and execute it to update the database, and if the Customers table is opened in the Table Designer, the column name will still be the old one when you clicked the Update Database button. 您必须手动关闭表设计器并且不在本地将其保存为脚本。You have to manually close the Table Designer without saving it locally as a script. 在从“SQL Server 对象资源管理器”中重新打开表时,将注意到该数据库实际上已用你在部署脚本中进行的更改更新。When you reopen the table from SQL Server Object Explorer, you will notice that the database has actually been updated with the changes you made in the deployment script.

  6. 在 Transact-SQL 编辑器的“输出”窗格(如果正在自己执行部署脚本,也可以是“消息”窗格)中,请注意指示更新成功的以下内容。In the Output pane of the Transact-SQL editor (or the Message pane if you are executing the deployment script yourself), notice the followings which indicate that the update is successful.

正在创建 [dbo].[Customers]...正在创建 [dbo].[Products]...正在创建 [dbo].[Suppliers]...正在创建 FK_Products_SupplierId...正在创建 FK_Products_CustomerId...正在创建 CK_Products_ShelfLife 数据库更新的事务部分成功。根据新创建的 constraintsUpdate 检查现有数据完成。Creating [dbo].[Customers]...Creating [dbo].[Products]...Creating [dbo].[Suppliers]...Creating FK_Products_SupplierId...Creating FK_Products_CustomerId...Creating CK_Products_ShelfLife The transacted portion of the database update succeeded.Checking existing data against newly created constraintsUpdate complete.

  1. 在“SQL Server 对象资源管理器”中,注意新表已显示在“Trade”数据库的“表”节点下。In SQL Server Object Explorer, notice that the new tables have shown up under the Tables node of the Trade database.

查看对 Visual Studio 外的数据库进行的更改To view changes made to a database outside Visual Studio

  1. 打开 SQL Server Management Studio。Open SQL Server Management Studio. 在“连接到服务器”对话框中,输入你已连接到 Visual Studio 中的相同数据库服务器,然后单击“连接”。In the Connect to Server dialog box, enter the same database server that you have been connected to in Visual Studio and click Connect.

  2. 在“SQL Server 对象资源管理器”中,展开“数据库”,然后导航到“Trade”数据库。In SQL Server Object Explorer, expand Databases and navigate to the Trade database.

  3. 右键单击“Trade”下的“表”,然后选择“新建表”。Right-click Tables under Trade and select New Table. 在表设计器中,输入 id 作为列名称,输入 int 作为数据类型。In the Table Designer, enter id as the Column Name and int as the Data Type.

  4. 在工具栏中单击“保存”图标以便保存该表。Click the Save icon in the toolbar to save the table. 接受默认名称,然后单击“确定”。Accept the default name and click OK.

    返回 Visual Studio。Go back to Visual Studio. 在“SQL Server 对象资源管理器”中查看“Trade”数据库之下的“表”节点。Examine the Tables node under the Trade database in SQL Server Object Explorer. 请注意新创建的“Table_1”表的外观。Notice the appearance of the newly created Table_1 table.

  5. 右键单击“Table_1”并选择“删除”。Right-click Table_1 and select Delete. 在“预览数据库更新”对话框中,单击“更新数据库”。Click Update Database in the Preview Database Updates dialog box.

另请参阅See Also

如何:修复错误How to: Fix Errors