In this example we will briefly touch ADO.Net , to understand
dataset, data-adapter, command
|
Ado.Connected and ADO.Disconnected Model. : In the connected
model the you code will connect and disconnect with the data source.
There are three classes will mainly hold the information to get back
to the data source. Where as in the dis-connected model Data
can be obtained once, then remains disconnected from the data
source.
LINQ from Microsoft offering a choice to the programmer Not
Knowing enough of ADO.NET (Good or Bad too)
- ADO.Connected , threre are three main and fundamental
classes. These classes will store data using these classes.
- Connection : SqlConnection
- command: SqlCommand
- DataReader : Read the data base (it is faster than
DadaAdapter) forward only
- ADO Disconnected
- DataAdapter : SqlDataAdapter : transfer data from
physical database to the memory.
- will have choice of choosing the command type for batch
operation.
- DataTable : in-memory database
table to work with
- DataView : in memory data view
- DadaSet : in-memory Dataset capable
to edit original data in bulk. There fore there is no need of
keeping the connection-link alive
- DataRow Extract Data, serialize as
in a Label.Text from DataSet.Table
- ForEach loop
- using (SqlConnection connection = new SqlConnection(connectionString))

|
Objectives to Learn
- Creating New DataBase Using Server Explorer.
- Crating a table as Database owner as dbo,
- Here the default page will have the "Connection String to
the database.
- Note :
- When you are adding an existing database, Visual Studio
just creating a Replica or Representation-Copy of the
original Database, meaning that one point many users can
use/edit/develop this database, ( Note: think about
Conflicts during simultaneous edit/commit/new data entry--
we deal with those conflicts later).
- Connection to database would connect you to the
database, add a string to Web.config
- Using Dataset
- Using Code We created two SQL table and also showed that
same can be done using Generate Script
- Retrieving Row values from GridView , Using 'foreach loop" ,
DataSet and DataRow subsequently.
|

|
Step 1: Create New Web Site (
http://manas6/aspnet.35/mm.codedataset/ )

|
Step 2 Create new Database using


|
Step 3: Create a table using this code or use GUI to create the
table

CREATE TABLE Dset1
( sid nchar(10) NOT NULL, FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
Note varchar(50) NULL)
You may also use this DROP statement to destroy the table and
recreate it. However, you may skip this learning SQL language and
use Generate Change Script to save the code for you.

/*
Thursday, November 13, 20084:39:18 PM
User:
Server: MANAS6\SQLEXPRESS
Database: CodeDataSet
Application:
*/
/* To prevent any potential data loss issues, you should review this
script in detail before running it outside the context of the
database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Table1
(
id nchar(10) NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
Note varchar(MAX) NULL
) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.Table1 ADD CONSTRAINT
PK_Table1 PRIMARY KEY CLUSTERED
(
id
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
|
Step 4 : create another table dsMain that will hold "mid" and
will be used to create a relationship with the other tables.

Add/Fill data using the Visual tool " Server Explorer > Tables >
Show Table Data

|
Now we start editing the existing code in Default.aspx.cs
//wrong
//Data Source=MANAS6\SQLEXPRESS;Initial Catalog=CodeDataSet;Integrated
Security=True;Pooling=False
//correct
// Data Source=MANAS6\\SQLEXPRESS;Initial Catalog=CodeDataSet;Integrated
Security=True;Pooling=False
private string strCnn= "Data
Source=MANAS6\\SQLEXPRESS;Initial Catalog=CodeDataSet;Integrated
Security=True;Pooling=False";
// private string strcnn2 = "Data Source=(MANAS6\\SQLEXPRESS);Initial
Catalog=CodeDataSet;Integrated Security=True;Pooling=False";
|
Now add two Label and One GirdView control

|
Code Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" Debug="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.CodeDataSet: Query.SQL</title>
<style type="text/css">
body { background-color :Gray; }
#div1 { background-color:#FFFFCC; width: 350px; height:auto; }
#div2 { background-color:#FFFFCC; width: 350px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div >
<div id="div1"><asp:Label ID="L1" runat="server" Text="Connection:
Operation <br/>"></asp:Label>
</div>
<asp:GridView ID="GridView1" runat="server" BackColor="#CCFFCC"
Caption="Code Filled GridView"
>
</asp:GridView><br />
<div id="div2"><asp:Label ID="L2" runat="server" Text="Read Rows as
String : </br/>"></asp:Label></div>
</div>
</form>
</body>
</html>
|
Code Default.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
//
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
//wrong
//Data Source=MANAS6\SQLEXPRESS;Initial Catalog=CodeDataSet;Integrated
Security=True;Pooling=False
//correct
// Data Source=MANAS6\\SQLEXPRESS;Initial Catalog=CodeDataSet;Integrated
Security=True;Pooling=False
private string strCnn= "Data Source=MANAS6\\SQLEXPRESS;Initial
Catalog=CodeDataSet;Integrated Security=True;Pooling=False; ";
// private string strcnn2 = "Data Source=(MANAS6\\SQLEXPRESS);Initial
Catalog=CodeDataSet;Integrated Security=True;Pooling=False";
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = GetConnectionString();
data_loader(connectionString);
}
protected void data_loader(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
//Create a SqlDataAdapter for the Suppliers table.
SqlDataAdapter adapter = new SqlDataAdapter();
// A table mapping names the DataTable.
adapter.TableMappings.Add("Table", "Dest1");
try
{
// Open the connection.
connection.Open();
L1.Text += "<br/>The SqlConnection is open.";
// Create a SqlCommand to retrieve Dset1
data.
SqlCommand command = new SqlCommand("SELECT
* FROM dbo.Dset1;",connection);
command.CommandType = CommandType.Text;
L1.Text += "<br/>The command text ." + command.ToString();
// Set the SqlDataAdapter's SelectCommand.
adapter.SelectCommand = command;
L1.Text += "<br/>The DataAdpater worked through command .";
// Fill the DataSet.
DataSet dataSet1 = new DataSet("Dset1");
/*
* as an alternate we could have used i
* DataSet dataSet2 = new DataSet();
* adapter.Fill(dataSet2, "Dset1");
*/
adapter.Fill(dataSet1);
L1.Text += "<br/>The Is Filled .Databale started ";
GridView1.DataSource = dataSet1;
GridView1.DataBind();
L2.Text += "Show Id, FirstName LastName";
foreach (DataRow row in dataSet1.Tables["Dest1"].Rows)
{
L2.Text += "-----<br/> " + row["id"].ToString() + " ---" +
row["FirstName"].ToString() + " --- " +row["LastName"].ToString();
}
}
catch (Exception error)
{
L1.Text = error.Message.ToString();
}
finally
{
connection.Close();
L1.Text += "<br/>The Connection Closed .";
}
}
}
private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
//Data Source=MANAS6\SQLEXPRESS;Initial Catalog=CodeDataSet;Integrated
Security=True;Pooling=False
return strCnn;
}
}
|
An alternate way to fill SQLDataAdapter

