0% found this document useful (0 votes)
20 views

C# Unit - V

Uploaded by

btechcse21052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C# Unit - V

Uploaded by

btechcse21052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

2 Marks
1. What is ADO .Net? (Nov 2018)
ADO.NET is a set of classes that expose data access services to the .NET programmer.
ADO.NET provides a rich set of components for creating distributed, data-sharing applications.
It is an integral part of the .NET Framework, providing access to relational data, XML, and
application data. ADO.NET supports a variety of development needs, including the creation of
front-end database clients and middle-tier business objects used by applications, tools, languages,
or Internet browsers.

2. Where are the ADO .NET Classes found?


The ADO.NET classes are found in System.Data.dll, and are integrated with the XML
classes found in System.Xml.dll. When compiling code that uses the System.Data namespace,
reference both System.Data.dll and System.Xml.dll.

3. Draw the ADO.NET Architecture

4. What are the Uses of a DataSet?


 Remote data between tiers or from an XML Web service.
 Interact with data dynamically such as binding to a Windows Forms control or combining
and relating data from multiple sources.
 Cache data locally in your application.
 Provide a hierarchical XML view of relational data and use tools like an XSL
Transformation or an XML Path Language (XPath) Query on your data. For more
information, see XML and the DataSet.

U20CSCM02 - C# AND .NET PRGRAMMING 1


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

 Perform extensive processing on data without requiring an open connection to the data
source, which frees the connection to be used by other clients.
5. How do you compile and run the ADO.NET sample application?
1. Using Notepad or another text editor, create a blank text file named sample.vb for Visual
Basic sample code or named sample.cs for C# sample code.
2. Copy and paste the Visual Basic or C# sample code from this topic into the blank text
file. Save the file.
3. Open a command prompt (Start, then Run, then enter "command").
4. In the command prompt, change the directory to the directory that contains the new text
file. For example:
cd\SampleCode\ADONETSample
5. In the command prompt, enter one of the following commands to compile the sample (the
path to your file will likely differ).
6. For Visual Basic, use vbc.exe and use the following command to reference the system
libraries needed to run your ADO.NET application.
vbc.exe sample.vb /r: System.dll /r:System.Data.dll /r:System.Data.OracleClient.dll
/r:System.Xml.dll

6. What are .NET Data Providers?


A data provider in the .NET Framework serves as a bridge between an application and a
data source. A data provider is used to retrieve data from a data source and to reconcile changes
to that data back to the data source.

7. Explain with diagram .NET Data Providers

U20CSCM02 - C# AND .NET PRGRAMMING 2


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

8. List the .NET Framework data providers that are included in the .NET Framework?
(Nov 2018)

9. What are the Core elements of NET Framework data provider model.
The Connection, Command, DataReader, and DataAdapter objects represent the core
elements of the .NET Framework data provider model.

10. Explain about the classes in data provider model?

U20CSCM02 - C# AND .NET PRGRAMMING 3


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

11. What is ADO.NET Dataset


The ADO.NET DataSet contains DataTableCollection and their DataRelationCollection . It
represents a collection of data retrieved from the Data Source. We can use Dataset in
combination with DataAdapter class. The DataSet object offers a disconnected data source
architecture. The Dataset can work with the data it contain, without knowing the source of the
data coming from. That is , the Dataset can work with a disconnected mode from its Data Source
. It gives a better advantage over DataReader , because the DataReader is working only with the
connection oriented Data Sources.

12. Explain with a diagram the ADO.NET Dataset

13. How will you fill a DataSet using a TableAdapter


1. Create a new project in Visual Basic IDE.
2. On the Database Explorer click the icon for creating new data connection and
3. Choose the SQL Server file.
4. Establish the connection and you should be seeing the database objects on the window.
5. In the solution Explorer click on the project name and
6. Choose add an item option.
7. In the dialog box that opens choose DataSet item and

