机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx

上传人:b****5 文档编号:21369025 上传时间:2023-01-30 格式:DOCX 页数:9 大小:21.39KB
下载 相关 举报
机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx_第1页
第1页 / 共9页
机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx_第2页
第2页 / 共9页
机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx_第3页
第3页 / 共9页
机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx_第4页
第4页 / 共9页
机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx

《机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx》由会员分享,可在线阅读,更多相关《机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx(9页珍藏版)》请在冰豆网上搜索。

机械毕业设计英文外文翻译24Visual Studio NET如何为并发控制生成SQL语句Word格式.docx

生成的SQL语句在哪里

并发与数据适配器(DataAdapter)

优化生成的SQL语句

并发与CommandBuilder对象

结论

任何可能同时被多个用户访问或修改数据的应用程序,都需要进行并发控制。

否则,一个用户更改记录时可能不经意的覆盖了其他用户的更改。

VisualStudio.NET的设计工具可以生成“保持所有值”方式的开放式并发SQL语句或生成“最后的更新生效”方式的SQL语句来更新数据。

这篇文章将解释:

●不同的SQL语句是如何生成的

●如何修改自动生成的SQL语句可以提高执行效率

你需要具备以下知识:

●基本的ADO.NET概念,包括数据集(DataSet)以及数据适配器(DataAdapters)。

更多信息请参见ADO.NET数据访问介绍(IntroductiontoDataAccesswithADO.NET)。

●数据并发机制以及会操作VisualStudio.NET。

更多内容请参见介绍ADO.NET中的数据并发(IntroductiontoDataConcurrencyinADO.NET)。

自动生成的SQL语句在哪里

自动生成的SQL语句在command对象的CommandText属性里。

在设计阶段配置DataAdapter对象时或使用CommandBuilder对象时SQL命令被自动生成。

更多信息,请参见并发与CommandBuilder对象(ConcurrencyandCommandBuilderObjects)。

配置DataAdapter对象

●从工具箱的数据选项卡中拖一个DataAdapter对象

●从服务器资源管理器拖一个数据表

●选中已有的DataAdapter对象,然后单击在属性窗口底部的“配置数据适配器”链接

CommandBuilder对象

●CommandBuilder对象在运行时刻被创建,更多信息请参阅SqlCommandBuilder或OleDbCommandBuilder。

并发控制与数据适配器(DataAdapter)

使用“数据适配器配置向导”配置数据适配器时,你可以选择是否使用开放式并发来生成Update和Delete语句。

一些思考和注意事项

●你的数据源必须有一个主键才能以开放式并发方式生成SQL语句

●当使用从“服务器资源管理器”拖放一个数据表的方式来创建DataAdapter对象时,DataAdapter对象自动生成基于开放式并发的Update和Delete语句。

如果你不想使用开放式并发,右击DataAdapter对象,从快捷菜单中选择“配置数据适配器”,然后在“高级SQL生成选项”对话框中清除“使用开放式并发”选项的选定。

向导则会重新创建不带并发检测的SQL语句。

●当重新配置现有的DataAdapter时,应注意“高级SQL生成选项”对话框里的选项已经全部恢复默认。

例如最初配置DataAdapter时没有选定“使用开放式并发”选项,但是当重新配置DataAdapter时,“使用开放式并发”选项却会被选定,即便你根本没有打开过“高级SQL生成选项”对话框。

●如果你在“数据适配器配置向导”的“选择查询类型”页面选择“使用现有的存储过程”,则“使用开放式并发”选项将不可用。

存储过程仍按其原来的方式执行。

如果想使用并发检测的话,必须将其包括到存储过程中、或在你的应用程序中编写相应的代码。

●当使用开放式并发来创建SQL命令时,不会对二进制数据列验证进行并发处理。

这将导致用这种方法对大的二进制记录集执行按位比较算法时的效率低下。

用向导生成SQL语句

为了理解VisualStudio.NET如何使用开放式并发来生成SQL语句,让我们来看看用“数据适配器配置向导”生成的Update语句。

我们将查看同一条语句在选择“使用开放式并发”选项和不选择“使用开放式并发”选项时的不同状态。

你会注意到,选择开放式并发与不选择开放式并发所生成SQL语句的区别只存在于Where子句上。

注:

以下的例子使用用“数据适配器配置向导”生成的Update语句,并从NorthWind示例数据库的Customers表中选择了若干列。

使用开放式并发的Update语句

这个例子使用了“数据适配器配置向导”的默认配置,即选中了“使用开放式并发”选项。

当使用开放式并发时,生成的command的参数集里还存在一个参数副本。

第二个参数集(带@Original_前缀的那个)保存了最初从数据源里读取的值。

