Access SQL Server ce 3.5 on Windows Mobile (2)

Source: Internet
Author: User

This articleArticleThis section describes how to access a local database from a device. You can use sqlceresultset to access the database faster and more effectively.

 

Database Access is performed by using data sources. dataset and sqlceresultset are generally used.

Which one is better?

 

Dataset and sqlceresultset can both be used as data sources for data binding, or directly as a way to operate databases. What are the differences between them? Dataset objects have rich functions and can store data in different able tables. Each of them is composed of columns, and each row of data is described by datarow. Dataset can also be used to correspond to the data constraints of tables (whether it is empty or not). It can also describe the relationship between tables (corresponding to the primary and Foreign keys), similar to a relational database. It can save all the data from the database, save the changed values, and differentiate the data by the row status, and temporarily Save the data.

 

Sqlceresultset is quite different. It does not store data, but can directly read or update data. Therefore, it is a lightweight data access object.

 

Dataset applications can be used as distributedProgramOffline Access Program for data transmission.

When accessing a local database on a device with limited memory, using dataset wastes a lot of resources. At this time, sqlceresultset is useful. Sqlceresultset can be viewed as a combination of functions:DatasetThe updatability and rollabilitySqlcedatareaderSimilar performance.

 

Program Demonstration:

You can use get data to read data, and then use new, update, and delete data.

The effect is as follows:

A record with sysno 12 is added.

Select a row with sysno 6 and press update. Then, the qty and price of the row have changed.

Select a record with sysno 9 and press delete. This row is deleted.

 

The demo is simple,CodeIt is also very simple.

Select a local database, add a dataset to the project, and select the accessed table. On the project page, select the dataset XSD file, and on the properties page, change custom tool (custom tool) to msresultsetgenerator, so that the dataset will be changed to sqlceresultset, which is very convenient. The generated sqlceresultset is strongly typed.

 

In the form_load event, first instantiate the object and pass in the connection string and operation method.

1 Private   Void Form1_load ( Object Sender, eventargs E)
2 {
3 Orderset =   New Devicesqldemo2.dbresultsets. ordersresultset (connstring, resultsetoptions. scrollable | Resultsetoptions. Sensitive | Resultsetoptions. updatable );
4 }

 

When binding a data source, you only need to bind a data source with sqlceresultset and bind it to the control.

1 Bindingsource BS =   New Bindingsource ();
2 Orderset. BIND (BS );
3 This . Datagrid1.datasource = BS;

 

When adding a table, you only need to use the Add Table Name Record. When modifying and deleting a table, you must first use the readabsolute method to move the cursor to the corresponding row.

You only need to modify the attributes of the sqlcereusltset object, and then use the update method to update the database immediately.

The delete operation uses the delete method to delete the row where the cursor is located.

 

This article describes how to use sqlceresultset to provide program performance when operating a local database.

 

Download Code:Devicesqldemo2.rar

Development Environment: vs2008 + wm6.0

 

Author: appleseeker
Date: 2008-08-12

Related Article

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.