Remote Access databases with C #

Source: Internet
Author: User
Tags requires access database

At present, the database server based desktop management programs and Web programs have been too many applications, especially the large number of network popularization, isolated database management system is not competent for distributed management applications, but the face of the existing desktop based on Access database applications we can not completely discard. We use. Net Remoting to encapsulate the behavior of connecting and accessing access as a remote object for other clients in the network to access the actual Access database by invoking the remote object. We use C # 2005 as the development language to achieve these functions.

First, technical points

As we all know, Windows applications start a process at run time that includes several threads, and communication between different processes is necessary to develop a distributed application that traditionally requires not only an in-depth understanding of the objects of processes on both sides of the communication stream, but also a deep understanding of the host of low-level protocols, Application programming interfaces and configuration tools. In short, it is a complex task that requires a great deal of expertise and experience.

Fortunately. NET provides us with the remote processing function, it provides the communication method can quickly and conveniently completes the above-mentioned establishment communication task. Therefore, the. NET Framework provides support whether it is necessary to quickly develop a WEB application or to spend more time building critical enterprise-wide applications. With. NET remoting, a client application can use an object from another process on the same computer or on any other computer that is available on its network.

To create an application that enables two objects to communicate directly across applications using. NET remoting, you simply generate the following objects:

1. Objects that can be processed remotely.

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

3, the client application that makes the request to the remote object.

. NET, there are two ways to communicate objects in different applications: one is to transfer object replicas across application domain boundaries, and one is to exchange messages using proxies. MarshalByRefObject is the base class for objects that communicate by using proxies to exchange messages. When a remote object is used across applications, the base class of the object must be inherited from MarshalByRefObject.

Second, the realization of the program

(1) First, we create a new solution named "Testremoteaccess" in the IDE of VS to accommodate the three projects described previously for remoting, first add the class library named "RemoteObject" to the solution, and then change the default created class name to " Cremoteaccess ", and inherits from" MarshalByRefObject ", the code is as follows:

using System;
using System.Collections.Generic;
using System.Text;
  namespace RemoteObject
{
 public class CRemoteAccess : MarshalByRefObject
 {}
}

We need to create all the functions within this object to connect to and access the local Access database for the server-side client program to call at the same time. Methods for connecting and accessing Access databases are no longer detailed here, see attachment source code.

First, all functions that need to be exposed to the client must have their visibility set to public. The variable m_connstring needs to be set to public static, in order to save the database connection string after the client invokes setremoteaccessconnstring for constant access during this connection, as follows:

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

After a successful connection to the Access database, we need to return the dataset to the requesting client for display and editing, and we declare several related functions in the remote object:

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

The client can pass the SQL query script to fetch data from the related database table by calling Getusertable, and return a DataTable, which can then be appended to DataGridView to display the data. Getusertable the acquisition of the data by calling the private LoadData function. The SaveData function is used to save the edited dataset back to the local Access database file with the following code:

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

(2) The remote object creation is complete and we need to create a server-side application that listens for the remote object request. In the Testremoteaccess solution, create a new Windows Form project named: "TestServer", dragging the next few components from the toolbox, the interface looks like this:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.