检查Where子句发现,每一个生成的语句都要检测数据库当前的值是否等于最初读取的值(例,WHERECity=@Original_City)。

通过数据库中的每个字段与最初读取的值相比较,我们很容易确定是否同时有其他用户修改了某个字段。

如果Where子句不成立,就没有记录会被修改,与此同时还引发了一个“数据库并发”异常。

如果数据源的某个字段为空值(NULL),生成的SQL语句同样验证最初读取的记录是否也为空值。

UPDATECustomers

SETCustomerID=@CustomerID,CompanyName=@CompanyName,ContactName

=@ContactName,ContactTitle=@ContactTitle,City=@City

WHERE(CustomerID=@Original_CustomerID)AND(City=@Original_City

OR@Original_CityISNULLANDCityISNULL)AND(CompanyName=

@Original_CompanyName)AND(ContactName=@Original_ContactNameOR

@Original_ContactNameISNULLANDContactNameISNULL)AND(ContactTitle=

@Original_ContactTitleOR@Original_ContactTitleISNULLAND

ContactTitleISNULL);

SELECTCustomerID,CompanyName,ContactName,ContactTitle,City

FROMCustomersWHERE(CustomerID=@CustomerID)

不使用开放式并发的Update语句

这个例子更改了“数据适配器配置向导”的高级选项,没有选中“使用开放式并发”选项。

以下的语句表明:

只要数据库中一条记录满足CustomerID=@Original_CustomerID,则所有的字段都会被更新。

不管这条记录现在是什么样的值,它都将被设置为通过SQL语句传递到数据源的值。

在这里没有任何关于并发的检测,也无法得知是否同时有其它用户在更改这条记录。

这种方式称为“最后的更新生效”方式。

无论以前对这条记录进行过什么样的修改,更新操作都会执行。

SETCustomerID=@CustomerID,CompanyName=@CompanyName,

ContactName=@ContactName,ContactTitle=@ContactTitle,City=@City

WHERE(CustomerID=@Original_CustomerID);

VisualStudio.NET生成“保持所有值”方式的SQL语句来实现开放式并发。

虽然这可能没有生成最高效的SQL语句,但是它的却生成了可以对数据源所有列(包括主键)进行并发检测的SQL语句。

使用“保持所有值”方式实现开放式并发,当执行效率非常低下时,你可以手工修改生成的SQL语句以使它们不检查数据源的所有列。

最常见的方式是使用时间戳或版本号字段。

如果你的数据源包含一个每次修改记录时都会更新的时间戳字段,你只需要验证数据源中的时间戳和你程序中的时间戳二者是否匹配,就可以知道是否同时有其他用户修改了记录。

下面这条SQL语句使用检查时间戳模式。

 

这个例子假设数据库已经设置了时间戳字段

UPDATECustomersSETCustomerID=@CustomerID,CompanyName=@CompanyName,

WHERE(CustomerID=@Original_CustomerID)AND

(TimeStamp=@Original_TimeStamp);

SELECTCustomerID,CompanyName,ContactName,ContactTitle,City,

TimeStampFROMCustomersWHERE(CustomerID=@CustomerID)

当应用程序使用SqlCommandBuilder或者OleDbCommandBuilder时,生成的Update和Delete语句的CommandText属性被自动的以开放式并发方式创建。

如果你不想用开放式并发,则可以通过修改DataAdapter对象的Update和Delete命令的CommandText属性来实现。

更多内容,请参见OleDbCommand.CommandText属性或SqlCommand.CommandText属性。

当使用开放式并发的“保存所有值”方法时,SQL语句在设计时由VisualStudio.NET的设计工具自动生成或在运行时由CommandBuilde自动生成。

它把数据库所有字段(包括主键)当前值与初始值进行比较,这可能不是一种最高效的方式。

如果你的数据使用版本号或者时间戳方式控制并发,则可以通过修改生成的SQL语句来获取更高效的执行。

英文原文

VisualStudio.NETTechnicalArticles

HowVisualStudio.NETGeneratesSQLStatementsforConcurrencyControl

February2002

Summary:

ThispaperexaminestheSQLstatementsVisualStudio®

.NETgeneratesfordifferentkindsofconcurrencycontrol,howtomodifythemforbetterperformance,andhowtogenerateastatementthatdoesnotuseconcurrencycontrol.(5printedpages)

Contents

Introduction

Prerequisites

WhereAretheSQLStatements?

ConcurrencyandDataAdapters

OptimizingtheGeneratedSQLStatement

ConcurrencyandCommand-BuilderObjects

Conclusion

Introduction

Anyapplicationthatmighthavemultipleuserssimultaneouslyattemptingtoaccessandmodifydataneedssomeformofconcurrencycontrol.Otherwise,oneuser'

schangescouldinadvertentlyoverwritemodificationsfromotherusers.ThedesigntoolsinVisualStudio.NETcancreateSQLstatementsthatusethe"

checkallvalues"

approachtooptimisticconcurrencyorthe"

last-inwins"

approachtoupdatingdata.Thispaperwillexplain:

∙Howeachofthesestatementtypesaregenerated.

∙HowtomodifythegeneratedSQLstatementforbetterperformance.

Youshouldhaveanunderstandingof:

∙FundamentalADO.NETdataconcepts,includingdatasetsanddataadapters.Formoreinformation,seeIntroductiontoDataAccesswithADO.NET.

∙ConcurrencycontrolbasicsandtheoptionsavailableinVisualStudio.NET.Formoreinformation,seeIntroductiontoDataConcurrencyinADO.NET.

SQLstatementsarelocatedintheCommandTextpropertyofcommandobjects.SQLcommandsareautomaticallygeneratedatdesigntimewhenconfiguringdataadapters,andatruntimewhenusingcommandbuilderobjects.Formoreinformation,seeConcurrencyandCommandBuilderObjects.

ConfiguringDataAdapters

∙DragadataadapterfromtheDatataboftheToolbox

∙DragatablefromServerExplorer

∙Modifyinganexistingadapter,byselectingadataadapterandclickingtheConfigureDataAdapterlinkatthebottomofthePropertieswindow.

CommandBuilderobjects

∙Commandbuilderobjectsarecreatedprogrammaticallyatruntime.Formoreinformation,see(SqlCommandBuilderorOleDbCommandBuilder)

WhenconfiguringdataadapterswiththeDataAdapterConfigurationWizard,youcandecidewhethertouseoptimisticconcurrencyforthegeneratedUpdateandDeletestatements.

ConsiderationsandCaveats

∙YourdatasourcemusthaveaprimarykeyinorderfortheSQLstatementstobegeneratedtouseoptimisticconcurrency.

∙WhencreatingdataadaptersbydraggingtablesfromServerExplorer,thedataadaptercreatesUpdateandDeletestatementsthatareautomaticallyconfiguredforoptimisticconcurrency.Ifyoudonotwanttouseoptimisticconcurrency,youcanreconfigurethedataadapter:

Right-clicktheadapterandselectConfigureDataAdapterfromtheshortcutmenu,thencleartheUseoptimisticconcurrencyoptionoftheAdvancedSQLGenerationOptionsDialogBox.Thewizardwillrecreatethestatementswithouttheadditionalcodetocheckforconcurrencyviolations.

∙Whenreconfiguringanexistingdataadapter,notethattheadvancedsettingsallreverttotheirdefaultstate.Forexample,ifyouclearedtheUseoptimisticconcurrencyoptionwhentheadapterwasoriginallyconfigured,itwillautomaticallybeselectedifyoureconfigureit,evenifyoudonotaccesstheAdvancedSQLGenerationOptionsdialogbox.

∙IfyouselecttheUseexistingstoredproceduresoptionintheChooseaQueryTypesectionoftheDataAdapterConfigurationWizard,theoptiontouseoptimisticconcurrencyisnotavailable.Thestoredprocedureswillexecuteasis,andanydesiredconcurrencycheckingmustbedonewithinthestoredprocedure,orprogrammaticallybuiltintoyourapplication.

∙Whencommandsaregeneratedtouseoptimisticconcurrency,noverificationwillbeperformedonbinarycolumnstodeterminewhetherconcurrentchangeshavebeenmade.Theresourcestoperformabit-by-bitcomparisonofalargebinaryrecordwouldbeextremelyinefficient.

SQLStatementsGeneratedbytheWizard

TounderstandhowVisualStudio.NETconstructsSQLstatementsthatuseoptimisticconcurrency,letusinspecttheUpdatestatementgeneratedbytheDataAdapterConfigurationWizard.WewilllookatthesamestatementgeneratedbothwithandwithouttheUseoptimisticconcurrencyoptionselectedintheAdvancedSQLGenerationOptionsdialogboxofthewizard.

YouwillnoticethedifferencesbetweenstatementsthateitheruseoptimisticconcurrencyornotarelocatedintheWhereclause.

Note 

ThefollowingexamplesusetheUpdatecommandthatisgeneratedbyrunningtheDataAdapterConfigurationWizard,andselectingseveralcolumnsfromtheCustomerstableintheNorthwindsampledatabase.

UpdateStatementUsingOptimisticConcurrency

ThisexampleusesthedefaultsettingsoftheDataAdapterConfigurationWizard,whichhastheUseoptimisticconcurrencyoptionselected.

Whenusingoptimisticconcurrency,thecommandsaregeneratedwithasecondsetofparam

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 少儿英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1