The Dataset: System - Data.Dll System - XML.DLL
The Dataset: System - Data.Dll System - XML.DLL
NET Introduction
It is a module of .Net Framework which is used to establish connection
between application and data sources. Data sources can be such as SQL
Server and XML. ADO.NET consists of classes that can be used to connect,
retrieve, insert and delete data.
All the ADO.NET classes are located into System.Data.dll and integrated
with XML classes located into System.Xml.dll.
ADO.NET has two main components that are used for accessing and
manipulating data are the .NET Framework data provider and the DataSet.
The DataSet
It is used to access data independently from any data resource. DataSet
contains a collection of one or more DataTable objects of data. The following
diagram shows the relationship between .NET Framework data provider and
DataSet.
Fig: ADO.NET Architecture
The .NET Framework provides the following data providers that we can use in
our application.
.NET Framework Data It provides data access for Microsoft SQL Server. It requires
Provider for SQL Server the System.Data.SqlClient namespace.
EntityClient Provider It provides data access for Entity Data Model applications. It
requires the System.Data.EntityClient namespace.
.NET Framework Data It provides data access for Microsoft SQL Server Compact
Provider for SQL Server 4.0. It requires the System.Data.SqlServerCe namespace.
Compact 4.0.
Object Description
DataReade It is used to read data from data source. The DbDataReader is a base clas
r for all DataReader objects.
DataAdapt It populates a DataSet and resolves updates with the data source. The bas
er class for all DataAdapter objects is the DbDataAdapter class.
The .NET Framework Data Provider for SQL Server classes is located in
the System.Data.SqlClient namespace. We can include this namespace in
our C# application by using the following syntax.
1. using System.Data.SqlClient;
Class Description
SqlConnectio It is used to create SQL Server connection. This class cannot be inherited
n
SqlDataRead It is used to read rows from a SQL Server database. This class cannot b
er inherited.
SqlException This class is used to throw SQL exceptions. It throws an exception whe
an error is occurred. This class cannot be inherited.
1. using System.Data;
2. using System.Data.OracleClient;
.NET Framework Data It is good for single-tier applications that use Microsof
Provider for OLE DB Access databases.
.NET Framework Data It is good for middle and single-tier applications that us
Provider for ODBC ODBC data sources.
.NET Framework Data It is good for middle and single-tier applications that us
Provider for Oracle Oracle data sources.
It will prompt for database connection. Provide the server name and
authentication.
After successful connection, it displays the following window.
2. Creating Database
After creating database, now, let's create a table by using the following
C# code. In this source code, we are using created student database
to connect.
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database=s
tudent; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("create table st
udent(id int not null,
20. name varchar(100), email varchar(50), join_date date)"
, con);
21. // Opening Connection
22. con.Open();
23. // Executing the SQL query
24. cm.ExecuteNonQuery();
25. // Displaying a message
26. Console.WriteLine("Table created Successfully");
27. }
28. catch (Exception e)
29. {
30. Console.WriteLine("OOPs, something went wrong."+e);
31. }
32. // Closing the connection
33. finally
34. {
35. con.Close();
36. }
37. }
38. }
39. }
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database=s
tudent; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("insert into stud
ent
20. (id, name, email, join_date)values('101','Ronald Trump'
,'ronald@example.com','1/12/2017')", con);
21. // Opening Connection
22. con.Open();
23. // Executing the SQL query
24. cm.ExecuteNonQuery();
25. // Displaying a message
26. Console.WriteLine("Record Inserted Successfully");
27. }
28. catch (Exception e)
29. {
30. Console.WriteLine("OOPs, something went wrong."+e);
31. }
32. // Closing the connection
33. finally
34. {
35. con.Close();
36. }
37. }
38. }
39. }
Execute this code by using Ctrl+F5 and it will display the following
output.
5. Retrieve Record
Here, we will retrieve the inserted data. Look at the following C# code.
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database=s
tudent; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("Select * from s
tudent", con);
20. // Opening Connection
21. con.Open();
22. // Executing the SQL query
23. SqlDataReader sdr = cm.ExecuteReader();
24. // Iterating Data
25. while (sdr.Read())
26. {
27. Console.WriteLine(sdr["id"] + " " + sdr["name"]+"
"+sdr["email"]); // Displaying Record
28. }
29. }
30. catch (Exception e)
31. {
32. Console.WriteLine("OOPs, something went wrong.\
n"+e);
33. }
34. // Closing the connection
35. finally
36. {
37. con.Close();
38. }
39. }
40. }
41. }
Execute this code by Ctrl+F5 and it will produce the following result.
This displays two records, one we inserted manually.
Output:
6. Deleting Record
This time student table contains two records. The following C# code delete
one row from the table.
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database=studen
t; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("delete from student
where id = '101'", con);
20. // Opening Connection
21. con.Open();
22. // Executing the SQL query
23. cm.ExecuteNonQuery();
24. Console.WriteLine("Record Deleted Successfully");
25. }
26. catch (Exception e)
27. {
28. Console.WriteLine("OOPs, something went wrong.\n"+e);
29. }
30. // Closing the connection
31. finally
32. {
33. con.Close();
34. }
35. }
36. }
37. }
Output:
Play Videox
Connection does not close explicitly even it goes out of scope. Therefore, you
must explicitly close the connection by calling Close() method.
SqlConnection Signature
1. public sealed class SqlConnection : System.Data.Common.DbConnection, ICl
oneable, IDisposable
SqlConnection Constructors
Constructors Description
SqlConnection Methods
Method Description
ChangePassword(String, It changes the SQL Server password for the user indicated i
String) the connection string.
SqlConnection Example
Now, let's create an example that establishes a connection to the SQL
Server. We have created a Student database and will use it to connect.
Look at the following C# code.
Play Videox
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().Connecting();
10. }
11. public void Connecting()
12. {
13. using (
14. // Creating Connection
15. SqlConnection con = new SqlConnection("data source=.;
database=student; integrated security=SSPI")
16. )
17. {
18. con.Open();
19. Console.WriteLine("Connection Established Successfully");
20. }
21. }
22. }
23. }
Output:
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().Connecting();
10. }
11. public void Connecting()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database=studen
t; integrated security=SSPI");
18. con.Open();
19. Console.WriteLine("Connection Established Successfully");
20. }
21. catch (Exception e)
22. {
23. Console.WriteLine("OOPs, something went wrong.\n"+e);
24. }
25. finally
26. { // Closing the connection
27. con.Close();
28. }
29. }
30. }
31. }
Output: