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

Gis2409 - .Net Assignment

The document provides an overview of the SQL namespace in .NET, detailing key classes and methods for interacting with SQL databases, particularly Microsoft SQL Server. It covers common SQL methods such as opening and closing connections, executing queries, and manipulating data, as well as the syntax for creating tables, inserting, updating, and deleting records. The document serves as a guide for developers to effectively use ADO.NET for database operations.
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 views7 pages

Gis2409 - .Net Assignment

The document provides an overview of the SQL namespace in .NET, detailing key classes and methods for interacting with SQL databases, particularly Microsoft SQL Server. It covers common SQL methods such as opening and closing connections, executing queries, and manipulating data, as well as the syntax for creating tables, inserting, updating, and deleting records. The document serves as a guide for developers to effectively use ADO.NET for database operations.
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/ 7

ASSIGNMENT

.NET FRAMEWORK

TOPIC :- SQL namespace, SQL methods, Creating table, insert, update, delete

BY
SMRUTHI M, GIS2409
1ST METCH
SQL NAMESPACE

An SQL namespace in .NET is a collection of classes and methods that allow


developers to interact with SQL databases—mainly Microsoft SQL Server—through
ADO.NET (Active Data Objects for .NET).

It offers the tools to:

• Connect to a SQL Server

• Execute queries and stored procedures

• Read and manipulate results

Class Role

SQL Connection Opens and manages a connection to SQL Server

SQL Command Executes SQL queries and commands

SQL Data Reader Reads rows from a database

SQL Data Adapter Acts as a bridge between a database and a Dataset

Used to pass parameters to SQL queries safely (avoid SQL


SQL Parameter
injection)

ADO.NET Workflow

Here's a basic flow of how these classes interact:

1. Establish Connection: Use SQL Connection

2. Create Command: Use SQL Command to define your query

3. Execute: Use Execute Reader, Execute Non-Query, or ExecuteScalar

4. Read Data: Use SQL Data Reader or Dataset with SQL Data Adapter

5. Close Connection: Always close the connection or use blocks


COMMON SQL METHODS

1. Open ()

• Belongs to: SQL Connection

• Use: Opens a connection to the database.

C#

SQL Connection conn = new SQL Connection (connection String); conn.Open();

2. Close () / Dispose ()

• Belongs to: SQL Connection, SQL Data Reader

• Use: Closes the connection or reader to free resources.

C#

conn. Close (); // or use conn. Dispose (); reader. Close ().

3. Execute Reader ()

• Belongs to: SQL Command

• Use: Executes a SQL query that returns rows of data (like SELECT) and returns a
SQL Data Reader.

C#

SQL Command cmd = new SQL Command ("SELECT * FROM Students", conn);

SqlDataReader reader = cmd. executes Reader ();

4. Execute non-query ()

• Belongs to: SQL Command

• Use: Executes SQL commands that don’t return data (like INSERT, UPDATE,
DELETE).

• Returns: Number of affected rows.

C#
SQL Command cmd = new SQL Command ("DELETE FROM Students WHERE Id=1",
conn);int rows = cmd .Execute Non Query ();

5. ExecuteScalar ()

• Belongs to: SQL Command

• Use: Executes a query that returns a single value (like COUNT, MAX, etc.).

C#

SQL Command cmd = new SQL Command ("SELECT COUNT (*) FROM Students",
conn); int count = (int)cmd. Execute Scalar ();

6. Fill()

• Belongs to: SQL Data Adapter

• Use: Fills a Data Table or dataset with query results.

C#

SQL Data Adapter = new SQL Data Adapter ("SELECT * FROM Students", conn);

data Table = new data Table (); adapter. Fill(table);

7. Add () / Add With Value ()

• Belongs to: SQL Parameter Collection (inside SQL Command)

• Use: Adds parameters to SQL queries (to prevent SQL injection).

C#

Cmd. Parameters. Add With Value ("@Name", "John");

8. Begin Transaction (), Commit (), Rollback ()

• Belongs to: SQL Connection, SQL Transaction

• Use: Used for database transactions to group multiple operations.

C#

SQL transaction = conn. Begin Transaction (); cmd—transaction = transaction;


transaction. Commit (); // or transaction. Rollback ();
CREATE TABLE in SQL

The CREATE TABLE statement is used in SQL to define a new table in the database. A
table is a collection of rows and columns, where each column has a data type,
name, and optionally, constraints (like primary key or not null).

Syntax:

CREATE TABLE table name (

column1 datatype constraint,

column2 datatype constraint,

...

);

Components of CREATE TABLE:

Component Description

table name The name you give to the table

column
The name of each column (field)
name

datatype The type of data allowed in that column (e.g., INT, VARCHAR, DATE)

Rules applied to the column like PRIMARY KEY, NOT NULL, UNIQUE,
constraint
DEFAULT, FOREIGN KEY
INSERT – Add New Data

The INSERT INTO statement is used to insert new records in a table.

It is possible to write the INSERT INTO statement in two ways.

Syntax

The first way specifies both the column names and the values to be inserted.

If you are adding values for all the columns of the table, then no need to specify the column
names in the SQL query. However, make sure that the order of the values is in the same
order as the columns in the table.

1. INSERT INTO table name (column1, column2, column3, ...)

2. VALUES (value1, value2, value3, ...);

3.

4. '2nd way

5. INSERT INTO table name

6. VALUES (value1, value2, value3, ...);

UPDATE TABLE

The UPDATE statement is used to modify the existing records in a table.

Syntax

1. UPDATE table_name
2. SET column1 = value1, column2 = value2, ...

3. WHERE condition;

DELETE STATMENT IN SQL

The DELETE statement is used to delete existing records in a table for a particular Record.

Syntax

1. DELETE FROM table_name WHERE condition;

Example

1. DELETE FROM Employee WHERE EmpId=1;

In Employee table EmpId = 1 record gets deleted.

You might also like