June 10, 2008 at 4:50 am
ok , if you confused i'll explain, i want when i close my application, the database which i work with detached from the server OK, So when i write the detach quiery for the database in the dipose method of the main form or after the Application.Run(new Form1()); ,, there was an exception says that the database under usage or still used, and that after closing all connections related to this database and closing all connections to the instance of sql server ,
so how can i fully close or make the database unusable to be allowed to detach it????????????????
YoU CaN't LoSe WhAt YoU NeVeR HaD;)
June 10, 2008 at 8:40 am
Please don't cross post. It just wastes people's time and fragments replies.
No replies to this thread please. Direct replies to:
http://www.sqlservercentral.com/Forums/Topic514284-145-1.aspx
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 11, 2008 at 3:55 am
Sorry man i just said that if i didn't get answer to this question in development forum, i can get it from backup forum , sorry again,,,,,,,,,, BUT if you don`t make me duplicate my question, why you didn`t answer me there, you just said "Why do you want to detach the database when you close the app? " and no one answer me So what shall i do except put my question in another forum :):):):)
YoU CaN't LoSe WhAt YoU NeVeR HaD;)
June 11, 2008 at 5:05 am
Be patient. We're not paid to answer questions here.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 12, 2008 at 5:42 am
Hi
Detaching a database requires exclusive access to the database. If the database that you want to detach is in use, before you can detach it, set the database to SINGLE_USER mode to obtain exclusive access.
For example, the following ALTER DATABASE statement obtains exclusive access to the AdventureWorks database after all current users disconnect from the database.
USE master;
ALTER DATABASE AdventureWorks
SET SINGLE_USER;
GO
Also, may I ask why you are detaching the database when activity has finished. It may be more appropriate to simply take the database offline?
Hope this helps.
John
June 12, 2008 at 6:03 am
for details follow this link :
http://www.sqlservercentral.com/Forums/Topic514284-145-1.aspx
YoU CaN't LoSe WhAt YoU NeVeR HaD;)
June 12, 2008 at 6:37 am
i try that but an exception message said :
"Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."
the code is :
string Mconnection = ConfigurationManager.ConnectionStrings["MotherConnection"].ConnectionString;
SqlConnection Mconn = new SqlConnection(Mconnection);
Mconn.Open();
SqlCommand Mcom = new SqlCommand("Alter database DigArch set single_user", Mconn);
Mcom.ExecuteNonQuery(); // here the exception
Mcom = new SqlCommand("Exec sp_detach_db 'DigArch', true", Mconn);
Mcom.ExecuteNonQuery();
Mconn.Close();
YoU CaN't LoSe WhAt YoU NeVeR HaD;)
June 12, 2008 at 7:34 am
I GET IT :):):):)
thank you BigJohn your answer is apart of the solution just you have to make it like this :
ALTER DATABASE DigArch SET SINGLE_USER WITH ROLLBACK IMMEDIATE; ALTER DATABASE DigArch SET MULTI_USER;
thank you again:)
YoU CaN't LoSe WhAt YoU NeVeR HaD;)
June 12, 2008 at 8:04 am
You are most welcome.
June 12, 2008 at 8:20 am
I'm thinking that your connection string had better be connecting you to some database other than the one you're detaching.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 12, 2008 at 9:38 am
yes, sure it connected to master database to be allowed to detach the database, but why if the connection has another job prevent another database to detached????
YoU CaN't LoSe WhAt YoU NeVeR HaD;)
June 12, 2008 at 2:08 pm
Good question... I've never seen that behavior before... Of course, it's been several revisions of SQL Server since I've actually had to write any GUI code.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 13, 2008 at 4:41 am
June 13, 2008 at 10:34 am
Security Issue my friend;)
YoU CaN't LoSe WhAt YoU NeVeR HaD;)
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply