SoFunction
Updated on 2025-04-05

Remote access to the Access database using C#

 

1. Technical points

We all know that when Windows applications start a process, it always includes several threads. Communication between different processes is necessary to develop distributed applications. Traditionally, this requires not only a deep understanding of the objects of processes on both ends of the communication flow, but also a deep understanding of the hosts, application programming interfaces, and configuration tools of low-level protocols. In short, it is a complex task that requires a lot of expertise and experience.

Fortunately, .Net provides us with remote processing functions, and the communication methods it provides can quickly and conveniently complete the above-mentioned tasks of establishing communications. Therefore, whether it is necessary to develop web applications quickly or to spend more time generating critical enterprise-wide applications, the .NET Framework provides support. With .NET remote processing, client applications can use objects in other processes on the same computer or any other available computer on its network.

To create an application that allows two objects to communicate directly across the application using .NET remote processing, just generate the following objects:

1. Objects that can be processed remotely.

2. The application that listens for requests to the remote object is the server program.

3. The client application that makes a request to the remote object.

There are two ways to communicate objects in different applications under .Net: one is to transmit object copies across application domain boundaries, and the other is to exchange messages using a proxy. MarshalByRefObject is the base class of an object that communicates by using a proxy to exchange messages. When using remote objects across applications, the object's base class must be inherited from MarshalByRefObject.

2. Program implementation

(1) We first create a new solution named "TestRemoteAccess" in the VS IDE to accommodate the three projects used to implement remote processing mentioned above. First, add a class library called "RemoteObject" to the solution, and then change the default created class name to "CRemoteAccess", and inherit from "MarshalByRefObject". The code is as follows:

using System;
using ;
using ; 
namespace RemoteObject
{
 public class CRemoteAccess : MarshalByRefObject
 {}
}

 

We need to create all functions within this object for connecting and accessing the local Access database for the server client program to call at the same time. The methods used to connect and access the Access database will not be described in detail here, see the attachment source code.

First of all functions that need to be exposed to the client must be set to public. The variable m_ConnString needs to be set to public static. The purpose is to save the database connection string after the client calls SetRemoteAccessConnString to be accessible during this connection. The code is as follows:

……
public static string m_ConnString;
……
public void SetRemoteAccessConnString(string Connstr)
{
 m_ConnString = Connstr;
}
…… 

After successfully connecting to the Access database, we need to return the dataset to the requested client for display and editing. In the remote object, we declare several related functions:

private void LoadData(string SqlStr, string TableName)
public void SaveData(DataTable ClientDataTable)
public DataTable GetUserTable(string SqlStr, string TableName) 

The client can pass an SQL query script to get the data of the relevant database table by calling GetUserTable and return a DataTable. Then, the DataTable attached value to the DataGridView to display the data. GetUserTable completes the acquisition of data by calling the private LoadData function. The SaveData function is used to save the edited dataset back to the local Access database file. The code is as follows:

……
m_connection.Open();
m_adapter.Update(ClientDataTable);
……


(2) The remote object creation is completed, and we need to create a server application to listen for requests from the remote object. Create a new Windows Form project named "TestRemoteAccess" solution in the "TestRemoteAccess" solution, drag and drop several components from the toolbox, and the interface is as follows:


In addition to having the ability to remotely access objects, the server program TestServer is the main function of obtaining the actual Access database file path and setting the database connection string of the remote object. We must add references to remote objects and class libraries such as remote processing and network communication protocols. At the beginning of the server program startup, you need to create an instance of the remote object and register a communication port, and then call the method. The description of this method in MSDN is as follows: By initializing a new instance of WellKnownServiceTypeEntry with the given parameters, registering the object Type on the server as a known type. All customers who know the URI of the known object can obtain the proxy for the object. The so-called URI is the Uniform Resource Identifier. The code is as follows:


……
remotableObject = new ();
TcpChannel channel = new TcpChannel(8080);
(channel);
(typeof(), "RithiaTestAccessServer", );
……



After selecting the Access database file to be accessed, we need to call the SetRemoteAccessConnString method of the remote object. This method will save the connection string to connect the Access database file during this connection with the server program. The code is as follows:


……
ProviderStr = ProviderStrPart +  + ";Jet OLEDB:Database Password=" + ;
……
(ProviderStr);
……



(3) Finally, we create a client program for connecting and requesting services. It will obtain the relevant data set by calling the remote object registered by the server program TestServer and save the edited data back to the actual database file. Create a new Windows Form project named "TestClient" in the "TestRemoteAccess" solution. Drag and drop several components from the toolbox. The interface is as follows:

The client program needs to know the computer name or IP address on which the server program is running and the port number on which it is listening, then create an instance of the remote object and create a DataTable to receive the returned data. The code is as follows:

……
string RemoteURL;
Host = ;
Port = ;
RemoteURL = "tcp://" + Host + ":" + Port + "/RithiaTestAccessServer";
try
{
 TcpChannel chan = new TcpChannel();
 (chan);
 remoteObject = ()(typeof(), RemoteURL);
 RemoteDataTable = new DataTable();
  = false;
}
catch (Exception E)
{
 (());
}
finally
{
}
…… 

The client program registers the corresponding channel and port number based on whether the channel listened to by the service program is TCP or HTTP, and combines it into the Url of the remote object, that is, Url= Channel://hostname: port number/object Uri, and then creates an instance of the remote object. Just like using a local object, it can be accessed. We can call the GetUserTable method of the remote object to get the result set of the specified query script. The code is as follows:

……
RemoteDataTable = (, "Test");
 = RemoteDataTable;
…… 

When saving the result set, you only need to simply call the SaveData method, the code is as follows:

……
(RemoteDataTable);
…… 

3. Results

The program was successfully debugged and run under Visual Studio .Net 2005 and Windows XP SP2.
Source code download: /down/401452/q520525745