ADO.NET difference faqs- 3

562 views

Published on

This provides a list of ADO.NET FAQs-3 which are of "Difference between" kind

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
562
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ADO.NET difference faqs- 3

  1. 1. 1.Difference between ExecuteNonQuery() and ExecuteScalar() methods in ADO.NET S.No ExecuteNonQuery() ExecuteScalar() 1 It will work with Action Queries It will work with Non-Action Queries only that contain aggregate functions. (Create,Alter,Drop,Insert,Update, Delete). 2 It returns the count of rows It returns the first row and first column effected by the Query. value of the query result. 3 Return type is int Return type is object. 4 Return value is optional and can Return value is compulsory and should be assigned to an integer variable. be assigned to a variable of required type.Example-1 for ExecuteNonQuery Method -Insert:SqlCommand cmd = new SqlCommand("Insert Into SampleTable Values(1,2)",con);//con is the connection objectcon.Open();cmd.ExecuteNonQuery(); //The SQL Insert Statement gets executedExample-2 for ExecuteNonQuery Method - Update:public void UpdateEmployeeEmail(){ SqlConnection conn = new SqlConnection(connString)) String sqlQuery = "UPDATE Employee SET empemail=umar.ali@xyz.com WHEREempid=5; SqlCommand cmd = new SqlCommand(sqlQuery, conn); try { conn.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); } return count;}
  2. 2. Example-1 for ExecuteScalar Method:This returns only one value that is first column value of the first row in the executed querypublic int getSomeProdId(){ int count=0; SqlConnection conn = new SqlConnection(connString)) String sqlQuery = "SELECT COUNT(*) FROM dbo.region"; SqlCommand cmd = new SqlCommand(sqlQuery, conn); try { conn.Open(); //Since return type is System.Object, a typecast is must count = (Int32)cmd.ExecuteScalar(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); } return count;}Example-2 for ExecuteScalar Method:This returns one value only, no recordsets.cmd.CommandText = "Select Name, DOB, from Emp where ID=1";Dim strName As string = cmd.ExecuteScalar.ToString2.Difference between ExecuteNonQuery() and ExecuteReader() methods in ADO.NET S.No ExecuteNonQuery() ExecuteReader() 1 It will work with Action Queries It will work with Action and Non- only Action Queries (Select) (Create,Alter,Drop,Insert,Update, Delete). 2 It returns the count of rows It returns the collection of rows effected by the Query. selected by the Query. 3 Return type is int Return type is DataReader. 4 Return value is optional and can Return value is compulsory and should
  3. 3. be assigned to an integer variable. be assigned to an another object DataReader.Example-1 for ExecuteNonQuery Method -Insert:SqlCommand cmd = new SqlCommand("Insert Into SampleTable Values(1,2)",con);//con is the connection objectcon.Open();cmd.ExecuteNonQuery(); //The SQL Insert Statement gets executedExample-2 for ExecuteNonQuery Method - Update:public void UpdateEmployeeEmail(){ SqlConnection conn = new SqlConnection(connString)) String sqlQuery = "UPDATE Employee SET empemail=umar.ali@xyz.com WHEREempid=5; SqlCommand cmd = new SqlCommand(sqlQuery, conn); try { conn.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); } return count;}Example for ExecuteReader Method:Here, ExecuteReader is used to get set of records by specified query, namely, "select * from emp"SqlConnection con = new SqlConnection(constr); //constructor can be connection of string.SqlCommand cmd = new SqlCommand ("select * from emp", con);con.Open();SqlDataReader dr = cmd. ExecuteReader (CommandBehavior. CloseConnection); //Implicitlycloses the connection because CommandBehavior. CloseConnection was specified.while(dr.Read()){Console.WriteLine (dr.GetString(0));}dr.Close();
  4. 4. And, further updates on difference between questions and answers, please visit my blog @http://onlydifferencefaqs.blogspot.in/

×