Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the EXECUTENONQUERY () return value of asp.net

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to solve the EXECUTENONQUERY () return value of asp.net". In the daily operation, I believe many people have doubts about how to solve the problem of EXECUTENONQUERY () return value of asp.net. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to solve the EXECUTENONQUERY () return value of asp.net"! Next, please follow the editor to study!

The ExecuteNonQuery () method mainly updates data. It usually uses Update,Insert,Delete statements to operate the database. The method returns the meaning of the value: the successful execution of the Update,Insert,Delete statement is the number of rows affected by the command. If the number of rows affected is 0, the return value is 0. If the data operation is rolled back, the return value is-1. For this kind of update operation, it should be no problem and better to judge whether it is greater than 0, but for other operations, such as the operation on the database structure, if the operation is successful, the return is-1, which is a little different from our usual way of thinking, so we should pay attention to it, such as the Create operation of adding a data table to the database. Return-1 when the datasheet is created successfully, and exceptions often occur if the operation fails (such as the datasheet already exists), so it is best to use try--catch-- statements to tolerate faults when performing this operation.

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

[code]

SqlConnection conn = new SqlConnection ("Data Source=.;Initial Catalog=PSDB;Integrated Security=SSPI")

String str = "CREATE TABLE aaa (" +)

"[ID] [int] IDENTITY (1,1) NOT NULL," +

"[BasicID] [int] NULL," +

"[AdoptedName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL," +

"[AdoptedSex] [char] (2) COLLATE Chinese_PRC_CI_AS NULL," +

"[AdoptBirthday] [smalldatetime] NULL," +

"[AdoptedType] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL," +

"[ApprTime] [smalldatetime] NULL," +

"[Remark] [varchar] (500) 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]

The value returned is-1 if the execution is successful, and an exception is returned if the data table already exists: an object named 'aaa' already exists in the database.

At this point, the study on "how to solve the EXECUTENONQUERY () return value of asp.net" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report