executenonquery&& ExecuteQuery Difference

Source: Internet
Author: User

When working on a data project in the last few days, when dealing with the ExecuteNonQuery () method in ADO, it is always a good practice to determine whether the return value is greater than the max. But in fact is not the case, fortunately, the processing of data manipulation for a long while to modify, insert, delete, otherwise the problem is a bit dozen, are some basic knowledge, but very important individuals feel the need to write down.

ExecuteNonQuery () method The primary user updates the data, usually it uses the Update,insert,delete statement to manipulate the database, and its method returns the value meaning: for Update,insert,delete statements Execution success is the number of rows affected by the return value for the command, if the number of rows affected is 0 o'clock and the value returned is 0, if the data operation is rolled back with a return value of-1, it should be no problem and better for such an update operation to use more than 0 of our usual judgment operation, but for other operations such as the database structure , if the operation succeeds when the return is 1, this situation and our usual way of thinking a little gap so should be careful attention, such as a database to add a data table create operation, when the creation of a data table successfully returned 1, if the operation fails (such as the data table already exists) will often occur exception, Therefore, it is best to use the try--catch--statement to fault tolerance when performing this operation.

For example, use the ExecuteNonQuery () method to perform a create operation

    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] (COLLATE) chinese_prc_ci_as null, "+
 " [Adoptedsex] [char] (2) COLLATE chinese_prc_ci_as null, "+
  [Adoptbirthday] [smalldatetime] null, "+
 " [adoptedtype] [varchar] (+) 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 ());

If the execution succeeds, the value returned is-1, if the data table already exists returns an exception: an object named ' AAA ' already exists in the database.

Returns a ResultSet object containing the data generated by the given query; never null if no information is queried, returns a ResultSet object with next () false
while (Rs.next ()) to determine if it is correct
while (Rs.next ()) {
System.out.println ("Name:" +rs.getstring ("stuname") + "\ T Age:" +rs.getstring ("Stuscore"));

executenonquery&& ExecuteQuery Difference

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.