ASP .Net Database Connectivity
Presenter: Sridhar Udayakumar
Outlines
● Introduction to Database
● What Is SQL?
● Working with Data in the .NET Framework
● ADO.Net
A Database
● A database is a collection of data that is
organized in a manner that facilitates ease of
access, as well as efficient management and
updating.
● A database is made up of tables that store
relevant information.
● For example, you would use a database, if you
were to create a website like YouTube, which
contains a lot of information like videos,
usernames, passwords, comments.
What is SQL?
● SQL stands for Structured Query Language.
● SQL is used to storing, manipulating and retrieving
relational database data.
● MySQL is a program that understands SQL.
● SQL can:
● insert, update, or delete records in a database.
● create new databases, table, stored procedures,
views.
● retrieve data from a database, etc.
SQL is an ANSI (American National Standards
Institute) standard, but there are different
versions of the SQL language.
Working with Data in the
.NET Framework
Approach to connect DB
ASP .Net framework and database can be interconnected
in 2 different approaches.
• ADO.NET
• Using any ORM (Entity Framework, NHibernate etc.)
ADO .Net
• Database connectivity and functionality are defined
in the ADO.NET namespaces of the .NET Framework.
• ADO.NET comes with several data providers,
including SqlClient, OleDB, and ODBC.
• .NET framework also provides in-memory data access
using LINQ. In this article
• SQL data provider of ADO.NET is defined in the
System.Data.SqlClient namespace
System.Data.SqlClient
• System.Data.SqlClient namespace of .NET Framework
contains all of the classes required to connect to
a SQL Server database and read, write, and update.
• The namespace provides classes to create a database
connection, adapters, and SQL commands that provide
the functionality to execute SQL queries.
Steps to connect database using ADO.Net
1. Declaring connectionStrings in web.config file
2. Establish the Connection
a. Get connection string from web.config file
b. create new SqlConnection object to connect
database by using connection string from
web.config file
c. Open the connection
3. Perform CRUD operation
4. Close the connection
Declaring connectionStrings
Add the below connection string configuration in web.config file
<connectionStrings>
<add name="yourconnectinstringName" connectionString="Data
Source= DatabaseServerName; Integrated Security=true;Initial
Catalog= YourDatabaseName; uid=YourUserName;
Password=yourpassword; "
providerName="System.Data.SqlClient" />
</connectionStrings>
Example:
<connectionStrings>
<add name="EmployeeDbConn" connectionString="Data
Source=INBOOK_X1_SLIMSQLEXPRESS;Initial
Catalog=EmploeeDb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Establish the Connection
//Get connection string from web.config file
string strcon =
ConfigurationManager.ConnectionStrings["dbconnection"].Conne
ctionString;
//create new sqlconnection and connection to database by
using connection string from web.config file
SqlConnection con = new SqlConnection(strcon);
//Open the Connection
con.Open();
PERFORM CRUD OPERATION
Executing the Commands
– Once connected to the database, you can execute
the set of commands that you're having and which
would execute on the server (or the data
provider) to execute the function you're trying
to do, such as a query for data, insert the
data, update records and so on and so forth.
SqlCommand command = new SqlCommand("SELECT * FROM
TableName", conn);
Parameterizing the data
• Parameterizing the query is done using the
SqlParameter ed into the command. For example, you
might want to search for the records where the
criteria match.
• You can denote that criterion by @ the variable
name into the query and then adding the value to it
using the SqlParameter object
// Create the command
SqlCommand command = new SqlCommand("SELECT * FROM TableName
WHERE FirstColumn = @0", conn);
// Add the parameters.
command.Parameters.Add(new SqlParameter("0", 1));
Reading the data returned
• In SQL, you usually use the SELECT statement to get
the data from the database to show.
• The class SqlDataReader present for the
SqlCommand that returns the Reader object for the
data. You can use this to read through the data and
for each of the columns, provide the results on the
screen.
Reading the data returned
string sql = "select * from employee";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
employeeList.Add(new Employee()
{
EmployeeId = (int)reader["employeeId"],
employeeName = reader["EmployeeName"].ToString(),
age = (int)reader["age"]
});
}
Close the Connection
Close the connection object once the CRUD operations CRUD is
completed
conn.Close();
THANK YOU

Asp .Net Database Connectivity Presentation.pptx

  • 1.
    ASP .Net DatabaseConnectivity Presenter: Sridhar Udayakumar
  • 2.
    Outlines ● Introduction toDatabase ● What Is SQL? ● Working with Data in the .NET Framework ● ADO.Net
  • 3.
    A Database ● Adatabase is a collection of data that is organized in a manner that facilitates ease of access, as well as efficient management and updating. ● A database is made up of tables that store relevant information. ● For example, you would use a database, if you were to create a website like YouTube, which contains a lot of information like videos, usernames, passwords, comments.
  • 4.
    What is SQL? ●SQL stands for Structured Query Language. ● SQL is used to storing, manipulating and retrieving relational database data. ● MySQL is a program that understands SQL. ● SQL can: ● insert, update, or delete records in a database. ● create new databases, table, stored procedures, views. ● retrieve data from a database, etc. SQL is an ANSI (American National Standards Institute) standard, but there are different versions of the SQL language.
  • 5.
    Working with Datain the .NET Framework
  • 6.
    Approach to connectDB ASP .Net framework and database can be interconnected in 2 different approaches. • ADO.NET • Using any ORM (Entity Framework, NHibernate etc.)
  • 7.
    ADO .Net • Databaseconnectivity and functionality are defined in the ADO.NET namespaces of the .NET Framework. • ADO.NET comes with several data providers, including SqlClient, OleDB, and ODBC. • .NET framework also provides in-memory data access using LINQ. In this article • SQL data provider of ADO.NET is defined in the System.Data.SqlClient namespace
  • 8.
    System.Data.SqlClient • System.Data.SqlClient namespaceof .NET Framework contains all of the classes required to connect to a SQL Server database and read, write, and update. • The namespace provides classes to create a database connection, adapters, and SQL commands that provide the functionality to execute SQL queries.
  • 9.
    Steps to connectdatabase using ADO.Net 1. Declaring connectionStrings in web.config file 2. Establish the Connection a. Get connection string from web.config file b. create new SqlConnection object to connect database by using connection string from web.config file c. Open the connection 3. Perform CRUD operation 4. Close the connection
  • 10.
    Declaring connectionStrings Add thebelow connection string configuration in web.config file <connectionStrings> <add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient" /> </connectionStrings> Example: <connectionStrings> <add name="EmployeeDbConn" connectionString="Data Source=INBOOK_X1_SLIMSQLEXPRESS;Initial Catalog=EmploeeDb;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
  • 11.
    Establish the Connection //Getconnection string from web.config file string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].Conne ctionString; //create new sqlconnection and connection to database by using connection string from web.config file SqlConnection con = new SqlConnection(strcon); //Open the Connection con.Open();
  • 12.
  • 13.
    Executing the Commands –Once connected to the database, you can execute the set of commands that you're having and which would execute on the server (or the data provider) to execute the function you're trying to do, such as a query for data, insert the data, update records and so on and so forth. SqlCommand command = new SqlCommand("SELECT * FROM TableName", conn);
  • 14.
    Parameterizing the data •Parameterizing the query is done using the SqlParameter ed into the command. For example, you might want to search for the records where the criteria match. • You can denote that criterion by @ the variable name into the query and then adding the value to it using the SqlParameter object // Create the command SqlCommand command = new SqlCommand("SELECT * FROM TableName WHERE FirstColumn = @0", conn); // Add the parameters. command.Parameters.Add(new SqlParameter("0", 1));
  • 15.
    Reading the datareturned • In SQL, you usually use the SELECT statement to get the data from the database to show. • The class SqlDataReader present for the SqlCommand that returns the Reader object for the data. You can use this to read through the data and for each of the columns, provide the results on the screen.
  • 16.
    Reading the datareturned string sql = "select * from employee"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { employeeList.Add(new Employee() { EmployeeId = (int)reader["employeeId"], employeeName = reader["EmployeeName"].ToString(), age = (int)reader["age"] }); }
  • 17.
    Close the Connection Closethe connection object once the CRUD operations CRUD is completed conn.Close();
  • 18.