What exactly does cmd.ExecuteNonQuery () do in my program

string connection = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=D:\\it101\\LoginForm\\App_Data\\registration.mdb";

string query = "INSERT INTO [registration] ([UserID] , [Name] , [Contact_no] , [City]) values (123, 'abc' ,12345, 'pqr')";

OleDbConnection con = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;

con.Open();
cmd.ExecuteNonQuery();

But what if I don't write cmd.ExecuteNonQuery () at the end of my program? And if the query needs to be executed, why is it written executeNonquery () instead of executeQuery ()?

+4
source share
3 answers

if we want to deal with a database, two things will happen: ie; Change Retrieving

Modification:

In the “Editing” section, we have “Insert”, “Delete”, “Update”, “Requests”. To do this, we need to use the ExecuteNonQuery.why command, because we do not query the database, we modify it.

Syntax:

cmd.ExecuteNonQuery

Extract:

Select Statement. ExecuteReader(), ExecuteScalar()

Query, . ExecuteReader()

Query, . ExecuteScalar()

:

cmd.ExecuteReader()

cmd.ExecuteScalar()

(ExecuteReader(), ExecuteScalar(), SqlCommand.ExecuteNonQuery()) , SqlCommand. , .

+12

ExecuteNonQuery , (, UPDATE INSERT).

ExecuteQuery , (.. SELECT).

ExecuteNonQuery , .

+4

SqlCommand.ExecuteNonQuery

Executes a join statement and returns the number of rows affected. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

executeQuery method

Runs the given SQL statement and returns a single SQLServerResultSet object.

If you want to fulfill your query, you must use ExecuteNonQuery

+3
source

Source: https://habr.com/ru/post/1529764/


All Articles