|
Runtime analysis, and clues to understand GrivView Events

|
|
|
|
Mix -Match & Disconnected model

MSDN : Source bridging two concepts connected and disconnected
The .NET Framework Data Providers are components that have been
explicitly designed for data manipulation and fast, forward-only,
read-only access to data.
- The Connection object
provides connectivity to a data source.
- The Command object enables
access to database commands to return data, modify data, run
stored procedures, and send or retrieve parameter information.
- The DataReader provides a
high-performance stream of data from the data source. Finally,
the DataAdapter provides the bridge
between the DataSet object and the
data source.
- The DataAdapter uses
Command objects to execute SQL
commands at the data source to both load the
DataSet with data and reconcile changes that were made to
the data in the DataSet back to the
data source. For more information, see
.NET Framework Data Providers (ADO.NET) and
Retrieving and Modifying Data in ADO.NET.
Choosing a DataReader (Connected ) or a
DataSet (Disconnected)
When you decide whether your application should use a
DataReader (see
Retrieving Data Using a DataReader (ADO.NET)) or a
DataSet (see
DataSets, DataTables, and DataViews (ADO.NET)), consider the
type of functionality that your application requires. Use a
DataSet to do the following:
- Cache data locally in your application so that you can
manipulate it. If you only need to read the results of a
query, the DataReader is the
better choice.
- 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.
- Perform extensive processing on data without requiring
an open connection to the data source, which frees the
connection to be used by other clients.
If you do not require the functionality provided by the
DataSet, you can improve the
performance of your application by using the
DataReader to return your data in a forward-only,
read-only manner. Although the DataAdapter
uses the DataReader to fill the
contents of a DataSet (see
Populating a DataSet from a DataAdapter (ADO.NET)), by using
the DataReader, you can boost
performance because you will save memory that would be consumed
by the DataSet, and avoid the
processing that is required to create and fill the contents of
the DataSet.
|
Connected ADO :
|
|
Some Tips
CREATE
USE [adoConnect1]
GO
/****** Object: Table [dbo].[Main] Script Date: 11/25/2008
12:58:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Main](
[sid] [int] NOT NULL,
[Job] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL,
[FirstName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL,
[LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL,
[Note] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_Main] PRIMARY KEY CLUSTERED
(
[sid] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
INSERT
INSERT INTO [adoConnect1].[dbo].[Main]
([sid] ,[Job],[FirstName],[LastName],[Note])
VALUES
(<sid, int,>,<Job, varchar(50),>,<FirstName, varchar(50),>,<LastName,
varchar(50),>,<Note, text,>)
UPDATE
UPDATE [adoConnect1].[dbo].[Main]
SET [sid] = <sid, int,>
,[Job] = <Job, varchar(50),>
,[FirstName] = <FirstName, varchar(50),>
,[LastName] = <LastName, varchar(50),>
,[Note] = <Note, text,>
WHERE <Search Conditions,,>
DELETE
DELETE FROM [adoConnect1].[dbo].[Main]
WHERE <Search Conditions,,>

|
|
|