Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How ADO.NET uses the DataAdapter class

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces ADO. NET how to use DataAdapter class, the article is very detailed, has a certain reference value, interested friends must read!

ADO. NET or more commonly used, so I studied ADO. NET, here to come up with and share with you, I hope useful to you. Let's talk about ADO. NET using DBCommand, the class through which all database operations are performed. If DBConnection is a pathway, DBCommand is a vehicle running back and forth along that pathway. Without it, all database operations in the program cannot be passed to the database server. DBConnection and DBCommand thus form the basis of ADO. NET. There are a few things to note when working with DBCommand objects.

Set the CommandText and CommandType properties. In general, you don't need to set the CommandType property, but if you execute a stored procedure, you need to set the CommandType property to StoredProcedure. It is recommended to use more parameters and reduce the concatenation of strings. This can reduce program writing errors and avoid sentence bugs. Parameters can be used as follows:

SqlCommand myComm = new SqlCommand(); myComm.CommandText = "SELECT * FROM UserInfo WHERE UserName = @UserName "; myComm.Parameters.Add( "@UserName", yourValue );

Distinguish ExecuteNonQuery method from ExecuteReader method. The former mainly deals with non-query type statements, and the number returned is the affected number, but it has no effect on the "INSERT" statement; while the latter mainly deals with query statements, but it needs DataReader to assist the operation. *** Don't forget to call the Dispose method to release the DBCommand object when you're done. DBCommand alone is not enough to obtain the record set queried, so DataReader is needed to obtain the records queried. For example:

SqlCommand myComm = new SqlCommand(); myComm.CommandText = "SELECT * FROM UserInfo WHERE UserName = @UserName "; myComm.Parameters.Add( "@UserName", yourValue ); SqlDataReader myReader = myComm.ExecuteReader(); while( myReader.Read() ) { myReader.GetValue( index );// Get value through specific index } myReader.Close(); myComm.Dispose();

Therefore, two common methods in DataReader are Read and GetValue. The former is to judge whether to read the record, and the latter is to obtain the value of a field in the record. The return is an object type object, and type conversion is required to obtain the desired field value. Unfortunately, DataReader can only read one row at a time, so when it is used, the query result must be read row by row, during which it is exclusive to DBConnection, that is, it cannot use the same DBConnection to do other database operations at this time.

One point to mention is that ADO. NET provides a better and more convenient tool for manipulating data, the DataAdapter class. For a DataAdapter object, four DBCommands can be set, namely SelectCommand, InsertCommand, DeleteCommand and UpdateCommand. These four DBCommands do not need to be initialized, mainly depending on the specific requirements of the program. If you only use the query aspect, you only need to set SelectCommand, which is the same as the general DBCommand operation.

For the recordset returned by DataAdapter operation, you need to use DataSet for auxiliary operation, for example:

Get Recordset for Query

DataAdapter.Fill( DataSet, "yourTableName" );// "yourTableName" is the name using in data set

update the database

DataAdapter.Update( DataSet, "yourTableName" );

What should be noted here is: first of all, the DataSet needs to be changed. If it is the same as after Fill, it has no impact on the database; secondly, it needs to set InsertCommand, DeleteCommand and UpdateCommand. If the corresponding operation statement is not set, it has no impact on the database. DataAdapter is less efficient, but avoids operations such as reading recordsets, type converting the acquired record data, and so on. At the same time, its possession of DBConnection is only released immediately after the operation on the database. Therefore, it is strongly recommended that ADO. NET use DataAdapter more and ADO. NET use DataReader less. At the end of the article, it should be explained that the above mentioned is only the virtual class name of database operation. If the program really operates the database, it should choose different subclasses to create objects according to different databases.

The above is "ADO. NET how to use DataAdapter class" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report