Several ways for mobile to connect to the database

Source: Internet
Author: User

There are four connection methods:

1. Use WebService as an intermediary to access data. In PDA, you can directly access the Web service installed on the accesser.
The service can access the remote database. The simplest one is that the Web service can be a little slow in speed.

 

2. You can directly access. SQL Server2000 or SQL server2005. If you access the database, you can only use the previous method. Use sqlce to connect to the database.

 

3. Connect the PDA client to the server. the most troublesome and secure one. use SOCKET, network program, client installed on PDA, and use socket to connect to the server to transmit data on PDA. Of course, you have to write an accesser to enable the listener to accept client data, it is complicated and troublesome to send the data from data types to the client.

 

Fourth, the form of reading and writing files. Save the database information read from the accessors as files locally in the PDA. For example, XML or INI files

 

At the beginning, I remember trying to use the first method. Using WebService to connect to the database is simple, but sometimes it is still slow and not safe. therefore, the third method is used. A server is written on a fixed IP address, and the client transmits data through socket.

 

When I was idle, I tried how to connect with sqlce and tried the following simple code:

 

String connectionstring = @ "Data Source = mobilece. SDF ";

Sqlceconnection conobj = new sqlceconnection (connectionstring );

Conobj. open ();

Datatable dtpda = NULL;

Sqlcedataadapter dapda = new sqlcedataadapter ();

Dapda. selectcommand = conobj. createcommand ();

Dapda. selectcommand. commandtext = "select username, userpwd from login ";

Dtpda = new datatable ("login ");

Dapda. Fill (dtpda );

Conobj. Close ();

This. datagrid1.datasource = dtpda;

 

The prompt is that the database file is always not found (Prompt: first remember to add reference system. Data. sqlserverce and import the namespace using
System. Data. sqlserverce;). It is not possible to change the absolute path.

 

Finally, I found the connection string from the Internet and changed it to the following:

 

Sqlceconnection conobj = new sqlceconnection ("Data Source =" + (system. io. path. getdirectoryname (system. reflection. assembly. getexecutingassembly (). getname (). codebase) + "\ mobilece. SDF ;")));

 

The test connection is passed and the data in the database table is displayed.

 

If this path is used, you are prompted to add an existing item in the project and find the created SDF database file. to the project. In the solution, the newly added SDF file is displayed. In the debug file, you can also see this file. Do not copy this file, copying the file directly to the DEBUG directory cannot find the file.

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.