Build a well-laid Windows program

Source: Internet
Author: User

DataSet object

When the application needs to query the data, it can use the DataReader object to read the data, and DataReader can only read one row of data at a time to memory. And keep it connected to the database all the time. It is also read-only read-only method. It is troublesome for the program to try to get the data read again. With datasets, we can manipulate data by disconnecting from a database and manipulating data from multiple identical or different data sources

1.1 Understanding DataSet Objects

We can simply understand the data set as a temporary database, he put the application needs of the data temporarily in memory, because the data are cached on the local machine, there is no need to keep the database connection. When the application needs data, it reads the data directly from the in-memory data set. You can also modify the data in the dataset and then commit the modified data to the database.

Datasets do not interact directly with the database, and the interaction between it and the database is passed. NET data provider, so the dataset is independent of any database.

1.2 Creating a DataSet

Dateset in the System.Data namespace, create the dataset syntax as follows

[Syntax]:

DataSet DataSet object = New DataSet ("Name string for DataSet");

The parameter in the method is the name of the dataset, either Yes or no, and if there are no write arguments, the name of the created dataset defaults to NewDataSet.

For example:

DataSet myDataSet = new DataSet ();

DataSet myDataSet = new DataSet ("MySchool");

2 DataAdapter Objects

Bridge of data adapter database and DataSet object connection

2.1 Understanding DataAdapter Objects

. NET data providers and DataAdapter classes
. NET Data Providers Connection Class
SQL Data Provider
In the System.Data.SqlClient namespace
SqlDataAdapter
OLE DB Data Provider
In the System.Data.OleDb namespace
OleDbDataAdapter
ODBC Data Provider
In the System.Data.Odbc namespace
OdbcDataAdapter
Oracle Data Provider
In the System.Data.OracleClient namespace
OracleDataAdapter

Main properties and methods of DataAdapter objects

Property:SelectCommand Command object to retrieve data from the database

Method

Fill () Populates the table in the dataset with data

Update submits the data in the dataset to the database

2.2 Populating data sets

Populating a DataSet with DataAdapter requires four steps

(1) Creating a Database Connection object

(2) Creating SQL statements for querying data from a database

(3) Create DataAdapter objects using the SQL statements and connection objects created above

Grammar:

SqlDataAdapter Object name = new SqlDataAdapter (query SQL statement, database connection);

Or

Sqldataadqpter adapter = new SqlDataAdapter ();

SqlCommand command = new SqlCommand (query SQL statement, database connection);

Adapter. SelectCommand = command;

(4) Call the Fill method of the DataAdapter object to populate the dataset

Grammar:

DataAdapter object. Fill (DataSet object, "data table name string");

The fill () method receives a string parameter for the name of a data table. If the data set does not already have the data table, a data table is created after the fill () method is called. If the data table is originally present in the DataSet, the results that are now detected will continue to be added to the data table.

Experience:

Creates a DataAdapter object that is related to the Connection.command object and writes the SQL statement

In different code snippets, the Connection object may appear in different locations

3. Data binding

Data binding is the process of binding the control of an application to any column or row of a database in order to display the data stored in the database on the control. VS uses the DataSource property to provide us with static and dynamic data binding that teaches a good solution to this problem and reduces the complexity of the code

3.1ComboBox Data Binding

When binding data is implemented through a combo box, the properties that are commonly used are:

DataSource get or set the data source

DisplayMember Gets or sets the property to display for this listcontiol

ValueMember Gets or sets a property that will be used as the actual value of the item in the ListControl.

4. Understanding DataGridView Controls

The Data grid view control is a powerful control, and you can modify and delete data directly in DataGridView.

The DataGridView control can display data as a table, set to read-only, or allow editing of data. To specify which table's data DataGridView display, just set his DataSource property and use a single line of code to implement

Main properties:

Columns contains a collection of columns

Data sources for DataSource DataView

ReadOnly whether cells can be edited

Main properties of each column:

DataPropertyName the name of the bound data column

HeaderText column Header text

Visible make a column viewable

FROZEN Specifies whether the column moves when horizontal scrolling datagridview

ReadOnly whether the specified cell is read-only

4. Displaying data using the DataGridView control

(1) Adding controls

(2) Set the properties of the DataGridView and the properties of each column

(3) filter information (for enquiry)

(4) Data source for binding DataGridView

4. Save changes to the data set

(1) Use the SqlCommandBuilder object to generate update related commands

[Syntax]:

SqlCommandBuilder Builder = new SqlCommandBuilder (Dataadaptr object created);

(2) Call the Update method of the DataAdapter object

Dataadaptre object. Update (DataSet object, "data table name string");

Experience:

SqlCommandBuilder only operates on a single table. That is, when you create a Dataadaptr object. The SQL statement used can only look up data from a single table and cannot make a federated query.

Common errors:

(1) Do not use SqlCommandBuilder to call the Updata () method directly

(2) When using DataGridView to display a table in a dataset, the Datapropertname property is not set for his column

Build a well-laid Windows program

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.