Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Modifying Data in a Data Source


SQL statements that modify data (such as INSERT, UPDATE, or DELETE) do not return rows. Similarly, many stored procedures perform an action but do not return rows. To execute commands that do not return rows, create a CUBRIDCommand object with the appropriate SQL command and a CUBRIDConnection, including any required CUBRIDParameters. Execute the command with the ExecuteNonQuery method of the CUBRIDCommand object.

The ExecuteNonQuery method returns an integer that represents the number of rows affected by the statement or stored procedure that was executed. If multiple statements are executed, the value returned is the sum of the records affected by all of the statements executed.

Example

The following code example executes an INSERT statement to insert a record into a database using ExecuteNonQuery.

// Assumes connection is a valid SqlConnection.
connection.Open();

string queryString = "INSERT INTO nation values ('A', 'B', 'C', 'D')";

CUBRIDCommand command = new CUBRIDCommand(queryString, connection);
Int32 recordsAffected = command.ExecuteNonQuery();

The following code example executes an UPDATE statement to update a record into a database using ExecuteNonQuery.

// Assumes connection is a valid SqlConnection.
 connection.Open();
 
 string queryString = "UPDATE nation set capital = 'X' where `code` = 'A'";
 
 CUBRIDCommand command = new CUBRIDCommand(queryString, connection);
 Int32 recordsAffected = command.ExecuteNonQuery();

The following code example executes an DELETE statement to delete a record into a database using ExecuteNonQuery.

 // Assumes connection is a valid SqlConnection.
 connection.Open();
 
 string queryString = "DELETE FROM nation where `code` = 'A'";
 
 CUBRIDCommand command = new CUBRIDCommand(queryString, connection);
 Int32 recordsAffected = command.ExecuteNonQuery();

Here is a complete example:

using CUBRID.Data.CUBRIDClient;
  
namespace CommandExample
{
    class Program
    {
        static void Main(string[] args)
        {
            CUBRIDConnectionStringBuilder sb = new CUBRIDConnectionStringBuilder("localhost", "demodb", "public", "", "33000", true);
            using (CUBRIDConnection conn = new CUBRIDConnection(sb.GetConnectionString()))
            {
                conn.Open();
 
                string insertString = "INSERT INTO nation values ('A', 'B', 'C', 'D')";
                string updateString = "UPDATE nation set capital = 'X' where `code` = 'A'";
                string deleteString = "DELETE FROM nation where `code` = 'A'";

                CUBRIDCommand cmdInsert = new CUBRIDCommand(insertString, conn);
                Int32 recordsAffected = cmdInsert.ExecuteNonQuery();
 
                CUBRIDCommand cmdUpdate = new CUBRIDCommand(updateString, conn);
                recordsAffected = cmdUpdate.ExecuteNonQuery();

                CUBRIDCommand cmdDelete = new CUBRIDCommand(deleteString, conn);
                recordsAffected = cmdDelete.ExecuteNonQuery();

                conn.Close();
            }
        }
    }
}

comments powered by Disqus
Page info
viewed 772 times
translations en
Author
posted last year by
kevinyang
Contributors
updated last year by
View revisions
Share this article