Friday, 6 December 2013

READING AND WRITING DATA-----(ExecuteNonQuery Method)

We have already learned how to create DbCommand object and DbParameter Object.Now its time to know different methods are use with these Object.



ExecuteNonQuery Method:

We execute a DbCommand object differently depending on the data being retrieved or modified or the database object you are creating, altering, or dropping. You use the ExecuteNonQuery method when you don’t expect a command to return any rows—an insert, update, or delete query, for example. This method returns an integer that represents the number of rows affected by the operation. The following example executes a SQL command to add 10% to the unit price of the product whose ProductID is 10, and it returns the number of rows that were updated.

C# Code:
private void menuExecuteNonQuery_Click(object sender, EventArgs e)
{

var nw = ConfigurationManager.ConnectionStrings["nw"];
int count = 0;
using (var connection = new SqlConnection())
{
connection.ConnectionString = nw.ConnectionString;
var cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =
"UPDATE Products SET UnitPrice = UnitPrice * 1.1 WHERE ProductID =
connection.Open();
count = cmd.ExecuteNonQuery();
}
MessageBox.Show(count.ToString());
}


No comments:

Post a Comment