Content

  • ADO.NET
  • Connection string
  • Save Webform data in database

ADO.NET

ADO.NET is a set of classes (a framework) to interact with data sources such as databases. ADO is the acronym for ActiveX Data Objects. It allows us to connect to underlying data or databases. It has classes and methods to retrieve and manipulate data.

ADO.NET also allows us to connect with database by using ConnectionString

In this diagram, we can see that there are various types of applications (Web Application, Console Application, Windows Application and so on) that use ADO.NET to connect to databases (SQL Server, Oracle, OleDb, ODBC, XML files and so on).


Classes in ADO.NET

  • Connection Class : We use these connection classes to connect to the database
  • Command Class : The Command class provides methods for storing and executing SQL statements and Stored Procedures. The following are the various commands that are executed by the Command Class.
  • ExecuteReader : Returns data to the client as rows. This would typically be an SQL select statement. This method returns a DataReader object that can be used to fill a DataTable object or used directly for printing reports or show retrived data to end user.
  • ExecuteNonQuery : Executes a command that changes the data in the database, such as an update, delete, or insert statement
  • ExecuteScalar : This method only returns a single value. This kind of query returns a count of rows or a calculated value.
  • DataReader Class: The DataReader is used to retrieve data. It is used in conjunction with the Command class to execute an SQL Select statement and then access the returned rows
  • DataAdapter Class : The DataAdapter is used to connect DataSets to databases. The DataAdapter is most useful when using data-bound controls in Windows Forms, but it can also be used to provide an easy way to manage the connection between your application and the underlying database tables, views and Stored Procedures.
  • DataSet Class : The DataSet is essentially a collection of DataTable objects. In turn each object contains a collection of DataColumn and DataRow objects.

Connection string

  • Connection string consist of Data Source(server name),username,password(if applicable)
  • We can write connection string in back end code OR in web.config file
  • If we write connection string in web.config file then we have to call/declare this connection string in back end code whenever required
Example :
Web.config code
<connectionStrings>
    <add name="connstr" connectionString="Data Source=ADMIN;Initial Catalog=REVOLUTION;Integrated Security=True"/>
</connectionStrings>
Code Behind
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);

Save Webform data in MS-SQL Database

using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.Data.SqlClient;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
          
      namespace Revolution
      {
          public partial class Contact : System.Web.UI.Page
          {
              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
              protected void Page_Load(object sender, EventArgs e)
              {
                 
              }
          
              protected void btnsubmit_Click(object sender, EventArgs e)
              {
                  con.Close();
                  SqlCommand cmd = new SqlCommand("insert into RevEnquiryData values('" + txtname.Text + "','" + txtcontact.Text + "','" + txtmail.Text + "','" + txtdes.Text + "',@date)", con);
                  cmd.Parameters.AddWithValue("@date", DateTime.Now.ToShortDateString());
                  con.Open();
                  cmd.ExecuteNonQuery();
                  txtcontact.Text = "";
                  txtdes.Text = "";
                  txtmail.Text = "";
                  txtname.Text="";
                  this.ClientScript.RegisterStartupScript(this.GetType(), "SweetAlert", "swal('Enquiry Sent Successfully,Thank You..! We will contact you very soon..!','','success');", true);
                      
              }
          }
      }