Chapter VI using ADO.NET query and manipulate data

. 1, the StringBuilder class - defined variable string

So not only repeated modifications, not create (the new object String difference between classes)

Common methods:

  • . 1)  the Append () at the end of the additional
  • 2)  the Insert () Insert the specified string at the specified location
  • . 3)  the Remove () Removes the specified string

2, the DataReader objects - means for reading the data in the database

DataReader key members:

 

3 , using DataReader retrieve data step of:

  • Creating Command Objects
  • Call ExecuteReader () to create a DataReader objects
  • Use DataReader 's Read () method reads the data row by row
  • A column read data, (type) dataReader [] Note: Gets the value of a column: Method a: the specified column index from 0 starts   two: designated column name
  • Close DataReader objects: Note: DataReader after use must be closed

 4 , update the data

ataReader object can only provide access to data in the database for the application, how to increase the database data,

Delete and modify operations it? Need to call the Command object's ExecuteNonQuery () method.

To use the ExecuteNonQuery () summary:

  • 1, create a Connection object
  • 2. Define the sql statement
  • 3. Create a Command object
  • 4, the implementation of the ExecuteNonQuery () method
  • 5, the processing according to the result returned

Note: Use the ExecuteNonQuery () method returns the number of rows affected by the SQL statement. If the return value is less than or equal to 0, indicating that no records affected.

  

5 , ADO.NET summary

When performing operations on the data source, you may perform the data update (CRUD) operations, or query operation. For query operation

As there may be two situations: First query to obtain a single value, the second is to get a number of query records.

Query a single value

Command object requires the ExecuteScalar () method, the following steps.

(1) create a Connection object.

(2) Spelling SQL query.

(3) the use of SQL statements to create the Command object and the Connection object.

(4) open the database connection, the Connection object call Open () method.

(5) Command object calls the ExecuteScalar () method returns a scalar value.

Close the connection (6) to complete the operation, calling Connection object Close () method.

Several records inquiry

Command object requires the ExecuteReader () method, the following steps.

(1) create a Connection object.

(2) Spelling SQL query.

(3) the use of SQL statements to create the Command object and the Connection object.

(4) open the database connection, the Connection object call Open () method.

(5) Command object calls the ExecuteReader () method returns a DataReader object.

(6) the object invoked in the circulation DataReader Read () method reads the recorded line by line. If you read the record returns true, otherwise false.

(7) (type) dataReader [column name or index] reads the value of the row in a column.

(8) calls DataReader object's Close () method close the DataReader object.

Close the connection (9) After the operation is completed, calling Connection object Close () method.

 

Data update

Perform data on the database update operations (add, modify, delete data) use the Command object

The ExecuteNonQuery () method, steps such as Bian.

(1) create a Connection object.

(2) Spelling SQL CRUD statements.

(3) the use of SQL statements to create the Command object and the Connection object.

(4) open the database connection, the Connection object call Open () method.

(5) call the Command object's ExecuteN0nouery () method executes the command to return the affected database

Rows.

Close the connection (6) to complete the operation, calling Connection object Close () method.

Comparison of three methods shown command object is as follows.

※ ExecuteScalar () method: first row perform a query operation, and returns the result set and the first column.

※ ExecuteReader () method: execute the query operation, return the DataReader object.

※ ExecutenonQuery () method: add, modify, delete, returns the number of rows affected.

Guess you like

Origin www.cnblogs.com/suola/p/11344246.html