Loading

Execute Commands Methods: How to use it?

Submitted by: 


We have 4 types of Exceute methods, through which we can excecute the quries angainst Database.
1). ExecuteNonQuery(()
2). ExecuteReader()
3). ExecuteScalar()
4). ExceuteXmlReader()

1). ExecuteNonQuery(()
Explanation: Executes a command but does not return any value or output
usage conditions: UPDATE, INSERT, DELETE statements

  1. using System;
  2. using System.Data;
  3. public class exampleone
  4. {
  5. publis static void main(string[] args)
  6. {
  7. string source="server=(local);" +
  8. "integrated security=SSPI;"+
  9. "databs=dbNAME";
  10.  
  11. string select="UPDATE table set name='newname' where name='raj'";
  12.  
  13. SqlConnection conn = new SqlConection(source);
  14. conn.Open();
  15. SqlCommand cmd = new SqlCommand(select,conn);
  16. int rowsreturn = cmd.ExecuteNonQuery();
  17. Response.Write(rowsreturn);
  18. conn.Close();
  19. }
  20. }

2). ExecuteReader()
Explanation: Executes a command and returns a typed data reader object.
usage conditions: Display Data

  1. using System;
  2. using System.Data;
  3. public class exampletwo
  4. {
  5. publis static void main(string[] args)
  6. {
  7. string source="server=(local);" +
  8. "integrated security=SSPI;"+
  9. "databs=dbNAME";
  10.  
  11. string select="SELECT * from Tablename";
  12.  
  13. SqlConnection conn = new SqlConection(source);
  14. conn.Open();
  15. SqlCommand cmd = new SqlCommand(select,conn);
  16. SqlDataReader reader = cmd.ExecuteReader();
  17. while(dr.Read())
  18. {
  19. Response.write(dr[0].ToString());
  20. }
  21.  
  22. }
  23. }

3). ExecuteScalar()
Explanation: Executes a command and returns a single result.
usage conditions: count of records, time etc.

  1. using System;
  2. using System.Data;
  3. public class exampletwo
  4. {
  5. publis static void main(string[] args)
  6. {
  7. string source="server=(local);" +
  8. "integrated security=SSPI;"+
  9. "databs=dbNAME";
  10.  
  11. string select="SELECT count(*) from Tablename";
  12.  
  13. SqlConnection conn = new SqlConection(source);
  14. conn.Open();
  15. SqlCommand cmd = new SqlCommand(select,conn);
  16. object o = cmd.ExecuteScalar();
  17. Response.write(o);
  18.  
  19. }
  20. }

4). ExecuteXmlReader()
Explanation: Executes a command and returns a XmlReader Object.

  1. using System;
  2. using System.Data;
  3. using System.Xml
  4. public class exampletwo
  5. {
  6. publis static void main(string[] args)
  7. {
  8. string source="server=(local);" +
  9. "integrated security=SSPI;"+
  10. "databs=dbNAME";
  11.  
  12. string select="SELECT name,address from Tablename FOR XML AUTO ";
  13.  
  14. SqlConnection conn = new SqlConection(source);
  15. conn.Open();
  16. SqlCommand cmd = new SqlCommand(select,conn);
  17. XmlReader xr = cmd.ExecuteXmlReader();
  18. xr.read();
  19. do
  20. {
  21. s = xr.ReadOuterXml();
  22. if(s!=="")
  23. Response.Write(s);
  24. } while (s!="");
  25. conn.Close();
  26. }
  27. }

About the author:

PlanetSourceCode.in is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development




Comments

sir

i want source code for tic tac toe game in asp.net

Pages

Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.