0% found this document useful (0 votes)
2 views

Lecture 1 C# 3ed Class

The document provides an introduction to ADO.NET for database access using C#. It covers the objectives of learning about database technologies, ADO.NET classes, and how to use the DataReader, DataSet, and DataAdapter for data manipulation. It also explains the structure of databases, the role of data providers, and includes sample code for connecting to a SQL Server database and retrieving data.

Uploaded by

yousif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 1 C# 3ed Class

The document provides an introduction to ADO.NET for database access using C#. It covers the objectives of learning about database technologies, ADO.NET classes, and how to use the DataReader, DataSet, and DataAdapter for data manipulation. It also explains the structure of databases, the role of data providers, and includes sample code for connecting to a SQL Server database and retrieving data.

Uploaded by

yousif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Database Access Using

ADO.NET

Lec. 1
Introduction to Programming using C# with Database

Dr. Yousif Albayati


Objectives

• Be introduced to technologies used for accessing databases

• Become familiar with the ADO.NET classes

• Write program statements that use the DataReader class to


retrieve database data

• Access and update databases using the DataSet and DataAdapter


classes

2
Databases
• Databases store information in records, fields, and tables
• Database management system (DBMS): computer programs used to
manage and query databases
• Example DBMSs include SQL server, Oracle, and Access
• Many DBMSs store data in tabular format
• Data in tables are related through common data field keys

3
Database Access
• Typically use a query language to program database access
• Structured query language (SQL)
• ActiveX Data Objects (ADO.NET): .NET data access technology for
accessing data in databases

4
ADO.NET
• Includes number of classes that can be used to retrieve, manipulate,
and update data in databases
• Can work with databases in a disconnect manner

• Database table(s) can be retrieved to a temporary


file
• To retrieve data first, you must connect to the database
• ADO.NET uses a feature called data providers to connect, execute
commands, and retrieve results from a database

5
Data Providers
• Microsoft SQL Server
• Applications using SQL Server 7.0 or later
• Oracle
• Applications using Oracle data sources
• Object Linking and Embedding Database (OLE DB)
• Applications that use Microsoft Access databases
• Open Database Connectivity (ODBC)
• Applications supported by earlier versions of Visual Studio, access driver
(.mdb) and Microsoft ODBC for Oracle

6
Data Providers (continued)
• Classes are encapsulated into a different namespace by provider
• Four core classes make up each data provider namespace
• Connection
• Command
• DataReader
• DataAdapter

7
Data Providers (continued)

8
9
Lecture 1

C# and SQL Connection

1 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/*In order to able to use all Sql command you must call Sql library (using
System.Windows.Forms)*/

using System.Data.SqlClient;

namespace csql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
/* We use the ( SqlConnection) command in order to make a connection between
Microsoft Visual Studio and Microsoft SQL Server,as shown it contain the server
name and database name in addition, username and password.*/

SqlConnection con = new SqlConnection("Data Source=ERKAN\\SQLEXPRESS;Initial


Catalog=ERKAN;Integrated Security=True");

/*We use listView tool to present the database table continent, by clicking on
the button.*/

private void button1_Click(object sender, EventArgs e)


{
/*open the connection between Microsoft Visual Studio and Microsoft SQL Server*/

con.Open();

/*To bring the table continents(which is in the database) we use the (


SqlCommand) with the (select * from table name, and connection expression*/

SqlCommand code = new SqlCommand("select *from sudentinformation",


con);

/* SqlDataReader to read the table information.ExecuteReader to execute the


reading operation.*/

SqlDataReader r = code.ExecuteReader();
/*we use the while loop statement to keep (SqlDataReader) working (read) until to
finish all the column continents in the table.*/
while (r.Read())
{
ListViewItem add = new ListViewItem();
add.Text=r["studentname"].ToString();
add.SubItems.Add(r["studentage"].ToString());
add.SubItems.Add(r["studentdepartment"].ToString());

/*finally add the table items to the list view*/


listView1.Items.Add(add);

}
/*Close the connection between Microsoft Visual Studio and Microsoft SQL Server*/

con.Close();

}}}
To get the SQL connection string and create tables ,follow the steps

You might also like