The ExecuteNonQuery () method updates the data primarily by the user, typically using the Update,insert,delete statement to manipulate the database, and the method returns the value meaning: for the Update,insert,delete statement Execution success is the number of rows affected by the return value for this command, and if the number of rows affected is 0 o'clock, the value returned is 0. If the data operation rolls back the return value of-1, it should be fine and preferable for this update to be judged to be greater than 0 in our usual use, but for other operations such as the operation of the database structure , if the operation succeeds with a return of-1, this situation with our usual way of thinking a little gap so should pay attention to, for example, add a data table to the database create operation, when the creation of a data table success return-1, if the operation fails, such as the data table already exists, often occurs abnormal, Therefore, it is best to use try--catch--statements to fault tolerance when performing this operation.
For example, to perform a create operation with the ExecuteNonQuery () method
[code]
SqlConnection conn = new SqlConnection ("Data source=.;i Nitial catalog=psdb;integrated Security=sspi ");
String str = "CREATE TABLE aaa (" +
[ID] [int] IDENTITY (1, 1) not NULL, "+
" [Basicid] [INT] NULL, "+
" [adoptedname] [varchar] (m) COLLATE chinese_prc_ci_as NULL, "+
" [Adoptedsex] [char] (2) C Ollate chinese_prc_ci_as NULL, "+
" [Adoptbirthday] [smalldatetime] NULL, "+
" [Adoptedtype] [varchar ] (MB) COLLATE chinese_prc_ci_as null, "+
" [Apprtime] [smalldatetime] NULL, "+
" [remark] [varchar] (+) COLLATE chinese_prc_ci_as NULL "+
") on [primary] ";
SqlCommand comm = new SqlCommand (STR, conn);
int i = 10;
Try
{
Conn. Open ();
i = Comm. ExecuteNonQuery ();
Conn. Close ();
}
catch (Exception ex)
{
Response.Write (ex. message);
}
Response.Write (i.ToString ());
[Code]
If the execution succeeds, the value returned is-1, and if the datasheet already exists, the exception is returned: An object named ' AAA ' already exists in the database.