0% found this document useful (0 votes)
76 views11 pages

Advanced C# Database Programming Guide

The document outlines a programming course focused on advanced C# with an emphasis on database management using SQL Server. It covers key concepts such as creating databases, using ADO.NET for data access, and performing CRUD operations (Create, Read, Update, Delete) in C#. Additionally, it provides examples of connecting to a database, retrieving data, and implementing user authentication through SQL queries.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views11 pages

Advanced C# Database Programming Guide

The document outlines a programming course focused on advanced C# with an emphasis on database management using SQL Server. It covers key concepts such as creating databases, using ADO.NET for data access, and performing CRUD operations (Create, Read, Update, Delete) in C#. Additionally, it provides examples of connecting to a database, retrieving data, and implementing user authentication through SQL queries.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‫س‬ ‫ح‬‫ل‬‫ا‬ ‫ل‬ ‫ع‬ ‫س‬‫ق‬ ‫م‬ ‫ل‬ ‫مع‬‫كل عل الح س تكن ل جي ال‬

‫ م وم ا وب‬/ ‫ية وم ا وب و و و ا و اب‬


3rd Class – 2nd Course

Subject: Programming Language II (Advance C#)

Lecturer: Dr. Yousif A. Hamad


Lecture No 3-4
C# Objects (controls)
1. Create SQL server Database
2. String Connection
2. DataGrid View
3. Examples read all data from Database

Objectives of using SQL Server

• Be introduced to technologies used for accessing databases


• Become familiar with the [Link] classes
• Write program statements that use the DataReader class to retrieve database data
• Access and update databases using the DataSet and DataAdapter classes

What are the Databases

✓ Databases store information in records, fields, and tables

✓ Database management system (DBMS):computer programs used to manage and


query databases

o Example DBMSs include SQL server, Oracle, and Access Many DBMSs store
data in tabular formatData in tables are related through common data field
keys

Database Access

✓ Typically use a query language to program database access Structured query


language (SQL)
✓ ActiveX Data Objects ([Link]): .NET data access technology for accessing data in
databases
Data Providers

o Microsoft SQL ServerApplications using SQL Server 7.0 or later


o
o OracleApplications using Oracle data sources
o
o Object Linking and Embedding Database (OLE DB) Applications that use
Microsoft Access databases
o
o Open Database Connectivity (ODBC) Applications supported by earlier versions of
Visual Studio, access driver (.mdb) and Microsoft ODBC for Oracle

➢ Classes are encapsulated into a different namespace by provider


➢ Four core classes make up each data provider namespace
1. Connection
2. Command
3. DataReader
4. DataAdapter
1. Connection – To work with the data in a database, the first obvious step is the connection.
The connection to a database normally consists of the below-mentioned parameters.

1. Database name or Data Source – The first important parameter is the database name to which
the connection needs to be established. Each connection can only work with one database at a time.
2. Credentials – The next important aspect is the username and password which needs to be used
to establish a connection to the database. It ensures that the username and password have the
necessary privileges to connect to the database.
3. Optional parameters – For each database type, you can specify optional parameters to provide
more information on how .net should handle the connection to the database. For example, one can
specify a parameter for how long the connection should stay active. If no operation is performed
for a specific period of time, then the parameter would determine if the connection has to be closed.

2. Selecting data from the database – Once the connection has been established, the next
important aspect is to fetch the data from the database. C# can execute ‘SQL’ select
command against the database. The ‘SQL’ statement can be used to fetch data from a
specific table in the database.
3. Inserting data into the database – C# can also be used to insert records into the
database. Values can be specified in C# for each row that needs to be inserted into the
database.
4. Updating data into the database – C# can also be used to update existing records into
the database. New values can be specified in C# for each row that needs to be updated into
the database.
5. Deleting data from a database – C# can also be used to delete records into the database.
Select commands to specify which rows need to be deleted can be specified in C#. Ok, now
that we have seen the theory of each operation, let’s jump into the further sections to look
at how we can perform database operations in C#.
How to create data base (SQL server) and table in Visual Studio

Step 1:

Step 2:
Step 3:

Step 4: Set the Column Name and type of Information as char or int or ……

Save After Finishing the Table structure


Step 5: Adding the information to the Table if Needed

- Example of reading all data from db to display in List View

/*In order to able to use all Sql command you must call Sql library (using
[Link])*/
using [Link];
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=Server Name


\\SQLEXPRESS;Initial Catalog=DataBase Name;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*/
[Link]();

/*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 [Link] to execute the


reading operation.*/

SqlDataReader r = [Link]();

/*we use the while loop statement to keep (SqlDataReader) working (read) until
to finish all the column continents in the table.*/

while ([Link]())
{
ListViewItem add = new ListViewItem();
[Link]=r["studentname"].ToString();
[Link](r["studentage"].ToString());
[Link](r["studentdepartment"].ToString());

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

[Link](add);
}
/*Close the connection between Microsoft Visual Studio and Microsoft SQL
Server*/

[Link]();

}}}
Example of reading all data from db to display in DataGrid view

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace AABB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("data source =(localdb)\\ProjectsV13;
Initial catalog=ADB;Integrated security=True");

private void Button1_Click(object sender, EventArgs e)


{
[Link]();

SqlCommand cmd = new SqlCommand("select * from ATB",con);


SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
[Link](dt);
[Link] = dt;

[Link]();
} }}
Example of Checking data in db (column) using textbox

private void Button2_Click(object sender, EventArgs e)


{
[Link]();
SqlCommand cmd = new SqlCommand("select * from StudentInfo where
Lname=@LN",con);
[Link]("@LN", [Link]);

SqlDataReader sdr1 = [Link]();

if ([Link]())
[Link]("data Available in DB");
else
[Link]("data not Available in DB");
[Link]();
}
Example of Login reading username and password from database

using [Link];
namespace StudentinfoSystem
{
public partial class LoginForm :
Form
{
public LoginForm()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection("Data source =(localdb)\\ProjectsV13;


Initial catalog=StudentDBSystem;Integrated security=true");

private void Button1_Click(object sender, EventArgs e)


{
[Link]();
SqlCommand cmd = new SqlCommand("select * from LoginInfo where
Username=@Usr and Password=@Pass", con);

[Link]("@Usr", [Link]);
[Link]("@Pass",[Link]);

SqlDataReader sdr1 = [Link]();

if ([Link]())
{
Form1 f1 = new Form1();
[Link]();
[Link]();
}
else
[Link]("Not correct Pass or user name");
[Link]();
} }}

You might also like