1. Introduction to the problem:
In general, when using a program to connect to a database, when the database server does not exist, or the database server does not start,
Causes the database connection to fail, which can take a long time. Therefore, before connecting, it is necessary to make a judgment on these, and then return to the User A
The correct message to reduce the waiting time.
2. The database server is not connected, and can be divided into the following situations:
1>. There is no database server at all, and the system does not have a database server installed at all.
2>. The system has a database server installed, but the database server is not turned on.
3>. The database server is turned on, but is in a "paused" or "stopped" state.
4>. The most common, of course, is the problem with connection information.
Workaround:
1. The simplest way to directly use the information provided by the exception, Delphi's exception, is to provide a perfect type of error.
For example:
Procedure Tform1.btn1click (Sender:tobject);
Begin
Try
Adocon1. Connected:=true;
Except
On E:exception do
ShowMessage (E.message)
End
End
Note: However, when the database service is "stopped" or "not installed", there is a time-out due to the connection database throwing exception.
So the user is not connected to a long time, so you can first determine whether the installation, and the database server is open (requires the following methods),
The exception is then to determine whether the server is in the "boot" state or "paused".
< Span style= "COLOR: #3333ff" >2. Determine whether the system has a SQL Server database installed through the registry.
uses Registry;
Procedure Tform1.btn1click (sender:tobject);
var
Aregistry:tregistry;
cdkey:string;
Begin
Aregistry: = tregistry.create;
Aregistry.rootkey: = HKEY_LOCAL_MACHINE;
Try
aregistry.openkey (' Software\Microsoft\Microsoft SQL server\80\registration ', False );
Cdkey: = aregistry.readstring (' Cd_key ');
If CdKey = ' then
showmessage (' SQL Server database not installed ')
Else
showmessage (' SQL Server database installed ')
except
Aregistry.destroy;
END;
End;
Note: <1> If the green version is installed, there is no information in the registry and this method cannot be used.
<2>: After installing SQL Server Uninstall, the information in the registry is not modified, so the judgment may be incorrect and the prompts are already installed.
3. Determine if the SQL Server database server is turned on by process
Uses TLHELP32;
Procedure Tform1.button1click (Sender:tobject);
Var
Processlist:thandle;
pe:tprocessentry32;
Proclist:tstringlist;
I:integer;
Begin
ProcList: = tstringlist.create;
Try
Processlist: = CreateToolhelp32Snapshot (th32cs_snapprocess,0);
Pe.dwsize: = sizeof (TPROCESSENTRY32);
If Process32First (PROCESSLIST,PE) Then
Begin
Proclist.add (Pe.szexefile);
While Process32Next (PROCESSLIST,PE) do
Proclist.add (Pe.szexefile);
End
For I: = 0 to Proclist.count-1 do
If proclist[i] = ' sqlservr.exe ' Then
ShowMessage (' SQL Server already run! ‘);
Finally
Proclist.free;
End
End
Note: The three states of the SQL Server server:
<1>. "Start": there is ' sqlservr.exe ' in the process to access the connection.
<2>. "Paused": there is ' sqlservr.exe ' in the process and cannot access the connection.
<3>. "Stop": There is no ' sqlservr.exe ' in the process and cannot access the connection.
The connection is not allowed under pause or stop.
Scm-action 1-slient 1-service MSSQLServer//Start
Scm-action 6-slient 1-service MSSQLServer//Stop
Delphi determines if SQL Server server is installed and enabled