0% found this document useful (0 votes)
11 views21 pages

Lecture 17 Data Base Connectivity

Uploaded by

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

Lecture 17 Data Base Connectivity

Uploaded by

mf0432663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Visual Programming

Database Connectivity
C# Database Connection
Accessing Data from a database is one of the important aspects of any
programming language. It is an absolute necessity for any programming
language to have the ability to work with databases.

It can work with different types of databases. It can work with the
most common databases such as Oracle and Microsoft SQL Server.
 But with every database, the logic behind working with all of them is
mostly the same.
The first step in a C# application is to create an instance of the Server
object and to establish its connection to an instance of Microsoft SQL
Server.
Fundamentals of Database connectivity

In working with databases, the following are the concepts which are
common to all databases
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-
 Database name or Data Source parameter is the database name to
which the connection needs to be established. Each connection can
only work with one database at a time.
 Credentials: 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.
 Optional parameters: example, one can specify a parameter for
how long the connection should stay active.
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.
3)Inserting data 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
New values can be specified in C# for each row that needs to be updated
into the database.
5)Deleting data from a database
Select commands to specify which rows need to be deleted can be
specified in C#.
How to connect C# to Database

In our example, we will connect to a database which has the name of


Demodb. The credentials used to connect to the database are given
below
Username – sa
Password – demo123
We will see a simple Windows forms application to work with databases.
We will have a simple button called "Connect" which will be used to
connect to the database.
How to connect C# to Database

Now double click the form so that an event handler is added to the code
for the button click event. In the event handler, add the below code.
Coding Explanation
• Access data with the SqlDataReader
• A table called demotb. This table will be used to store the ID and names of various
Tutorials.
• The table will have 2 columns, one called "TutorialID" and the other called
"TutorialName.“

• Step 1) Let's split the code into 2 parts so that it will be easy to understand for the
user.
• The first will be to construct our "select" statement, which will be used to read the
data from the database.
• We will then execute the "select" statement against the database and fetch all the
table rows accordingly.
Access data with the SqlDataReader
Code explanation
In the final step, we will just display the output to the user and close all
the objects related to the database operation.
C# Insert Into Database

just like Accessing data, C# has the ability to


insert records into the database as well.

Output
C# Insert Into Database
Code explanation
C# Update Database
• , C# has the ability to update existing records from the database as well.
C# Update Database
C# Update Database
Code explanation:
Deleting Records
Deleting Records
• https://www.guru99.com/c-sharp-access-data
base.html

END
• THANK U 

You might also like