intern_adodotnet_sqlconnection.md - brainchildservices/curriculum GitHub Wiki
What is a Database?
A database is a systematic collection of data. They support electronic storage and manipulation of data. Databases make data management easy. In a server we can create n number of database, the only limitation is the storage. each and every database can contain several no of tables.
For More Please Refer
Purpose Database?
Databases are useful in many different scenarios for storing data. It is typical to use a database when different sets of data needs to be linked together, such as:
- Pupils in a school and their grades
- Customer records and sales information
- Patients’ and doctors’ records
- Transactions between different bank accounts
- Taxpayers and income tax payments
- And many more.
Different Types of Database Server
There are different types of database server, and they are also known as “Database Management System” or “Database Server Software”.
n-Tier Architecture
An N-tier architecture divides an application into logical layers and physical tiers. Layers are a way to separate responsibilities and manage dependencies. A traditional three-tier application has a presentation tier, a middle tier, and a database tier. The middle tier is optional.
N-tier architecture is also called multi-tier architecture because the software is engineered to have the processing, data management, and presentation functions physically and logically separated. That means that these different functions are hosted on several machines or clusters, ensuring that services are provided without resources being shared and, as such, these services are delivered at top capacity. The “N” in the name n-tier architecture refers to any number from 1.
For More Ref:- https://stackify.com/n-tier-architecture/
ADO.NET SqlConnection Class in C# with Examples
Before working with the database, you must import a data provider namespace, by placing the following in the beginning your code module.
Let’s Start with a real life example:- Once guy went to purchase an APARTMENT . To enter the premises, he has to pass the security check. As per security concerns they won’t allow someone to enter in their property without some requirements (Eg: - An Appointment Letter, or Entry ID). So for that he must have to meet the requirements. After the security checking he gets his entry. Once he enters the place a manager came to assist him with his choices. Once the manages sort out your choices he found me an apartment at the best of my choices and showed me how the apartment look like as per the requirements.
- Let’s say our guy is the user
- Apartments as Database name
- The security guy as our ConnectionString. To check all requirement are met or not, to enter the database. The requirements are: • Username – Entry ID • Password – Appointment Letter. • Database Name – Apartments
- Then a manager came to help him to find his requirements. Because it’s not an easy task to go and visit all the apartments personally. So an expert came to sort thing out.
- Once he finished with this analysis he showed the perfect apartment as per the requirements.
Connection to an ADO.NET Database
Before working with the database, you must import a data provider namespace, by placing the following in the beginning your code module.
For SqlClient .NET data provider namespace import code:
ConnectionOpen()
In the above example as you can see the security came to check whether our guy is a genuine customer or not. Like that how our ConnectionOpen() is working, because whenever you give a connection string it always check whether the credential are matching or not.
As per the example our ConnectionString will come like this:
SqlConnection connection = new SqlConnection("Data Source= (local); Initial Catalog= APARTMENT; User ID=ENTRYID; pwd=APPOINTMENTLETTER" Integrated Security=”True”);
Connection.Open()
Connection.Close()
as you can see once all the credential are have to match to open a connection with the database. because database contains sensitive data, like Name, Contact Details, etc.
ConnectionClose()
Closes the connection to the database. This is the preferred method of closing any open connection.
an Error you Might See if you dont close the connection
System.InvalidOperationException: The connection was not closed. The connection's current state is open
Work Flow
Major Properties of a Connection String:-
- Server Name - Represents the name of the Server. Without knowing the server name we cannot access it.
- Database Name - Represents the name of the Server. Without knowing the server name we cannot access it.
- Username & Password - Username & Password for Security
- Time Out - When you try to open a connection but if its not getting connected it will try to open the connection for a long time, so if we give the timeout property it will stop the process automatically at the time limit.
For More please Refer :- https://dotnettutorials.net/lesson/ado-net-sqlconnection-class/