Database Programming in C#
Database Programming in C#
CONTENTS
1. Introduction to ADO.NET
3. Connecting to a Database
4. Architecture of ADO.NET
INTRODUCTION TO ADO.NET
C# and .Net can work with a majority of databases, the most common being Microsoft SQL
Server and Oracle. But with every database, the logic behind working with all of them is
mostly the same. in the Microsoft .NET Framework, access to a wide variety of data sources
is enabled through a group of classes collectively named Microsoft ADO.NET.
ADO.NET (ActiveX Data Objects) is a data access technology from Microsoft .Net
Framework, which provides communication between relational and non-relational systems
through a common set of components. ADO.NET consist of a set of Objects that expose data
access services to the .NET environment. ADO.NET is designed to be easy to use, and Visual
Studio provides several wizards and other features that you can use to generate ADO.NET
data access code.
Database Access
Three steps:
3. Close connection
The namespaces in the following table expose the classes and interfaces used in .NET data
access
System.Data.SqlClient
This namespace of .NET Framework contains all of the classes required to connect to the
databases, read/write data to the databases.
3. Connecting to a Database
This string has the information about the server you're going to connect, the database you will
require and the credentials that you can use to connect. Each database has its own properties,
its own server, name and type of login information, using which you can connect to the
database to read/write the data from it.The connection string may include attributes such as
the name of the driver, server and database, as well as security information such as user name
and password
You configure a connection object using a connection string. A connection string is a set of
semicolon-separated name/value pairs. You can supply a connection string either as a
constructor argument or by setting a connection object’s Connection String property before
opening the connection. Each connection class implementation requires that you provide
different information in the connection string.
1. Open connection
2. Execute command
4. Close reader
5. Close connection
Advantages
Disadvantages
Continual connection
Not available via Internet
1. Open connection
3. Close connection
5. Open connection
7. Close connection
Advantages
Disadvantages
4.ARCHITECTURE OF ADO.NET
The two key components of ADO.NET are Data Providers and Dataset. .Net Framework
includes mainly three Data Providers for ADO.NET. They are the Microsoft SQL Server Data
Provider , OLEDB Data Provider and ODBC Data Provider + Oracle .NET Data Provider
1.DataTable : a collection of rows and columns that represents structured data in a tabular
format.
ConnectionString Parameters
Provider
Data Source
Initial Catalog
Integrated Security
UserID/Password
Closing connection: When you’re finished with a connection, you should always call its
Close method to free the underlying database connection and system resources.
Eg: conn.Close()
Alternatively, using statement makes very clean and efficient way of using connection
objects in your code. When using statement is used, the method Close() of connection
object is automatically called to close the opened connection and to release any resource.
EXAMPLE
Properties
Connection
CommandType
CommandText
Parameters
Methods
ExecuteNonQuery
ExecuteReader
ExecuteScalar
Gets or sets the SQL statement, table name or stored procedure to execute at the data
source.
CommandType. Text
CommandType.StoredProcedure
SqlCommand.CommandTimeout Property
The time in seconds to wait for the command to execute. The default is 30 seconds. Gets or
sets the wait time before terminating the attempt to execute a command and generating an
error.
Constructors
1. SqlCommand ()
2. SqlCommand(cmdText)
Example
SqlCommand command = new SqlCommand ();
command.CommandTimeout = 15;
command.Parameters.Add("@i", SqlDbType.Int);
command.Parameters["@i"].Value = idno;
command.Parameters.Add("@f", SqlDbType.Text);
command.Parameters["@f"].Value =fn; OR
command.Parameters.AddWithValue(“i", idno);
command.Parameters.AddWithValue("f", fn);
param.Value = inputCity;
EXECUTING COMMANDS
The SqlCommand class provides the following methods for executing commands
against the SQL Server database:
▪Use a DataReader object when you want to read data but not add, delete, or modify
records
Read() Method
o The content of each variable changes when the cursor position moves to a new
row.
o Use the Close() method of the SqlDataReader class to close it when you are
finished working with it.
Exclusive Access
o You cannot access any other commands until the SqlDataReader object is
closed.
EXAMPLE
2.ExecuteNonQuery(): Executes the command but does not return any output
▪Updates
▪Inserts
▪Deletes
ExecuteNonQuery :
▪Example SqlCommand cmd = new SqlCommand ("insert into titles (title_id, title, type,
pubdate)"+ "values ('CS150','C++ Programming'," + " 'computer science', 'May
2006'), conn); cmd.ExecuteNonQuery();
Insert Query
Delete Query
Update Query
3.ExecuteScalar(): Executes the command and returns the value from the first column
of the first row of any result set
Example