U20CSCM02 - C# AND .NET PRGRAMMING 4


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

8. Name it as ds and click ok.


9. You will see the DataSet item added to the solution and also
10. A blank screen will be seen.
11. From the Database explorer drag and drop the table ProductCategory.
12. Choose „not‟ in the message box that asks your permission to add the datafile as a project
data.
13. Now right click on the Form1 and
14. Choose the option to see the code window.
15. Type the following codes to fill the DataSet

14. How will you fill a DataSet by using a SqlDataAdapter?


1. Create a SqlConnection object. SqlDatAdapter object.
2. The SqlConnection object needs connection string as an argument and the SqlDataAdapter
requires the SQL Statement and Connection Object as an argument.
3. The ConnectString gives details about the Database Server, Initial Catalogue, connection
type, userid and password.
4. A typical connection string could look like this:
"data source=sql.domain.no; initial catalog=xxxxx; User ID=xxxxx;pwd=xxxxx; Integrated
Security=SSPI”
Code for filling the DataSet is given below:
Dim SQLStr As String
Dim ConStr As String
SQLStr = "SELECT Name FROM production.ProductCategory"
ConStr = "data source=sql.domain.no; initial catalog=xxxxx; User ID=xxxxx;pwd=xxxxx;
Integrated Security=SSPI"
Dim sqlConn As New System.Data.SqlClient.SqlConnection(ConStr)
Dim ds As New DataSet
Dim SQLAdapter As New System.Data.SqlClient.SqlDataAdapter(SQLStr, ConStr)
SQLAdapter.Fill(ds)

15. Write a simple program to display data in a dataset in a grid?


Imports system.Data
Public Class Form1
Dim ProductDataSet As ds
Private Sub DataLoad()

U20CSCM02 - C# AND .NET PRGRAMMING 5


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

Dim ProductTableAdapter As New dsTableAdapters.ProductTableAdapter()


ProductDataSet = New ds
ProductTableAdapter.Fill(ProductDataSet.Product)
End Sub

5Marks

1. WHAT IS MEANT BY ADO.NET(NOV 2013, NOV 2012)


 Every organization maintains data storage for its business, employees and clients.
 This data must be easily accessible and updateable.
 The machine in which the backend database is stored is known as server.
 This Database can be accessed and used by front end applications through some
connectivity.
 The machine in which the application run is known as client.
 Previously ODBC was used for this purpose.
ODBC – open database connectivity local/remote.
ADO – Active Data Object.
 It is defined as object model.
 It comprises of objects, methods and properties.
 Methods – Access and update Data Sources.
 Properties – Control the behavior of the method.
ADO.NET OBJECT MODEL

U20CSCM02 - C# AND .NET PRGRAMMING 6


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

Figure for ADO.NET Object Model

The .Net Framework comprises of


1. Connection Oriented (or) Data Providers.
2. Disconnection Oriented (or) Datasets.

1. Connection Environment/ Data Providers:


In connected the application makes a connection to the Data source and then interacts with it
through SQL request using the same connection. In these cases the application stays connected to
DB system even when it is not using any Database operations.
2. Disconnected Oriented/ Datasets:
A datasets is an in – memory data store that can hold multiple tables at the same time. Datasets
can only hold data and do not interact with a data source.

10 Marks
1.EXPLAIN DIRECT ACCESS (or) .NET DATA PROVIDERS (or) CONNECTION
ENVIRONMENT (Nov 2018) (May-22)
Data providers act as a bridge between an application and database. It is used to retrieve data
from a database and send back to the database after changes.
Figure for Connection Environment

U20CSCM02 - C# AND .NET PRGRAMMING 7


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

Connection Object:
 These objects are used to create a connection to the database for moving data
between the database and user application.
 The connection can be created using.
SQL Connection Object:
 Open (), close (), and dispose () are the methods of connection object used to open
and close the defined connection.

Dim conn As SQLConnection


