Command.ExecuteNonQuery() executes a t-sql statement or stored procedure without a return value. It is generally used for insert delete update and other operations to return the number of affected rows.
Command.ExecuteRader() executes a query that returns a result set
Command.ExecuteScalar() executes a query that returns a single value. The return value type is object.
Generally used to execute queries using aggregate functions such as count, max, min, average etc.
connection.CreateCommand()
Create a Command object based on the currently linked object
int?AddressId;
Define a nullable value type
AddressId.HasValue
Determine whether it has value
AddressId.Value.ToString();
Convert its value to String type
commandText can use 2 SQL statements separated by ";" to return multiple result sets.
Use NextResult in DataReader to get the next result set.
Using parameter objects in command
1.0
sqlCommand.Parameters.Add("@pname",SqlDbType.VarChar).Value=this.textBox1.Text;
2.0
sqlCommand.Parameters.AddWithValue("@pname",this.textBox1.Text);
Get a parameter object by parameter index or name
sqlCommand.Parameters[index|pname]
Use transactions
1Create a transaction object
XxxTransaction tran = XxxConnection.BeginTransaction()
Sql:SqlTransaction tran = SqlConnection.BeginTransaction();
2 Specify a transaction for the Command object
sqlCommand.Transaction = tran
3Execute command
sqlCommand.ExecuteNonQuery()
4Commit or rollback transaction
tran.Commit()
tran.Rollback()
When the transaction has not ended, by default the records involved cannot be operated and are locked. This is the isolation level of the transaction. If you want to modify the isolation level when opening a transaction, you can specify its isolation level.
-