Ado|asp.net| Notes |ado|asp.net notes from the C # Web application entry Classic
1. ADO. NET Architecture
Ado. NET this name does not represent the actual content--ado originally represents the ActiveX Data obejects, but is a kind of technology name. The most common way to access a database is to connect to the database first and then use the SQL statement. There are different command object methods for different database operations. For example, the ExecuteScalar () method returns an object that contains a value, and the ExecuteReader () method is used to access the DataReader object of the result set, and ExecuteNonQuery () returns an integer value representing the number of rows affected by the command. This refers to the DataReader object, which is a fast, read-only, forward-only connection pointer that returns data from the database. After the object is obtained by the ExecuteReader () method, when the read () is called, and if True, the method is used to access the data at the current location. A result set contains multiple rows of data, you typically access each row with the following code:
Reader = command. ExecuteReader ();
while (read. Read ())
{
Process Current row
}
Access to the data contained in each column cell in the current row can be accessed using the following DataReader method: (1) getxxx (), which retrieves the value entered. Methods such as Getboolean (), GetString (), and GetInt32 () can receive the index of the column as a parameter and return the correct value type. such as Response.Write (reader. GetString (0)) (where reader is the DataReader object of the above code, the same below). Of course, sometimes you don't know the index, but you know the name, this time you can use the DataReader object's GetOrdinal () method, which is used to receive the name of the column and return the column's position: int pos = reader. GetOrdinal ("CategoryID"); (2) The default Item property, through which you can directly access the value of a column, which can be an integer index value or a column name of type string, and the return value is of type object. It is therefore necessary to convert it to the desired data type: int id = (int) reader["UserId"], or int id = (int) reader[0], (3) GetValues () method to populate the array with the values in the column. The method receives an array of type object and populates it with the data in the current row: object[] values = new OBJECT[3]; Reader. GetValues (values), where you can initialize the array with the DataReader FieldCount property. The code just said to populate the array with the first three columns of the current row.