0% found this document useful (0 votes)
4 views3 pages

SqlConnection Con

The document explains how to connect to a SQL Server database using a SqlConnection and execute a query with a SqlDataAdapter. It describes the process of filling a DataSet with data from the database, making changes in memory, and then using a SqlCommandBuilder to generate SQL commands for updating the database. Finally, it details how to apply those changes back to the actual database using the Update method of the SqlDataAdapter.

Uploaded by

azam.2244666
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)
4 views3 pages

SqlConnection Con

The document explains how to connect to a SQL Server database using a SqlConnection and execute a query with a SqlDataAdapter. It describes the process of filling a DataSet with data from the database, making changes in memory, and then using a SqlCommandBuilder to generate SQL commands for updating the database. Finally, it details how to apply those changes back to the actual database using the Update method of the SqlDataAdapter.

Uploaded by

azam.2244666
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
You are on page 1/ 3

SqlConnection con = new

SqlConnection("your_connection_string");
Explanation:
This line creates a connection to the SQL Server database using
a connection string.
Think of it like opening a road to the database so you can send
or receive data.

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM


Students", con);
Explanation:
This line creates a data adapter. It runs the SQL query:
"SELECT * FROM Students" using the connection con.
The adapter acts like a bridge between the program and the
database — it helps to fetch data into your program and also
send updates back.

DataSet ds = new DataSet();


✅ Explanation:
This creates an empty DataSet, which is like a temporary in-
memory database in your program.
It can hold multiple tables from the real database.

da.Fill(ds, "Students");
✅ Explanation:
This line fills the ds (DataSet) with data from the "Students"
table of the real database.
So now ds.Tables["Students"] has all the student records.

// Make some changes to ds.Tables["Students"] here


✅ Explanation:
Here you can add, update, or delete rows in the Students table
of the DataSet.
But these changes are still only in memory — not yet saved to
the actual database.

SqlCommandBuilder cb = new SqlCommandBuilder(da);


✅ Explanation:
This line creates a SqlCommandBuilder object which
automatically generates SQL commands (INSERT,
UPDATE, DELETE) based on the changes you made in the
DataSet.
It works with the adapter da to understand what needs to be
updated.

da.Update(ds, "Students");
✅ Explanation:
This line takes all the changes you made to
ds.Tables["Students"] and updates the actual database with
those changes.

You might also like