MSSQL数据库不能手动创建新的连接

MSSQL数据库不能手动创建新的连接

相信在使用MSSQL数据库下使用事务回滚方式操作多表记录的时候,会经常出现“不能在手动或分布事务方式下创建新的连接”的出错提示信息,这个问题也已困扰我多年。

这次在开发一个大型的商务平台的时候,涉及到数据的计算,同时也必须要多表更新(或删除)。借助GOOGLE也没找到一个真能解决的问题。以前收集过一个MSDN的说明,官方的提示必须,只有用SQL语句执行数据库操作才能使用事务处理。

从官方提示上理解,事式处理中涉及到查询(Select)时,会出现这种出错提示。同时也应该与记录指针(Cursors)有关联。试着这样的思路,将事务处理中原出现的Conn.Execute("select...from...")修改为使用rs.Open...命令打开记录集,问题解决。

MSDN说明:

TipsforWorkingwithCursors

Someproviders,suchasSQLServer,implementaforward-scrolling,read-on ly(or’firehose’)cursormode,meaningth attheycanefficientlyretrievedat abykeepingaconnectionopen.Whenworkingwithsuchproviders,theconnectionc ouldbeblockedbyanotheruser’stransaction.Thefollowingexamplesdemonstr atescenarios

thatresultinerrors.

dbConn.Open"DSN=SQLForum;UID=sa;PWD=;"

’Example1

dbConn.BeginTrans

RS.Open"Select*FROMMessage",dbConn

SetdbCmd.ActiveConnection=dbConn

Example1:Theproblemisthatthecommandobject’sActiveConnectionisbei ngsettoaconnectionthatisforward-scrollingandin’firehose’mode.Thisis thesameconnectioninvolvedinthebatchmode.Theerrorfromtheproviderwillon lyappearintheErrobject,anditwillreturnasunspecified.Forexample,withth eODBCProvider,youwillget"Unspecifiederror".

dbConn.Open"DSN=SQLForum;UID=sa;PWD=;"

’Example2

RS.Open"Select*FROMMessage",dbConn

dbConn.BeginTrans

Example2:Theproblemhereisthattheconnectionisforward-scrollingandi nfirehosemode,soitcannotbeputintotransactionmode.Theerrorreturnedinth eErrorscollectionfromtheproviderwillindicatethatitisoperatinginfireho semode,andcan’tworkintransactionmode.Forexample,withtheODBCProvidera gainst

MicrosoftSQLServer,youwillgettheerror"Cannotstarttransactionwhile infirehosemode".

dbConn.Open"DSN=SQLForum;UID=sa;PWD=;"

’Example3

RS.Open"Select*FROMMessage",dbConn

SetdbCmd.ActiveConnection=dbConn

dbConn.BeginTrans

Example3:Theproblemhereisthattheconnectionisinforward-scrollingfi rehosemode,soitcannotalsobeinvolvedinabatchmode.Theerrorreturnedinthe Errorscollectionfromtheproviderwillindicatethatthetransactioncouldnot bestarted.Forexample,withtheODBCProvideragainstMicrosoftSQLServer,you willgettheerror"Cannotstarttransactionbecausemorethanonehdbcisinuse".

相关文档
最新文档