asp.net executenonquery () return value Introduction _ Practical Tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.