473,762 Members | 7,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ programmaticall y reconnection to SQL Server 2000 after stop/restart SQL service

2 New Member
I have write a program using MS Visual studio C++ 7.0 (platform Windows XP professional). I'm not using .NET.
This program save data in a SQL server 2000 database using ADO.
Everything works correctly, yet
if i stop the sql service and then restart it, while the programs write records, the reconnection to the server dont't work.

When the service stops i catch all the exceptions. I want try to reconnect to the database continually till the service restart and the connection reestablish.

I use connection, recordset and command object to execute an SQL statement (UPDATE MyTable SET filed1=value1 etc..

After the record write failure i call the method to close the connection. Even if the service is stopped this function don't generate an exception. Later when i call the ReconnectDataba se function i detect that the connection state is down and i try to reconnect to the database.

The reconnection will be reestablish correctly.

If i call BeginTransactio n/RollbackTransac tion/CommitTransacti on when i write the record i don't succed to close the connection after the record writing failure. The Close() function generate an exception. If i read the connection state it is ON even if it is not true.
I have tried to rewrite my ReconnectDataba se function but i don't succed to reconnect to the database even if the service has been restared.

This is the code to write the record:
Expand|Select|Wrap|Line Numbers
  1.  
  2.     try
  3.     {
  4.         pConnenction->BeginTrans();
  5.     }
  6.     catch(_com_error &e) 
  7.     {
  8.         .......        
  9.     }
  10.  
  11.  
  12.     // CADOCommand is my wrapper class around ADO object
  13.     CADOCommand cmd(pDatabase, cmdString, adCmdText);
  14.     if (myRecordset.Execute(&cmd) == FALSE) {
  15.  
  16.         try
  17.         {    
  18.             pConnection->RollbackTrans();
  19.         }
  20.         catch(_com_error &e) 
  21.         {
  22.              .........
  23.         }
  24.  
  25.         // When the service is stop this function generate an exception only if i 
  26.         // have previously call  BeginTrans/Rollback.
  27.         // I can catch this exception but my ReconnectionDatabase() function 
  28.         // (see later) don't succed to reconnect to the database
  29.         rsMachines.GetActiveConnection()->Close();
  30.  
  31.         return FALSE;
  32.  
  33.     }
  34.  
  35.     try
  36.     {
  37.         pConnection->CommitTransaction();
  38.     }
  39.     catch(_com_error &e) 
  40.     {
  41.         ...........
  42.     }
  43.  
  44.  
This is the function that i use to try to reconnect to the database:

Expand|Select|Wrap|Line Numbers
  1. BOOL CDbase::ReconnectDatabase(_ConnectionPtr pConnectionPtr)
  2. {
  3.     long state;
  4.     _bstr_t strConnection;
  5.  
  6.     state = pConnectionPtr->GetState(); 
  7.  
  8.     if (state == 0) {    
  9.  
  10.         strConnection = (_bstr_t) pDatabase->GetConnectionString();
  11.  
  12.  
  13.         pConnectionPtr->CursorLocation = ADODB::adUseClient;
  14.  
  15.         try {
  16.  
  17.                  pConnectionPtr->Open(strConnection,_bstr_t(L""),_bstr_t 
  18.                                                             (L""),ADODB::adModeUnknown);
  19.         }
  20.         catch(_com_error& e) {
  21.  
  22.  
  23.                 return FALSE;
  24.         }
  25.     }
  26.  
  27.     return TRUE;
  28. }
  29.  
I have tryed to close/open the database
I have tryed to delete and new all the object and then reopen the database.
If the sql service has been stopped and then restarted I only have exception.
Only if i don't use recovery the reconnection restart but i need recovery.


I have the same problem even when i disconnect/reconnect the network cable using a remote database.

Thanks for every kind of suggestion.

Emanuele.
Oct 11 '06 #1
0 2206

Sign in to post your reply or Sign up for a free account.

Similar topics

4
15895
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group : microsoft.public.dotnet.languages.csharp URL :...
2
3451
by: John Spiegel | last post by:
Hi all, Is there a way to programmatically restart Windows (2000, specifically), login automatically then start an application? I've been running across some Windows Shell options (which further examples would be appreciated) but would particularly like to find a .NET-specific example. TIA, John
9
647
by: Steve Buster | last post by:
All right, I have read every forum, newsgroup etc about this issue and no one seems to know how to fix it. I am getting a "Server Application Unavailable" exception running my .NET 1.1 application. I use W2K SP4 and have applied the hot Fix KB824146 and KB824105, both IE fixes. I don't have VS installed because this is a Quality Environment. I do have .NET SDK and .NET 1.1 Runtime installed. I can't change my APSNET user to run as...
2
3533
by: Tedmond | last post by:
Dear all, I wrote a service by C# but I found the service is not quite stable. I want to add some code that refresh or restart the service itself for a fixed period. Does anyone know how to programmatically restart a service itself? Or I need a separate process to restart my service? Thanks for any help. Tedmond
7
7231
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch this error. I want to restart my windows service every time the COM object throws an error. I use System.ServiceProcess.ServiceController to stop and start my service. But there is one thing I do not understand:
2
6965
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
0
1621
by: Emanuele | last post by:
I have write a program using MS Visual studio C++ 7.0 (platform Windows XP professional). I'm not using .NET. This program save data in a SQL server 2000 database using ADO. Everything works correctly, yet if i stop the sql service and then restart it, while the programs write records, the reconnection to the server dont't work. When the service stops i catch all the exceptions. I want try to reconnect to the database continually till...
1
5123
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from the control unit and store them into the Sql server - The operation console then retrieve this data from the sql for reporting and statistics purposes - I am using ODBC connection - The problem is that the operation console is not able to...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.