Access the Remote Access Database AllenTao (blog. csdn. netallentao) This article source code download visit remote database commonly used way is to use the large database comes with client tools, such as SQLServer and Oracle with such tools. For small databases such as Access
Use the http://blog.csdn.net/allentao/ to Access the Remote Access Database Allen Tao () This article source code download visit remote database commonly used way is to use the large database comes with the client tool, such as SQL Server and Oracle. For small databases such as Access
Access the Remote Access database with the command of "MIDAS"
Allen Tao (http://blog.csdn.net/allentao)
2005-5-3
Download the source code of this Article
A common method to access remote databases is to use client tools provided by large databases, such as SQL Server and Oracle. This method cannot be used for small databases such as Access, because Access does not provide such client tools. However, we can Access the Remote Access database through the MIDAS.
MIDAS is a three-tier architecture solution proposed by Delphi. The basic idea of the method I provided to Access a remote Access database using the "das" command is to establish a data module on the server side for remote Access, and establish a connection with the client through the "das" component, then, the client sends the SQL statement to the server, and the server returns the dataset to the client after execution.
The specific practices are as follows:
Server
The server uses the ADO control to Access the Access database. The ADOConnection control establishes a connection and the ADOQuery Performs related operations. Use the ClientDataSet control of Data Access to return a dataset to the client. Place the preceding controls in the Remote Data Module container for the client to connect.
Perform the following steps:
1. Create an Application. The Form name is FormServer, and the Project name is PrjServer;
2. File à New à Other ..., In the New Items dialog box that appears, select the Multitier page, select the Remote Data Module, and click OK;
3. in the pop-up Remote Data Module Wizard dialog box, fill in AccessServer in the coclass column. The Instancing and Threading models are kept by default, indicating that different threads are independent of each other, after confirmation, a derived class AccessServer of RemoteDataModule is generated. (You can View the detailed information of this class in View à Type Library );
4. create a folder named data in the folder where the program is located, and put a folder named my. mdb's Access database file, where there is a table named "person", which has only two fields: name and age, both of which are of the text type;
5. Place various controls on the AccessServer of the remote data module. The final effect is shown in:
The following table lists the attributes and functions of each control:
Control Type |
Page |
Attribute |
Value |
Function |
ADOConnection |
ADO |
Name |
ADOConnection1 |
Connect to the Access File |
ConnectionString |
Provider = Microsoft. Jet. OLEDB.4.0; Data Source =./data/my. mdb; Persist Security Info = False |
ADOQuery |
ADO |
Name |
ADOQuery1 |
Perform related query operations |
Connection |
ADOConnection1 |
ClientDataSet |
Data Access |
Name |
DataSetProvider1 |
Provide data interfaces to clients |
DataSet |
ADOQuery1 |
Options. poAllowCommandText |
True |
6. Compile and run the server program. If you run the program, you have registered a server named "MIDAS". Only when the program runs on the server can you develop the program for the next client.
Client
Connect to the server in DCOM mode,
1. Create an Application. The Form name is FormClient, and the Project name is PrjClient;
2. Place various controls on the Form. The final effect is shown in:
The following table lists the attributes and functions of each control:
Control Type |
Page |
Attribute |
Value |
Function |
Edit |
Stardard |
Name |
TxtQuery |
Receive query statements |
Text |
Select * from person |
Button |
Stardard |
Name |
BtnQuery |
|
DCOMConnection |
DataSnap |
Name |
DCOMConnection1 |
Connect to the server through DCOM |
ServerName |
PrjServer. AccessServer |
ClientDataSet |
Data Access |
Name |
ClientDataSet1 |
Issue SQL statements to the remote end and obtain the dataset |
RemoteServer |
DCOMConnection1 |
ProviderName |
DataSetProvider1 |
DataSource |
Data Access |
Name |
Performance1 |
Data source as a data presentation Control |
DataSet |
ClientDataSet1 |
DBGrid |
Data Controls |
Name |
DBGrid1 |
Show datasets retrieved from the remote end |
DataSource |
Performance1 |
Note:
L The ServerName attribute of DCOMConnection1 is selected from the list. This list is a list of all registered servers.
L The ProviderName attribute of ClientDataSet1 specifies a control on the server.
3. Double-Click the btnQuery button on Form and add the following code under its Click handler:
Self. ClientDataSet1.Close;
Self. ClientDataSet1.CommandText: Invalid self.txt Query. Text;
Self. ClientDataSet1.Open;
Note:
If the returned SQL statement is not returned, change the last statement from Open to Execute.
After running, the effect is shown in:
Change Client Connection Mode
In the above example, we use DCOM to connect to the server, which can only be used within the LAN. If you want to use it within the Wan, you can use Socket to connect to the server. To do this, you need to make the following changes:
1. Run the scktsrvr.exe program of delphiself on the server. The program is in the Borland/Delphi7/Bin/folder.
2. on the client side, replace the DCOMConnection control with the SocketConnection control. The following table lists the attributes of the changes:
Control Type |
Page |
Attribute |
Value |
Function |
SocketConnection |
DataSnap |
Name |
SocketConnection1 |
Connect to the server through Socket |
Address |
127.0.0.1 (local) |
ServerName |
PrjServer. AccessServer |
ClientDataSet |
Data Access |
RemoteServer |
SocketConnection1 |
Issue SQL statements to the remote end and obtain the dataset |
Note:
The default port of SocketConnection is 211, which is also the default port of scktsrvr. To this end, open the port on the server firewall.