Ado.net learning records (1)

Source: Internet
Author: User

Data normalization: divides data into multiple tables as much as possible to minimize the number of times the same data is repeated.
 
Main object elements of ado.net:
 
Data source: a relational database, such as sqlserver
Managed Data supplyProgram: Provides data warehouse communication functions, such as ODBC.
Connection object: establishes a communication channel between the page program and the database driver.
Command object: a tool that contains read/write data commands
Datareader/DataSet object: where data has been read or written is stored
. Net Control: mainly refers to <asp: DataGrid>

Connection object: used to connect to a data source.
Open the connection string using the open () method
The connection string contains the following information:
> The first part specifies the type of the provisioner or driver to be used. // server = localhost
> The second part specifies the database to be used // database = mydatabase
> The third part usually contains security information, including the user name and password. // uid = foolboy; Pwd = mypasswd

Command object and datareader: Read and modify data
Command usage:
Objcommand = new oledbcommand (strsql, objconnection );
Objdbdatareader = objcommand. executereader ();
Datareader: stores data
Read method datareader ["field"]

Data Binding:
Is the process of creating a connection between the data source and the data user.
It mainly refers to binding to the DataGrid.
Limitations of datareader:
> Only data can be read and cannot be modified.
> Only forward data loops are allowed.
> Only one table can be processed.
Is dataset a substitute for him or a datatable ?? What are their main differences?


Dataset and datatable objects
Dataset indicates the data in the database. Unlike datareader, dataset can store several tables and their relationships.
The following four objects are used for representation:
> Datatable: indicates the table itself.
> Dataset: a core object that establishes an adhoc relationship between multiple tables. A Row in one table can be associated with a row in another table.
> Dataadapter: used to pass the result from connection to dataset. The fill () method copies the data to dataset, and the update () method returns the data in dataset to the data source.
> Dataview: indicates the specific view of the Ables stored in dataset.
> DataGrid: Wait for the datasource to be bound only to a specific dataview.

Ado.net SQL Server objects
Sqlconnection
Sqlcommand
Sqldataadapter

Data Exception Handling

FAQs:
>CodeInclude references to non-existing ADO. Net objects
> The Code request data is null and does not exist.
> The connection string of the Code is incorrect.
> Include references to nonexistent columns or tables
> The correct userid and password are not provided.
> The code is an SQL statement with incorrect syntax.
> Database connection problems caused by network problems
 
Solution:
Capture error information using try... catch

Data Update Method

Problem:
> How to update? All our modifications are based on disconnected connections. If we write the modified results to the database?
> How to handle synchronous updates? What should I do if I update the same data successively? Will the result be overwritten?

The dataset & datatable & datarow relationship is as follows:
--------------------------------------
| Dataset |
| ------------------------- |
| Datatable |
|
| ---------------- |
| Datarow |
| ---------------- |
| Datarow |
| ---------------- |
| ------------------------- |
| ------------------------------------ |

Datatable = dataset. Tables ["tname"];
Datarow = datatable. Rows;
String strfirstname = datarow [0] ["firstname"];

the update method here is essentially a data set dataset update method, which does not involve data source updates
Update operation logic:
1. add record (add row)
to add a record, you must first declare two variables datatable. datarow
where datatable needs to be instantiated to a table in a specific dataset
datarow = datatable. newrow () is declared as a new row of the table
and then assigned a value to datarow, calling datatable. rows. add (datarow)
2. modify record (edit row)
first declare a variable datarow [] objrows to store the row to be edited
objrows = datatable. select ("query condition");
if it is a row, you can use objrows = datatable. rows [3];
then modify it, for example, objrows [0] [field1] = "" objrows [0] [field2] = ""
3. delete record
The following datatable. rows [5]. delete ();
suppose: You can declare a variable datarow [] objrows to store the row to be deleted.
objrows = able. select ("query condition");

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.