We need to constantly read and write the database, so ExecuteNonQuery (), ExecuteReader () and ExecuteScalar () are what we need to use in database operations. Let's take a look at them one by one:
ExecuteNonQuery (): This item allows us to use ExecuteNonQuery to change data in the database without using DataSet when updating the database. You can modify data in the database by executing UPDATE, INSERT, or delete SQL statements. Even if ExecuteNonQuery does not return any rows, the output parameters or return values mapped to the parameters are still filled with data. However, for UPDATE, INSERT, and delete SQL statements, the returned value is the number of rows affected by the ExecuteNonQuery () command. For other types of statements and rollback, the returned value is-1.
ExecuteReader (): we usually use the Recordset object in asp to read data from the database, and use loop statements to read data one by one, but in our ADO. NET, we use the ExecuteReader () method of the DataReader object to list data, and we use this ExecuteReader () method to display data is the fastest way, because when we use the DataReader object in ExecuteReader () method for data display in website construction, it can read forward only one by one and cannot return, that is, like Movenext of the Recordset object in the ADO method in ASP, there is no return method like move-1.
ExecuteScalar (): This CMD is equivalent to using some computing functions in SQL statements in ADO. Here we only return the first column of data in the first row of all affected rows, in this way, it is equivalent to the function of selecting count (*) from database. We can use
String SQL = "select * from database"
SqlCommand cmd = SqlCommand (SQL, conn)
Int I = (int) cmd. ExecuteScalar ()
In this way, our local change I value is the same as my function above.
Finally, it is worth mentioning that this is from some of my usual experiences: if we do not have data to operate, we can only use ExecuteReader () executeNoeQuery () and Executescalar (), if they are used when no data is available, the "no Object Instantiation" error will occur. Therefore, we can only use the REad () method in ExecuteReader () to check whether data exists.