conn = New SQLConnection ()
conn.connectionstring =”datasource =asdf; initial catalog =stud; userId =sa;
pwd =123”
conn.open ()
Datasource is the name of the SQL server which is to be connected.
Initial Catalog is the name of the database.
Used Id is the SQL server login account and pwd is the password.
The user must close the connection after using it. This is done by either close (or) dispose
method.
conn.close ()
Command Objects: (May-22)
These command objects are used to read, add, update and delete records in a database. These
operations are done by sql command objects.
Methods: Execute Reader ()
Execute Non Query ()
Properties: connection, command text.
Execute Reader () method is used to execute the commands and retrieve rows from the database.
Execute Non Query () method is used to execute the command without retrieving any row.
Dim conn As sqlconnection
Dim comm As sqlcommand
comm. = New sql command ()
comm.connection = conn
comm.commandText = “select * from Stud Table”
U20CSCM02 - C# AND .NET PRGRAMMING 8
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

Data Reader Objects: (May-22)


These objects are used to read row wise data in read only format from databases. Sql Data
Reader object is used.
Execute Reader () – is used to retrieve a row from database.
Read () – is used to obtain a row from the result of the query.
Get String () – is used to access a column value in a selected row.
Close () – must be called after the use of Data Reader Objects.
Example:
Dim conn As Sqlconnection
Dim comm As Sqlcommand
Dim rd As SqlDataReader
conn = New Sqlconnection ()
conn = New Sqlconnection ()
conn.connection string = “Datasource = asdf; Initial catalog =college; UserId =sa; pwd =123”
conn.open ()
comm = New Sqlcommand ()
comm.connection = conn
comm.commandText = “select * from staff”
rd = comm.ExecuteReader
Data Adapter Objects: (May-22)
This object is used to exchange Data between a database and a Dataset. It reads data
from the database to the dataset and then writes the changed data back to the database.

Properties:
Select Command (Accessing Rows in a Database)
Insert Command (Inserting rows in the Database)
Update Command (For modifying rows in the Database)
Delete Command (For deleting rows)
FILL Method:
It is used to write the result of select command into the dataset.
Example:
Dim Myadapter As New SqlDataAdapter
Myadapter.select command = mycomm
Dim Mydataset As New Dataset ()
Myadapter.Fill (Mydataset, stud Table)
U20CSCM02 - C# AND .NET PRGRAMMING 9
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
conn.close ()
Sample C # Program for Connection Environment:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=CENTRE-I-62;Initial
Catalog=management;Integrated Security=True";
string query1 = "select * from employee";
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlCommand cmd1 = new SqlCommand(query1, cn);
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
Console.WriteLine(dr1[0] + " " + dr1[1] + " " + dr1[2] + " " + dr1[3] + " " + dr1[4]);
}
cn.Close();
Console.ReadLine();
}
}
}

3. EXPLAIN INDIRECT ACCESS (or) DATASETS (or) DISCONNECTED


ENVIRONMENT. (May-22)
Dataset is a temporary storage of records. The dataset represents a complete set of data including
tables and their relationships.

U20CSCM02 - C# AND .NET PRGRAMMING 10


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
Dataset must contain atleast one data table object in its data table collection.
Each Data Table contains both a
1. Data Row Collection
2. Data Column Collection
Objects of Datasets are
1. Data Table Collection
2. Data Tables
3. Data Row

Figure for Disconnected Environment


Data Table Collection Object:
It is used to provide one or more tables represented by data Table object. It contains collections
such as Data Column Collection, Data Row Collection.
Data Table Object:
It contains a single table of memory resident data. It contains a collection of rows represented by
Data Row Collection, a collection of columns represented by data Column collection.
Data Row Object:
It contains methods and propeties, to retrieve, insert, delete and update the values in the Data
Table.
Add () – Add a new Data Row in the Data Collection.
Remove () – Delete a Data Row from the Data Row Collection
Accept Changes () – Confirm the Addition.
New Row () – Add New Row.
Example:
Dim conn As New Sqlconnection (“Datasource =asfd; Initial Catalog = college; UserId =sa; pwd
=123”)

