Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

DMO - detachDB problem

 
   Database Help (Home) -> Visual Basic RSS
Next:  Need to change connect string for Excel Queries u..  
Author Message
Elmo Watson3

External


Since: Oct 26, 2004
Posts: 4



(Msg. 1) Posted: Mon Feb 14, 2005 2:19 pm
Post subject: DMO - detachDB problem
Archived from groups: microsoft>public>vb>database (more info?)

I have the following code:
Set oServer = New SQLDMO.SQLServer
oServer.Connect (Servername), ServerID, ServerPwd
Set oDatabase = oServer("Master")
Call oServer.DetachDB(DBName, True)
oServer.Close

however, I continue getting the following error:
"Cannot detach the database 'Northwind' because it is currently in use"

Doing a detach with Enterprise manager works just fine on the same
server/db - - it's just that, with coding it, I get the error - -

I can restart the computer and go straight to this code - (not running any
other db programs to access the database) and it still does it - -

what might I be doing wrong?
(is there a way to kill any and all connections, before connecting and
detaching?)

 >> Stay informed about: DMO - detachDB problem 
Back to top
Login to vote
Andrea Montanari

External


Since: Sep 13, 2003
Posts: 498



(Msg. 2) Posted: Tue Feb 15, 2005 5:40 am
Post subject: Re: DMO - detachDB problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

hi Elmo,
Elmo Watson wrote:
 > I have the following code:
 > Set oServer = New SQLDMO.SQLServer
 > oServer.Connect (Servername), ServerID, ServerPwd
 > Set oDatabase = oServer("Master")
 > Call oServer.DetachDB(DBName, True)
 > oServer.Close
 >
 > however, I continue getting the following error:
 > "Cannot detach the database 'Northwind' because it is currently in
 > use"
 >
 > Doing a detach with Enterprise manager works just fine on the same
 > server/db - - it's just that, with coding it, I get the error - -
 >
 > I can restart the computer and go straight to this code - (not
 > running any other db programs to access the database) and it still
 > does it - -
 >
 > what might I be doing wrong?
 > (is there a way to kill any and all connections, before connecting and
 > detaching?)

if you really want to kill all active connections to the database to be
detached you can execute the Transact-SQL code SQL Server uses, that's to
say
sSql = Replace("DECLARE @command NVARCHAR(500)" & vbCrLf _
& "DECLARE @spid INT" & vbCrLf _
& "DECLARE active_spids CURSOR LOCAL" & vbCrLf _
& " FOR SELECT s.spid FROM master.dbo.sysprocesses s" & vbCrLf _
& " WHERE (s.dbid = (SELECT dbid FROM master.dbo.sysdatabases
WHERE (name = '#1#')))" & vbCrLf _
& "OPEN active_spids" & vbCrLf _
& "FETCH NEXT FROM active_spids INTO @spid" & vbCrLf _
& "WHILE (@@fetch_status = 0) BEGIN" & vbCrLf _
& " SET @command = 'KILL ' + CONVERT(VARCHAR(10), @spid)" & vbCrLf _
& " EXECUTE ( @command )" & vbCrLf _
& " FETCH NEXT FROM active_spids INTO @spid" & vbCrLf _
& "END" & vbCrLf _
& "DEALLOCATE active_spids", _
"#1#", sDBname)

On Local Error Resume Next
Set oQry = objServer.ExecuteWithResults(sSql, Len(sSql))
On Local Error GoTo 0
Set oQry = Nothing

but I do think you still have some active connection to the database, even
if you do not reference the Northwind database in your code... have a look
at sp_who, sp_who2 results and/or Process Info in Enterprise Manager,
Management->Current Activity node...
--
Andrea Montanari (Microsoft MVP - SQL Server)
<a rel="nofollow" style='text-decoration: none;' href="http://www.asql.biz/DbaMgr.shtm" target="_blank">http://www.asql.biz/DbaMgr.shtm</a> <a rel="nofollow" style='text-decoration: none;' href="http://italy.mvps.org" target="_blank">http://italy.mvps.org</a>
DbaMgr2k ver 0.10.0 - DbaMgr ver 0.56.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
--------- remove DMO to reply

 >> Stay informed about: DMO - detachDB problem 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
connected to mySQL using RDO,having a problem, - I have a problem,I have connected to mySql successfully. My problem is for example, when you log in it runs through this code fine and I get the correct results but....then when i log off and i log back in i get an error saying "Object invalid or no...

Access 2002 Automation problem with OpenReport - My VB 6 application needs to print a report in an Access 2002 database. This was working fine when I used Access 97 but now the application quits without any errors on the DoCmd.OpenReport line. I have my VB application references set to access 10.0. ....

sql query returns in wrong format - Hi, Hope someone can help me with this. I have MS SQL 2000 database where i have Table "PriceList" and there a column "Pricemk" wich is data type "money". All data in this column is in form "10001,35". So why when...

arrays - is there a way to concatenate the contents of an array (looping through the array) and storing each item in a comma delimited string? Thanks!

DataReport PageBreak - Hi everyone, I would like to know how to force pagebreak in a datareport for example after 5 records printed. Thank you all
   Database Help (Home) -> Visual Basic All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]