Lecture 1 C# 3ed Class
Lecture 1 C# 3ed Class
ADO.NET
Lec. 1
Introduction to Programming using C# with Database
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
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
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.*/
/*We use listView tool to present the database table continent, by clicking on
the button.*/
con.Open();
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());
}
/*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