U20CSCM02 - C# AND .NET PRGRAMMING 11


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
Conn.open ()
Dim ds As dataset = New dataset ()
Dim r As DataRow = ds.Tables (“staff”).NewRow ()
R (0) =”E008”
R (1) =”Johans”
R (2) = 12500
Ds.Tables (“staff”).Rows.Add (R) )

Working of Datasets:
1. Create a connection object
Sql connection cn = New Sqlconnection ();
2. Set a connection String.
Cn.connection String = “Datasource = SQL Server36; Initial Catalog = HR; UserId =sa;
pwd =123”
3. Create an oject of the Dataset class
Dataset Dataset1 = new Dataset ()
4. Create an object of Sql Data Adapter class.
Sql Data Adapter da = New Sql Data Adapter ()
5. Pass the SQL Query to the Command object.
Sql command cmd = New Sql command (“Select * from Employee”, cn)
da.select command = cmd
6. Fill Datasets (ie) Fill the records into the Datasets.
Da.Fill (Dataset1);

Two Types of Datasets


1. Typed Dataset
2. Untyped Dataset
1. Associated with XML schema created at the time of creation of the Dataset. It contains the
information above Datasets structures such as the tables columns and Rows.
2. Does not have associated XML schema. In untyped Dataset, the tables and columns are
represented as collections.
Sample C # Program for Disconnected Environment:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

U20CSCM02 - C# AND .NET PRGRAMMING 12


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=CENTRE-I-62;Initial
Catalog=management;Integrated Security=True";
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
DataSet ds = new DataSet("employee");
SqlDataAdapter da = new SqlDataAdapter("select * from employee", cn);
SqlCommandBuilder cmd1 = new SqlCommandBuilder(da);
da.Fill(ds, "employee");
DataRow r = ds.Tables["employee"].NewRow();
r["empcode"] = "E1005";
r["name"] = "SUNFLOWER";
r["role"] = "ASST PROF";
r["quali"] = "MTECH";

U20CSCM02 - C# AND .NET PRGRAMMING 13


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

r["salary"] = 20000;
ds.Tables["employee"].Rows.Add(r);
da.Update(ds, "employee");
Console.WriteLine("Updated");
cn.Close();
Console.ReadLine();
}
}}
PONDICHERRY UNIVERSITY QUESTIONS

2 MARKS
1. Define: Database. (NOV 2013)
2. What is the use of try block in VB.NET? (NOV 2013) (Ref.Qn.No.1, Pg.no.10)
3. What is the difference between copy() and clone()?(APRIL 2012)
4. Enumerate the distinct advantages of ADO.NET. (APR 2012) (Ref.Qn.No.11, Pg.no.5)
5. What are all the reason Exception can be raised? (NOV 2012)
6. List out the Exception Handling Model. (NOVEMBER 2012)
7. What is an Event? (APRIL 2013)
8. List the difference exists between command Text and Command Type. (APRIL 2013)

11 MARKS
1. Write a VB.NET program to sort the numbers using delegates. (NOVEMBER 2013)
2. Explain in detail about ADO.NET object model. (NOVEMBER 2013)
3. Write a program to add a new record in a database with following fields Student Id,
Student Name, Degree and Category. (APRIL 2012)
4. Explain in detail about the Object Model of the Dataset Class. (APRIL 2012)
5. Explain ADO.NET Object Model. (NOVEMBER 2012)
6. Discuss with Delegates. (NOVEMBER 2012)
7. Discuss about the properties of the connection class and command class. (APRIL 2013)
8. Explain delegates and events in detail. (APRIL 2013)

U20CSCM02 - C# AND .NET PRGRAMMING 14

You might also like