Вы находитесь на странице: 1из 538

Visual

Studio .NET:

-
-
2003

6S1.3.06
32.973.26
63

63

. .
Visual Studio .NET: .
.: -, 2003. 544 .: .
ISBN 5-94157-309-
Microsoft .NET
Visual Studio .NET.
ADO.NET, Windows, , , , ,
. . .
, : Visual
Basic #.
Visual Studio .NET.


681.3.06
32.973.26

:

.
.







Via Design

02429 24.07.00. 25.04.03.


701001/16. . . . . 43,86.
3000 . 873
"-", 198005, -, ., 29.
, I* 77.99.02.53..001537.03.02
13.03.2002 . .

""
19034, -, 9 , 12.

ISBN 5-94157-309-

. . , 2003
, "-", 2003

10
11

1. ADO.NET

14

1.1, ADO.NET
.
.2.
.3. DataSet
.4. DataSet
.5. XML
.6. ,
1.2. ADO.NET ADO
1.2.1.
1.2.2.
1.2.3.
,
1.2.4.
1.3. ( Connection)
1.3.1.
1.3.2.
1.3.3.
1.3.4.
1.3.5. Connection
1.3.6. Connection
.
1.3.7. Connection
Server Explorer
1.3.8. Visual Studio Connection
1.3.9. Connection ADO.NET
1.4. ( DataAdapter)
1.4.1.
1.4.2. Command
1.4.3. DataAdapter.
1.4.4. DataAdapter
1.4.5. TableMappings DataAdapter

14
15
17
18
20
21
22
22
22
23
24
24
25
25
26
26
27
27
27
28
29
29
32
33
34
36
37
44

_4

1.4.6. DataAdapter.
48
1.4.7. DataAdapter
58
1.4.8. DataSet

61
1.4.9. , DataAdapter...65
2. DataSet....

67

2.1.
67
2.1.1. DataSet, XML
68
2.1.2. DataSet
69
2.1.3. DataSet
71
2.1.4.
72
2.1.5.
72
2. .6. DataRelation
73
2. .7. DataSet
74
2. .8. DataSet
75
2. .9. Visual Studio DataSet.
75
2.1.10. DataSet
81
2.1.11. DataSet
S3
2.1.12. DataSet

84
2.1.13. DataSet

87
2.1.14. DataSet
96
2.1.15. DataSet DataView.. 100
2.1.16. DataSet.
104
2.1.17. DataView
106
2.1.18. DataView DataView Manager
110
2.1.19. DataSet
111
2.2. Visual Studio .NET
118
2.2.1.
118
2.2.2. 122
2.2.3.
127
2.2.4. ,
128
2.2.5.
132
2.2.6.
133
2.2.7.
135
2.2.8.
136
2.3.
137
2.3.1.
137
2.3.2.
I4i
2.3.3.
144


2.3.4.
2.3.5.
3.

5
145
145
148

3.1.
3.1.1. DataCommand
3.1.2. DataCommand
3.1.3. DataCommand
3.1.4. DataCommand
3.1.5. ,
3.1.6.
DataCommand
3.1.7. Data Command,
3.2.
3.2.1. DataSet
3.2.2.
3.2.3.

148
149
150
151
152
158

4. Windows- ADO.NET

173

162
166
167
168
169
170

4.1. Windows-
173
4.2. Windows-
176
4.3. Windows-
DataGrid
177
4.3.1.
178
4.3.2. DataSet
179
4.3.3. DataGrid
186
4.3.4. DataGrid
187
4.3.5.
189
4.4. Windows- ... 191
4.4.1. TextBox
193
4.4.2.
196
4.5.
198
4.5.1. Windows-
199
4.5.2. DataConnection DataAdapter.... 200
4.5.3.
203
4.5.4.
204
4.5.5.
205
4.5.6.
207
4.5.7.
208
4.5.8.
209
4.5.9.
211

4.6. Windows-
4.6.1. Windows-
4.6.2. Data Connection DataAdapter
4.6.3. DataSet
4.6.4.
4.6.5.
4.6.6.
4.6.7.
4.7. DataGrid
4.7.1. Windows-
4.7.2. Data Connection DataAdapter
4.7.3.
4.7.4.
4.7.5.
4.8. , Windows-
4.8.1.
4.8.2.
4.8.3.

213
214
215
217
218
220
223
224
225
226
226
228
229
231
234
235
235
237

5. ASP- ADO .NET ....243


5.1.
243
5.1.1. Web-
245
5.1.2.
246
5.1.3.
252
5.1.4. Web- Web-
253
5.2. Web-
254
5.2.1.
255
5.2.2.
255
5.2.3. DataGrid
259
5.2.4. DataGrid.
261
5.2.5.
262
5.3. ,
262
5.3.1.
263
5.3.2.
264
5.3.3.
267
5.3.4. .... 269
5.3.5.
270
5.4. Web- ... 271
5.4.1.
272
5.4.2.
273
5.4.3.
278

5.4.4. 279
5.4.5.
285
5.5. Web-

286
5.5.1. Web-
286
5.5.2.
288
5.5.3.
,
290
5.5.4. DataGrid
291
5.5.5. Web-
299
5.5.6. -
304
5.5.7. -
306
6. ASP-

.....309

6.1. Web-
309
6.1.1.
310
6.1.2.
311
6.1.3.
312
6.1.4.
313
6.1.5.
314
6.1.6.
315
6.1.7.
316
6.1.8.
317
6.2. DataGrid

319
6.2.1.
320
6.2.2.
320
6.2.3. DataGrid
324
6.2.4. DataGrid
325
6.2.5.
327
6.2.6.
331
6.2.7. DataSet
..................
332
6.2.8.
338
6.2.9.
339
6.2.10.
342
6.3. DataGrid
344
6.3.1.
345
6.3.2.
346
6.3.3. DataGrid
...
348
6.3.4. .... 350
6.3.5.
357
6.4. , Web-
357
6.4.1.
358

6.4.2.
6.4.3.

359
361

7. Web-

370

7.1. Web-
7.2. Web-
7.2.1. XML Web Service
7.2.2. Web-
7.2.3. Web-
7.2.4. Web-
7.2.5. Web-
7.3. Web-
7.3.1. Web-
7.3.2. Web-
7.3.3. Web-
7.4. Web-

370
371
371
372
374
376
377
381
381
383
384
385

8. Web-
8.1.
8.2.
8.3. -
Web-
8.3.1. Web-
8.3.2.
8.3.3.
8.3.4. DataSet
8.3.5.
8.4. - Web-
8.5. Web- Windows-
8.5.1. Windows-
8.5.2.
8.5.3.
8.5.4.
8.6. Web- Web-
8.6.1. Web-
8.6.2.
8.6.3.
8.6.4.
9.

9.1. Windows-
9.1.1.

388
388
390

391
392
393
396
398
399
403
403
404
406
408
411
411
412
413
416
421
423

423
423

9.1.2.
Windows
430
9.1.3.
433
9.1.4.
439
9.1.5. MyWin
440
9.2. Web-
441
9.2.1.
442
9.2.2. Web- ....444
9.2.3. Web-
446
9.2.4. Web-
447
10. -

449

10.1.
449
10.2.
452
10.3. Web-
453
10.3.1.
455
10.3.2.
465
10.3.3.
471
10.3.4.
484
10.4.
495
10.4.1. Web- 496
10.4.2. Web-
506
10.4.3. Web-
513
10.4.4. Web-
524
10.4.5.
536

539
540


,
- ( ). , . "-32" "" .
, ,
.
""
.
MS SQL Server, VB6, , , Visual
Studio .NET. ,
postolit@gupmta.ru.


,
Visual Basic .
: , ,
, , . , ,
.
ASP (Active Server Pages, ). -

. , Microsoft .NET, . .NET- ,
, ADO (ActiveX Data Objects, ActiveX). ,
, .
.
, ADO, , Visual Studio .NET
, , ,
Microsoft .NET.
, , .
.
. ,
Visual Basic , ADO, -

12

SQL.
,

(Windows-, Web-, Web-) .
ADO.NET , .

ADO.
, ,
.
( ),

.
Windows ADO.NET. DataGrid , , , , .
ASP ADO.NET. Web-,
, ,
, ,
. , .
DataGrid (, ).
Web-, , , , Web- -.
Web-.
. Web-
-.
-, Windows-
Web- Web-.

13

Windows- Web-
IIS (Web-),
.
-, ( Web-).
: ,
, , ,
.
, . .

, ,
.
. . :
Visual Basic #.
,
. , .
. .NET Framework
Class Library.
, Visual Studio .NET ,
.

ADO.NET

ADO.NET ActiveX Data Objects (ADO,
ActiveX). Web-. ADO ,
ADO.NET "" . ADO.NET
OLE DB Windows-,
() Web-.
:
ADO.NET;
connection;
DataAdapter, ;
,
ADO .NET.

1.1.
ADO.NET
, ,
, (-

ADO.NET

15_

, , , - . .).
, ,
, ,
, , .
,
,
. Microsoft .NET
ADO.NET, , ( ).
ADO.NET .
,
,

, .
, ,
, , ,
ADO.NET.
, , XML,
, , .
ADO.NET, , ,
. .
,
.

,
Visual Studio, , ,
, Microsoft
(MDAC) 2.6 .

1.1.1.
- , "" , .
, . , 810 -

16

, , 100, 200 .
"" ,
,
.
(Web-) .
,
.
, ADO.NET
.
.
, . , ,
WEB-, ,
.
""
, ,
. ,
. , ADO,
, . 1.1.

.1.1. ADO.NET

ADO.NET

17_

ADO.NET .
. , (, MS SQL Server).

(, , , , . .).
-. , ,
. DataConnection. ,
,
DataAdapter. , ,
,
DataSet. DataSet, , ,
"" , -,
, - .
. , . DataSet,
(DataGrid,
TextBox, ComboBox, Label . .). Visual Studio .Net .
Windows- ( Windows-),
-, . ,
Web- ( Web-),
. . 1.1, DataSet.
-
XML,
(), ().

1.1.2.

ADO.NET ,
SQL- (DataCommand).
, ,
SELECT
.

18

, :
(connection) , ,
SQL- , .

.
, ,
. SQL- SELECT, . ,
DataReader, ,
read-only, forward-only.
, , ,
. , .
. , , , . , ,
.
, . . , , , :
SELECT * FROM customers WHERE (custcmer_id=@custoinerid)

,
.

1.1.3.
DataSet
,
:
,
. , ,
: , , . .
,
: ; ,
, . . . ,
. -

ADO. NET

19_

.
"" , .
DataSet,
, .
Recordset, DataSet , .
, . DataSet . 1.2.

. 1,2. DataSet

DataSet . , "" ,
.
,
, .
, ( ,
). DataSet,
.
, DataSet ,
.
,
DataAdapter. , , DataSet,
DataSet
. .
SELECT, INSERT, UPDATE, DELETE

(. 1.3).

20

. 1.3. DataAdapter

, Fill DataAdapter, DataSet, SelectCommand


:
SELECT au_id, au_lname, au_fname FROM authors

DataSet "" ,
.
,
. , , ""
DataSet, Fill .

1.1.4. DataSet

, DataSet () ,
.
DataSet , , - DataAdapter,
SQL- .
DataSet
DataAdapter,
,
DataSet , () , . , DataSet , ,
, , , .
DataSet,
,
, -

ADO.NET

21

.
DataSet . 1.4.

. 1.4.
DataSet

1.1.5. XML
DataSet. ADO.NET
XML-. ,
, XML-.
, XML
, ,
DataSet. ADO.NET XML
. ADO.NET XML-
.
, XML
:
XML ,
,
XML;
XML . XML
,
, , HTTP.
, , XML. ADO.NET XML XML- , . -

22

(
).

1.1.6. ,
(, , ) XML-,
,
XML-. ,
,
.
DataSet XML. , , ,
XML-, XML Schema
Definition Language (XSD). ,
DataSet XML-, DataSet
XML-.

XML-. Visual Studio .NET XML- , ,
, . , DataSet,
, Visual Studio .NET XML-,
DataSet.

, ,
. , , .
.

1.2. ADO.NET ADO


ADO.NET,
ADO.

1.2.1.
ADO
Recordset. ADO.NET DataSet.

ADO.NET

23

.
.
Recordset .
Recordset ,
SQL-, , Recordset.
, Recordset .
, DataSet ( ) .
DataSet DataTabie.
DataSet , DataTabie. To
, DataTabie
. , DataSet .
DataSet . DataSet , ,
. , DataSet
, .
,
( , ). ,
, .
DataSet , ,
, ,
Recordset.

1,2.2.
ADO ,
Recordset (,
MoveNext) . ADO.NET , , ,

24

() ().
() , ,
() , DataRelation ,

.
, , ( ) , ,
. ADO.NET , ,
, , ADO. ,

(forward-only, read-only) DataReader.

1.2.3.
ADO.NET
, ,
, , , SELECT UPDATE.
DataSet, "" . ADO Recordset

, ADO .

ADO ADO.NET. ADO
OLE DB . ADO.NET
(OleDbDataAdapter
SqiDataAdapter). DataSet . ,
,
.

1.2.4.
DataSet ,
Recordset.
Recordset , COM marshalling. ADO.NET, DataSet, XML-.

ADO.NET

25

XML- COM marshalling, .

1.3.
( Connection)
,
(connection). ADO.NET :
SqlConnection ,
MS SQL Server 7.0 ;
O OleDbConnection ,
OLE DB.
SqlConnection
MS SQL Server 7.0 . OleDbConnection OLE DB
, , (
SQL Server).

1.3.1.
,
ConnectionString. ,
= , (DataSource),
(Database), (UserId),
(Password), . .
, , :
MyConnection="Provider=SQLOLEDB.l;DataSource=MySQLServer; InitialCatalog=NORTHWIND; Integrated Security=SSPI"

ConnectionString ,


(SqlConnection

OleDbConnection). ,
C o n n e c t o n S t r i n g udl- (Microsoft
Data Link file, Microsoft).

SQLConnection Provider, .

26

1.3.2.
connection
(open close). open ConnectionString, () . close .
.
,
( ). , .
DataAdapter DataCommand, .
(, Fill update), . , DataAdapter , .
, Fill,
, .
, .
. , DataAdapter
Connection.
,
DataAdapter, .

1.3.3.
Web- .
.

,
.
, , , , .

OleDbConnection,
, -

ADO.NET

27_

. SqlConnection,
,
.

1.3.4.

connection

BeginTransaction,

OleDbTransaction

SqlTransaction. Transaction , (Commit)


"" (RollBack).

1.3.5. Connection
connection
. , , ,
connection ,
, . . , ConnectionString
.
,
,
. . .NET Framework.
ConnectonString ( , ,
, )
.

1.3.6. Connection
, .

.
Web-, ,
Internet Information Services (IIS), "" . ,

28


, ""
. "" ( ,
), ,
Web-.
Web-
ASP.NET, ( HTTP, FTP, ). Web ,
Windows.
Windows, IIS SQL-.
, ,
.
,
, .
,
.

1.3.7. Connection
Server Explorer
Server Explorer
.
() ; ,
,
.
,
. ,
, ,
Connection,
. connection, Server Explorer, Server Explorer
connection, .

ADO.NET

29

, Server Explorer ( Connection) SQL- MyServer


Northwind, SQL Server. ,
,
Northwind, ,
. ""

Connection DataAdapter.
ConnectionString - Server Explorer.
, connection, (
Server Explorer)
.

. , Visual Studio
- , connection
Server Explorer.

1.3.8. Visual Studio


Connection
Visual Studio ""
connection. , ,

connection
, .
connection
.

1.3.9. Connection ADO.NET


Connection :

Visual Studio:

DataAdapter.
connection,

DataAdapter;

30

connection
, .
O ,
Server Explorer .

Connection DataAdapter.
O connection.
connection ,
. ,

Properties.
O connection .
Connection .
1. Windows- Web- (.
2 3).
2. Data Toolbox
Connection .
OleDbConnection SqlConnection (. 1.5),
.

. 1.5. Connection Data Toolbox

SqlConnection,
SQL Server 7.0 .
Connection SqlConnection, N
(, SqlConnectionl).

OleDbConnection, ,
.

ADO.NET

31

Connection OleDbConnectionN, N
(, OleDbConnection) .

3. Connection , Properties
ConnectionString.
DataSource, Database,
UserName ( provider
SqlConnection).

Connection
.

Connection ,
connection . Properties DynamicProperties
ConnectionString ( 1.6).

. 1.6. Connection Properties

Dynamic Propertys
Map property to a key in configuration file OK (. 1.7).

. 1.7. Connection
Dynamic Properly


, .

32

1.4. ( DataAdapter)
DataAdapter ADO.NET.
DataSet. DataAdapter DataSet, , , . DataAdapter
, DataSet
.
, DataAdapter ,
, DataSet .
SQL- .
Visual Studio :
O OleDbDataAdapter,
, OLE DB-;
O SqlDataAdapter, ,
SQL Server 7.0 .
SqlDataAdapter
,
OleDbDataAdapter.


Framework, . 1.8.
OleDb Managed Provider
Namespace

SqlClient Managed Provider


Namespace

. 1.8. OleDB- SqlClient-

.NET

ADO.NET

33_

DataAdapter
( ) DataTabie
DataSet. DataSet ( DataTabie),
(. 1.9).

. 1.9.
DataSet

DataSet, (Fill) DataAdapter, SQL- . DataAdapter


(OleDbDataReader SqlDataReader),

DataSet. , , (update)
DataAdapter, SQL (. 1.9). , ,
.

1.4.1.
, DataSet
, DataAdapter SQL-
(), . -

34

, , DataAdapter
DataSet. DataSet
DataRelation.
(, ), .
, ,
Northwind: Customers orders ("" "")
DataGrid.
, , join SQL-,
Recordset DataGrid. DataAdapter , Customers
("") DataSet , Orders (""). DataSet DataRelation ,
orders Customers
CustomerId, DataSet DataGrid. ,
DataSet ,
, ,
DataRelation,
.
Recordset, .
DataSet Recordset.
DataAdapter , . .
DataAdapter OleDbConnection SqlConnection, -

. DataAdapter ,
, : SELECT, UPDATE, INSERT DELETE.
OleDbConnection
OLE DB-. SqlConnection SQL Sewer 7.0 . , Connection .

1.4.2. Command
DataAdapter, , ,
. ,

ADO.NET

35_

, DataAdapter
:
O SelectCommand ,
;
O InsertCommand ,
;
O UpdateCommand ,
;
O DeleteCommand ,
.
SQL- .
OleDbCommand SqlCommand.
CommandText, SQL-
.

Command
Connection. , SqlConnection,
SQL-, , SqlCommand.


OleDbCommand SqlCommand, , Visual Studio
SQL DataAdapter. , DataAdapter
SQL- , UpdateCommand, InsertCommand DeleteCommand
. ,
,
SelectCommand, , .
Visual Studio
DataAdapter. ,
( ) .
SQL- , ,
.

36

DataAdapter . ,
(SelectCommand), WHERE,
, ,
, .
(, UpdateCommand)
, .

1.4.3.
DataAdapter
, DataAdapter
DataSet .
DataAdapter ,
.


( ), DataSet.

read only DataReader, .
DataAdapter .
O DataSet;
Fin SqlDataAdapter
OleDbDataAdapter. ,
SQL-
SELECT;
O ,
DataSet, .
Update SqlDataAdapter
OleDbDataAdapter. ,
SQL- INSERT,
UPDATE DELETE, , , .
( read-only),
DataReader DataSet. DataReader -

ADO. NET

37

()
. , DataReader
read-only, forward-only, . , Web-, , . Web-
,
DataSet. , DataAdapter DataReader DataSet. Visual Studio
OleDbDataReader SqlDataReader,
OLE DB- SQL
Server 7.0 .
Visual Studio
DataSet ( ) () .
, ,

.

. Visual Studio
, DataSet, ,
, . DataAdapter
TableMappings, ( ) DataSet
.

1.4.4. DataAdapter
DataAdapter CommandText
SelectCommand, InsertCommand, UpdateCommand DeleteCommand.
, .
, ,
, . .
.
O . .
, SQL- , WHERE . , , WHERE, -

38

, .
,
.
O . , () , .

.


, DataSet, WHERE SQL-
,
. ,
,
Web-. CommandText
SelectCommand SQL-.
: (?)
( @).
OleDbCommand ;
SqlCommand .
OleDbCommand :
SELECT BookId, Title, Author, Price FROM BOOKS WHERE (Title LIKE ?)

SqlCommand:
SELECT BookId, Title, Author, Price FROM BOOKS WHERE (Title LIKE @title)

( title), , ,
:
OleDbDataAdapterl.SelectCommand.Parameters("title").Value=txtTitle.Text
.
Visual Studio SQL- ,
Query Builder ( ).


SelectCommand , UpdateCommand,InsertCommand DeleteCommand
.

ADO,NET

39

UpdateCommand InsertCommand
, . , UpdateCommand DeleteCommand WHERE, , .
, . ,
,
ShoppingCart. ShoppingCart
, , : (BookId)
(CustId), , ShoppingCart.
, ShoppingCart.
SQL- INSERT.
SQL- :
INSERT INTO ShoppingCart

(BookId, CustId, Quantity) Values (?, ?, ?}

,
,

BookId,
CustId,
Quantity

ShoppingCart. ,
:
INSERT INTO ShoppingCart
@custid, @quantity)

(BookId, CustId, Quantity) Values (@bookid,

- ,
, , SQL- ,
UPDATE. :
UPDATE ShoppingCart

SET (BookId=?, CustId=?, Quantity=?)


WHERE (BookId=? AND CustId=?)

, :
UPDATE ShoppingCart
SET(BookId=@bookid, CustId=@custid, Quantity=@quantity)
WHERE

(BookId=@bookid AND CustId=@custid)

SQL- SET

40

ShoppingCart. WHERE , .
.
SQL- DELETE :
DELETE FROM ShoppingCart WHERE

(BookId=? AND CustId=?)

:
DELETE FROM ShoppingCart WHERE (BookId=@bookid AND CustId =@custid)


,
(SelectCommand, InsertCommand, UpdateCommand DeleteCommand)
DataAdapter Parameters, , SQL-.
OleDbDataAdapter ,
OleDbParameter OleDbParameterCollection.
SqlDataAdapter SqlParameter SqlParameterCollection.
(Data Adapter
Configuration Wizard), DataAdapter,
(SelectCommand, InsertCommand, UpdateCommand DeleteCommand).
Server Explorer ,
Visual Studio :
O , Visual Studio SelectCommand
(SQL- SELECT) UpdateCommand, InsertCommand DeleteCommand.
, SQL- SelectCommand ,
.
O ,
Visual Studio SelectCommand ,
.
(UpdateCommand, InsertCommand DeleteCommand),
, .

DataAdapter Data Adapter Configuration
Wizard. , , Properties.

ADO.NET

41_

Parameters
Command (SelectCommand,
UpdateCommand, InsertCommand DeleteCommand)

SQL-, CommandText. command


SQL- ,
SQL-. , UPDATE :
UPDATE

ShoppingCart

SET (BookId=?, CustId=?, Quantity=?)


WHERE (BookId=? AND CustId=?)

:
UPDATE ShoppingCart
SET (BookId=@bookid, CustId=@custid, Quantity=@quantity)
WHERE (BookId=@bookid AND CustId=@custid)

command ,
.
.
.
ParameterName,
.
,
, . .
, . ,
SQL-, ,
,
.
( ), ,
. ,
( )
.
() . (ParameterName),
. 1.1 SQL-
Visual Basic #. , Title_Keyword.

1
1.1
' Visual Basic
titleKeyword="%" & txtTitleKeyword.Text & "%"
OleDbDataAdapter1.SelectCommand.Parameters(1).Value=titleKeyword
OleDbDataAdapter1.SelectCommand.Parameters("TitleKeyword").Value =
titleKeyword

// C#
string titleKeyword="%"+txtTitleKeyword.Text+"%";
this.OleDbDataAdapter1.SelectCommand.Parameters[1],Value=titleKeyword;
this.OleDbDataAdapter1.SelectCommand.Parameters["TitleKeyword"].Value=
titleKeyword;

,
1, . , .
, , ,
.
, Properties Parameters . , . 1.10
InsertCommand customers Northwind.

. 1.10.
InsertCommand

(Collection) Parameters,

ADO.NET

43

(. 1.11). .
, .

. 1.11. InsertCommand


, :
O value;
O () DataSet.
, ( ). ,
, , , ( ).
, Text
Fill DataAdapter.
1.2.

44

| 1.2
' Visual Basic

titleKeyword="%" a txtTitleKeyword.Text & "%"


OleDbDataAdapter1.SelectCommand.Parameters("TitleKeyword").Value

titleKeyword
OleDbDataAdapter1.Fill(dsAuthors1)

// C#

titleKeyword="%"+txtTitleKeyword.Text+"|";
this.OleDbDataAdapter1.SelectCommand.Parameters["TitleKeyword"].Value=^>

titleKeyword;
this.OleDbDataAdapter1.Fill(dsAuthors1);

. update DataAdapter, DataSet, (update, insert, delete) , . .



DataSet. , DataSet
InsertCommand ,
INSERT ,
DataSet.
, . .
"" DataSet.
. DataAdapter
RowUpdating, ,
. . , .

1.4.5. TableMappings DataAdapter


DataAdapter ,
, ,

ADO. NET

45

DataSet. (table mapping).



DataSet. ,
au_id author_id_number
DataSet.
, Visual Studio DataSet,
( ) , .
DataSet .
O ,
.
O ,
.
,
TableMappings ,

DataTableMapping.

DataTableMapping ,
DataAdapter .
Properties DataAdapter (. 1.12).

. 1,12. TableMappings

(Collection) TableMappings ,
DataTableMapping (. 1.13). , -

46

ColumnMappings, ,
.

. 1.13. Table Mappings

Fin DataAdapter,
, DataSet .
1.
TableMappings.

2.

,
.

3.

, DataAdapter
DataSet.

, .
O .
, (, -

ADO.NET

,
DataAdapter).

O , ,
.

, . . ,
, , .
MissingMappingAction ,
, . :
O Passthrough
.
,
MissingSchemaAction (. );
O Ignore , ,
;
O Error - .
MissingSchemaAction ,
, . :
O Add ;
O AddWithKey
, ;
O Ignore , ;
O Error .
,
. MissingMappingAction
Passthrough MissingSchemaAction Add
DataSet.
, , -
, .

__

48_

, , TableMappings. ,
SQL-, ,
DataSet.

1.4.6. DataAdapter
DataAdapter :
O Server Explorer;
O ;
O .
.

DataAdapter
Server Explorer
DataAdapter Server Explorer Windows Web- (. 2
3), .
1. , Server Explorer.
2. Connection , .
3. Connection
(Tables, Stored Procedures, Views Functions). , ,
, ,
. '

UpdateCommand,
InsertCommand

DeleteCommand DataAdapter.

4.
(. 1.14).

, SELECT * FROM _.

ADO.NET

49

, , .
, , ,
.

. 1.14. Customers Server Explorer

Visual Studio connection


DataAdapter (. 1.15).

. 1.15. SqlConnectionl SqlDataAdapter1,


Server Explorer

50


, DataAdapter
. ,
.
Server Explorer DataAdapter,
. Data Adapter
Configuration Wizard.

DataAdapter
Data Adapter Configuration Wizard
.
Data Adapter Windows
Web- (. 2 3) .
1. .
2. Data Toolbox OleDbDataAdapter
SqlDataAdapter (. 1.16).

. 1.16. DataAdapter Toolbox

Visual Studio DataAdapter


Data Adapter Configuration Wizard.
3. :
(. 1.17)
Connection.

ADO.NET

51

. 1.17. Connection
DataAdapter

(. 1.18) , SQL- (
. 1.1).

. 1.18. DataAdapter

52

1
1.1.
DataAdapter

Use SQL statement

SQL- SELECT,
DataAdapter


DataSet,
UPDATE, INSERT DELETE

Use newly created stored


procedures

SELECT,
.
,

Use existing stored procedures

, DataAdapter

, ( Use SQL
statement).

(. 1.19) SELECT.

. 1.19.
DataAdapter

ADO.NET

53

SQL-,
, Query Builder. (. 1.20) .

. 1.20. Add Table


DataAdapter

, , , .
(. 1.21).
,
(. 1.22).
Next,
(. 1.23), , . , - ,

54

. 1.21.

. 1.22. SQL
DataAdapter

ADO.NET

55

, 1.23. ,
DataAdapter

.
.
DataAdapter connection,
.
DataAdapter connection,
Properties.
, DataAdapter
DataSet,
. Data Preview Data Data Adapter Preview Fill
DataSet (. 1.24).
DataAdapter
. . .

56

. 1.24. , DataAdapter

DataAdapter
DataAdapter , . ,
DataAdapter, .
DataAdapter
.
1. , connection .
2. Data Toolbox OleDbDataAdapter
SqlDataAdapter . Visual Studio DataAdapter Data
Adapter Configuration Wizard. .
3.

Data Adapter Configuration Wizard.

ADO.NET

57

DataAdapter
.
1. DataAdapter Properties,
,
.
SelectCommand (. 1.25). DataAdapter

,
UpdateCommand, DeleteCommand, InsertCommand.

. 1.25. SelectCommand
Properties

Command
(. 1.2).
1.2, Command

ActiveConnection Connection. ,

Connection

ActiveConnection Properties. ,
Command Connection.

Connection Command
CommandText

SQL- .

, (;). . ,

58_

1
1.2. ()

CommandType

CommandText. :

O Text SQL-
O StoredProcedure
TableDirect -
Parameters

Parameter , . 8 SelectCommand , .
UpdateCommand, InsertCommand DeleteCommand

2. MissingMappingAction.
Passthrough, .
.
3.

Data Generate DataSet.

Data, ,
,
.

4. , DataAdapter
DataSet, .

1.4.7.
DataAdapter
(SelectCommand,
InsertCommand, UpdateCommand DeleteCommand) SQL . DataAdapter Data Adapter Configuration Wizard,
. , .

ADO.NET

59

SQL- Command Regenerate parameters


collection for this command Query Builder.

DataAdapter .
1. DataAdapter.

2, DataAdapter
Properties (. 1.26).

. 1.26. UpdateCommand
SqlDataAdapter

3. Command,
(, UpdateCommand) Parameters
, SqlParameter Collection Editor
(. 1.27).

. 1,27. UpdateCommand

60

4. Add.
5. ,
.
6. ,
Members . . 1.3. , .
1.3. DataCommand

SourceColumn

,
. ,
UPDATE, INSERT
DELETE ( )

SourceVersion

SourceColumn,
SourceVersion ,
, ;
O Original ,
. , , . ,
,
,
O Current ,
. ,

O Default , ,

Value

, . ( ),

Value
SourceColumn,

NamedParameter

DBType,
sion,
Size

. , ,
(@parametername)
(?). SqlConnection ,
NamedParameter true

Preci- ( ),
Scale, ,
,

ADO.NET

61
1.3. ()

Direction

: :

Input .

Output (
SQL-)

ParameterName

1.
8.

InputOutput ,

ReturnValue . ( 0)

, .
,

SqlParameter Collection Editor.


3- 7-
Command.

1.4.8.
DataSet

Visual Studio DataAdapter ( )
DataSet. , .
.
1. DataAdapter.
2.

DataAdapter
Properties .

62_

3.

TableMappings
(. 1.28).

(Collection)

. 1.28.
TableMappings

TableMappings (. 1.29).

. 1.29. TableMappings

4.


DataSet,

ADO.NET

63_

Use a dataset to suggest table and column names, Dataset .


Sourse table, Dataset table. Column mappings.
5.

Source table, .
, Table.

6.

Dataset table .
4, (
Table).

7.

( , ), Source Columns
Dataset Columns. ,
:
Column mappings .

8. , .
.

DataSet
"" .

. ,
,
.

. , ,
, ,
.

DataSet (. 1.30).
9.

, .

10. , , Properties
MissingMappingAction MissingSchemaAction. . 1.4 -

64

. 1.30. TableMappings
1.4. MissingMappingAction
MissingSchemaAction
MissingMappingAction

MissingSchemaAction

Passthrough

Add or AddWithKey

Ignore

Ignore


, ,

DataSet.
, , ,

, ,
DataSet.
,
,

DataSet

ADO.NET

65
1.4. ()

MissingMappingAction

MissingSchemaAction

Error

Error


,

. ,

1.4.9. ,
DataAdapter
, DataAdapter, ,
DataSet.

DataAdapter
.
1. DataAdapter.

2. DataAdapter
Properties.
3. Preview Data
(. 1.31).

. 1.31. DataAdapter

.
4. Data Adapter
All DataAdapters.
5.

, SelectCommand, , Parameters.

3 . 873

66

6.

Fill Dataset
(. 1.32).

. 1.32. DataAdapter

SelectCommand
Results.
( ), . , . , . 1.32 ,
, ,
, . ,
TableMappings.

7.
. , . Clear Results.
ADO.NET
DataSet.

DataSet
ADO.NET ADO. ADO.NET
.
:
O DataSet;
O Visual Studio .NET ;
O , ,
;
: " DataSet?"

2.1.
DataSet -- ()
,
.
: , .

DataSet .
, Web, .

,
( ) .NET Framework (. 2.1).

68

. 2.1. .NET Framework

DataSet , .
O DataSet DataTabie DataRelation.
O DataTabie , C h i d R e l a t i o n s
ParentRelations.
O DataRow Rowstate, , . RowState: Deleted, Modified, New Unchanged.

2.1.1. DataSet, XML


ADO.NET DataSet ( ),
XML. Visual Studio .NET
Framework XML
. XML. .
O DataSet , ,
XML-. XML- W3C (World Wide Web Consortium) () . DataSet , ,

DataSet

69

ReadXmlSchema W r i t e X m l S c h e m a . ,
( InferXmlSchema) XML-.
O DataSet, , ( )
.
O XML- DataSet,
ReadXML, DataSet XML-,
WriteXML. XML
, DataSet XML-, . DataSet
XML-,
.
XML- ( XMLDataDocument)
, , DataSet XML.

2.1.2.
DataSet
DataSet . DataSet , DataSet XML- (xsd) . (, )
DataSet , .
DataSet DataSet, .
DataSet, , . , DataSet,
, .
, DataSet,
, WriteXmlSchema.

.
Visual Studio ,
, DataSet
.

70


DataSet
DataSet ,
. , , ,
( 2.1) CustomerId
customers.

' Visual Basic


Dim s As String
s=dsCustomersOrders1.Customers(0).CustomerID
// C#

string s;

s=dsCustomersOrders1.Customers[0].CustomerID;
( 2.2)
:

' Visual Basic


Dim s As String

s =
CType(dsCustomersOrders1.Tables("Customers") .Rows(0) .Item("CustomerId") ,
String)
// C#

string s=(string) dsCustomersOrders1 .Tables ["Customers"] .Rows[0]["CustomerID"];


, IntelliSense Visual
Studio. ,
, . , .

DataSet

71_

, , , (,
). ,
, . , , , .
,

.
, , .
DataSet , , .

2.1.3. DataSet
, ( ) . ,
customers customers,
.
, SQL-, .

Dataset, XML- ,
. ,
"Customers" "customers". ,
DataSet.

, (
)
. ,
DataSet,
, .

, ,
CaseSensitive . .

72

2.1.4.
DataSet , . DataSet,
. ,
,
.
.
O Fill . SQL DataSet. , .
.
O ,
DataRow Rows .
,
ROWS .
O .
, (, XML Web-services), .

2.1.5.

DataSet -- ,
( ADO Recordset). . , .
, ,

, .
, ADO, .
DataSet
( ).

, , .

DataSet

73

DataSet (,
DataGrid), () .

2.1.6. DataRelation
, . DataSet ,
DataRelation,
. DataRelation ,
.
, Northwind, SQL
Server, Customers ()
Orders (). Customers ,
. 2.1.
2.1. Customers

CustomerID

CompanyName

City

ALFKI

Alfreds Futterkiste

Berlin

ANTON

Antonio Moreno Taquerias

Mexico D.F.

AROUT

Around the Horn

London

orders . Orders CustomerID


( ) . orders , , , . 2.2.
2.2. Orders
OrderId

CustomerID

OrderDate

10692

ALFKI

10/03/1997

10702

ALFKI

10/13/1997

10365

ANTON

11/27/1996

10507

ANTON

4/15/1997

74

, " ". , , ALFKI . , , DataSet


ds,
( 2.3).

SqlDataAdapter1.Fill(ds, "Customers")
SqlDataAdapter2.Fill (ds, "Orders")
ds.Relations.Add("", ds.Tables("customers").Columns("CustomerID"),
ds.Tables("Orders").Columns("CustomerID"))
DataRelation,
. ,
ANTON, , . ,
10507, DataRelation,
(ANTON).

2.1.7. DataSet
, DataSet , .
, , , . .
O . , ,
.
. , ,
, .
UniqueConstraint
ForeignKeyConstraint
.
unique true.
EnforceConstraints, , Dataset
. true. . ,
,

DataSet

75

( ).
.
Visual Studio , . , , . , Unique
true. , D a t a R e l a t i o n ,
.

2.1.8. DataSet
, . , DataSet , update ,
.
DataRow ,
RowState, ,
, . Deleted, Modified, New
Unchanged. Update RowState, , ,
(add, edit, delete) ,

2.1.9. Visual Studio


DataSet
Visual Studio 2 .
O (Component Designer)
Toolbox Server Explorer.
Visual Studio , , ,
. , Component Designer
. .
Q

XML- (XML Designer),


.

( ). .

76

, Visual Studio,
, , Microsoft Data Access
Components (MDAC) 2.6 .


DataSet
,
.
1. XML- (xsd-). :
.
Component Designer
, DataAdapter xsd-;

XML- (XML
Designer);

XML Web-service ,
. , Visual Studio .
2.

DataSet. Visual Studio DataSet


System. Data. , ,
, .
Visual Studio
, .

3.

, 2,
.

,
, 3.

Component Designer
Component Designer , Visual Studio . Component Designer

DataSet

77_

.

FillSchema.
XML- (xsd-) ,
.
, , Component Designer
. ,
. , SQL- , DataSet .
TableMappings , , .
( ) DataSet. -
, DataSet
, ,
.
Component Designer .
Q
.
, Visual Studio .
, XML Designer.
Q ( ) .
, SQL-, ,
.
Q , Component Designer, ,
DataRelation.
XML Designer.

XML Designer
XML Designer ,
,
. XML- .

78

Q (, )
Server Explorer . ,
,
.
Q

. ,
.


DataSet, , .
XML Designer ,
. ,
,
( DataRelation), Component
Designer,
XML- xsd-. , xsd-.
xsd- , , . , Visual Studio xsd-
.
,
.
XML Designer:
XML- Visual Studio;
Q xsd
,
,
,

.
XML-,

DataSet.

DataSet
Component Designer
, Component Designer ,
.
DataSet , .

DataSet

79

Component Designer . , .
, DataSet.
, Properties .
DataSet Component Designer
.

i. (DataAdapter)
.
2.

Component Designer ,
DataSet (. 2.2).

. 2.2. Component Designer

,
() . , , ,
, DataSet.

,
.

3.

Data Generate Dataset.


Generate DataSet (. 2.3).

SO

. 2.. Generate DataSet

4. New
.
, Add an instance of this DataSet to
the designer.
Visual Studio, ,
XML- (xsd), . , DataSet. , Visual Studio , XML .

Solution Explorer, Show All Files.

, DataSet
, , . ,
dsAuthors,
dsAuthorsl.

DataSet

81

" " DataSet1.


,
DataSetll (- , ).

5.

( ). .
Fill .
6. DataSet
() ,
.

2.1,10.
DataSet
,
, ,
. ,
"" ( )
, . . (expressions).
,
, .
, (
SQL-), count.
, "", (UnitPrice) (Quantity)
:
UnitPrice * Quantity

, Child, . ,
(Prise)
:
Avg(Child.Price)

DataSet
XML Designer.

82

.
1.

XML-,
XML- (xsd-) Solution Explorer. XML Designer
(. 2.4).
2. (grid), , ,
, :

Element ;
;
.
. 2.5 , .

. 2.4. XML Designer

. 2.5.
XML Designer

3. ,
Properties Expression.
(. 2.6).

. 2.6. Properties

DataSet

83_

2.1.11.
DataSet
, .
, . ,
(, DataRelation).
DataSet .

Component Designer. ,

.

Q XML Designer ( ). , XML-


.
.
DataSet
Component Designer .
1.

, ,
,
.

2.

, Data
Generate Dataset.

3.

Generate Dataset.

4.

Choose a dataset Existing


,
.

5.

Choose which table(s) to add to the dataset,


,
.
(. 2.7).

6.

, Add this dataset to the


designer.

7.

OK DataSet.

84

. 2.7. Generate Dataset


.
DataSet ( ,
). . 2.8.

. 2.8. DataSet XML Designer

2.1.12.
DataSet

, .

DataSet

, .
. :
G ;
Q XML Designer;
Q XML Web-service ,
.
, ,
.
DataSet
.
1. .
2. Data Toolbox DataSet .
Add Dataset (. 2.9).

. 2.9. Add Dataset

3.

Typed Dataset
,
.

.

86

XML Web-service ,
Solution Explorer, reference.
, , XML Web-service
.


.
4. ,
.
.
( 2.4) , Customers () dsCustomersOrders ( dsCustomersOrders).
2.4
' Visual Basic
OleDbDataAdapter1.Fill(dsCustomersOrders1,

"Customers")

// C#

OleDbDataAdapter!.Fill(dsCustomersOrders1, "Customers");

DataSet , XML
Web-service , , ,
. , XML Web-service . 2.5 ,
XML Web-service,
GetCustomersDataset, dsCustomers1.
2.5
' Visual Basic
Dim ws1 As New MyServer.CService1()
dsCustomers1.Merge(ws1.GetCustomersDataset())
// C#

MyServer.CService1 ws1=new MyServer.CService1()


dsCustomers1.Merge(ws1.GetCustomersDataset());

DataSet

5.

87_

DataSet ,
.
,
Update.

2.1.13. DataSet

DataSet,
.
, ,
XML- xsd-.
, , . , ,
, DataRelation
Properties,
, DataSet.

,
,
( WriteXmlSchema).


Component Designer
.
.
1.

Data Toolbox DataSet


, .
Add Dataset (. 2.10).

2.

Untyped Dataset .
.

3.

, ,
Properties :
Name ,
;

88

DataSetName
XML-, . , .

. 2.10. Add DataSet


Properties,
. Tables, DataTable,
.
Columns, DataColumn.
Collection Editor, Properties.


,
. ROWS
Properties.

.
1.


Properties (. 2.11).

DataSet

89

. 2.11. DataSet

Tables ,
Tables Collection Editor (. 2.12).

, 2.12. Tables Collection Editor

3. Add, . ,
Name.

90

. ,
, .

4.

.
, , , Members, Properties
Columns, Columns Collection Editor
(. 2.13);

. 2.13. Columns Collection Editor

Add, , .
(, , . .). ,
, . 2.3.
2.3.

AllowDBNull

, ,
(Null)

DataSet

91

2.3. ()

ColumnName

Tables, ,

DataType

.NET Framework, .
XML ,
DataSet
(, System. String
xs:string ).

Readonly

, ,

Unique

Name

,
. true
,

,

,
(. 2.14);

. 2.14. , Columns Collection Editor

92

, , Columns Collection
Editor.
5. , PrimaryKey.
.

PrimaryKey .
unique true , .
6. 3-5 ,
.

DataSet
, XML Designer ,
(
WriteXmlSchema). Component Designer.

.
1.
Properties.
2.

Tables,
Tables Collection Editor.

3. Columns,
Columns Collection Editor.
4.

Add .

5.

Properties, ColumnName , .

6.

DataSet

93

7. Expression.
. 2.15 ,
: , ( ).

. 2.15. ,
Columns Collection Editor


.
Q . ,
.
Q .
, , .
.
1.
Properties.
2.

Tables
Tables Collection Editor.

94

3.

, , Constraints
Constraints Collection Editor (. 2.16).

. 2.16. Constraints Collection Editor

4.

Add Unique Constraint Foreign


Key Constraint .

Unique Constraint Foreign Key


Constraint. .
<Fl>.



( DataRelation) .
DataRelation
.
1.
Properties.
2.

Relations Relations Collection Editor (. 2.17).

DataSet

95

. 2.17. Relations Collection Editor

3. Add .
Relation
(. 2.18).

. 2.18. Relation

96

4. Relation ,
. 2.4.
2.4.

Name

,
DataRelation . (,
CustomersOrders)

Parent table

Child table

Key Columns

,
.

Foreign Key Columns

Enforce constraints

,
,

DataRelation

,

Update rule
Delete rule

() () ,

Accept/Reject rule

5. .
6. 3 5 , .

2.1.14.
DataSet
, DataSet ,
"". , -

DataSet

97

( ,
, . .)
(, ).
,
, , , .
() (). .
Q Select,
. ;
,
, .
Q ( DataView). , , .. .
DataSet
(view), . , DataSet.
. , select . :
Q
, ;
Q
;
,
. ,
orders (), , , . .

DataView,
,
, -

98

(
).
,
( )
. ,
,

DefaultView,

DataView.
( ).
, ,
, .
:
Q DataSet ;
Q ( , , );
Q
.


Select RowFilter
, , , .

DataSet.
, . :
true, .
, , :
Price > 10.00
.

RowState Version
.
( ),
() .
,
: (
). , :

DataSet

99

, , , . , , ,
. .

ROWS, , .
, .
select ,
, , , , . , RowStateFilter, ,
.

,
, .

,
. ,
" " (OrderDate),
OrderDate. , . Select, .
,
sort.


Data View Manager

. ,

DataViewManager.
DataViewManager DataSet , , .
DataViewManager ,
. ,
. ,

100

(Customers), (orders)
DataRelation, . Customers (CompanyName) Orders
(OrderDate) . GetChildRecords
D a t a R e l a t i o n , , .
DataViewManager, .
DataViewManager . ,

. ,
DataViewManager

.

2.1.15.
DataSet DataView
DataView
.

DataView .
1.

DataSet.

2.

3.

Data Toolbox, D a t a V i e w
(. 2.19).

. 2.19. DataView Toolbox

101

DataSet

4.

5.

DataView1.

DataView
, Properties, . 2.5 .

, ,
.
2.5. DataView

Name

, DataView

Table

( DataTable),
. . , (
)

Sort

, ,
( ). ( ) S
DESC ( ). ,

RowFilter

, , . ; true, .
, , : Price > 10.00

RowStateFilter

, . CurrentRows (
, )

AllowEdit, AllowNew,
AllowDelete


(, , )

102

DataView

DataView

.
DataView,
.
. DataView (DefaultView),
, .
DataView
.
1.
, DataView .
2.

Sort, . ,
. ( ),
.
3. RowFilter, () . true false, :
CustomerStatus='Active'

(), , , "Margie's Travel",


(\). , , \, : CompanyName='Margie\'s Travel'

4. , ,
RowStateFilter CurrentRows DataViewRowState ( 2.6).
2.6
' Visual Basic
Dataview1.RowStateFilter=DataViewRowState.CurrentRows

DataSet

103

// C#
Dataview1.RowStateFilter=DataViewRowState.CurrentRows;
( 2.7) ,
,
DefaultView.
2.7
' Visual Basic
Dataset1.Customers.DefaultView.Sort="City"
// C#
Dataset1.Customers.DefaultView.Sort="City";

( 2.8) ,
DataView .
, ,
.
. DataGrid, DataView.
2.8
' Visual Basic
If radioCity.Checked Then
DataView1.Sort="City"
ElseIf radioCompanyName.Checked Then
DataView1.Sort="CompanyName"
End If

Dataview1.RowFilter="CustomerStatus='Active'"
Dataview1.RowStateFilter=DataViewRowState.CurrentRows
Me.DataGrid1.DataSource=DataView1
// C#

if (radioCity.Checked)
(

DataView1.Sort="City";
)

else if (radioCompanyName.Checked)

104

DataView1.Sort="CompanyName" ;

}
DataView1.RowFilter="CustomerStatus='Active'";
DataView1.RowStateFilter=DataViewRowState.CurrentRows;
this.DataGrid1.DataSource=DataView1;

2.1.16.
DataSet
,
select.
. , .
select, :
DataTable.Select(filterExp,

sortExp,

rowstatevalue)

. 2.6 .
2.6. DataTable

filterExp

, true
false

sortExp

. ,

rowstatevalue

,
.

DataViewRowState (,
DataViewRowState.CurrentRows)

. , .
(Nothing VB, null #).

select .
( 2.9),
Customers dataSet1. Active.
city, -

105

DataSet

,
. listBox1.
2.9
' Visual Basic
Dim filterExp As String="Status='Active'"
Dim sortExp As String="City"
Dim d r a r r a y ( ) As DataRow
Dim i As Integer
drarray=dataSet1.Customers.Select(filterExp,sortExp,DataViewRowState,Curr
entRows)
For i=0 To (drarray.Length-1)
listBox1.Items.Add(drarray(i)("City").ToString)
Next
// C#

string filterExp="Status='Active'";
string sortExp="City";
DataRow[] drarray;
drarray=dataSet1.Customers.
Select (filterExp, sortExp,DataViewRowState.CurrentRows) ;
for (int i=0; i<drarray.Length-1; i++)
listBox1.Items.Add(drarray[i]["City"].ToString());

( 2.10) ,
Select, (

).
2.10

' Visual Basic


drarray=dataSet1.Customers.Select(Nothing, Nothing,

DataViewRowState.Deleted)
// C#
drarray=dataSet1.Customers.Select(null,

null,DataViewRowState.Deleted);

106

2.1.17. DataView
DataView, , , DataView, .
, DataView. .
Q

DataView , , . ,
.

DataView A l l o w E d i t , A l l o w N e w
A l l o w D e l e t e true, , .


DataView .
1. Sort DataView ,
.
2.

Find FindRows DataView,


:
, Find;
,
FindRows.

( 2.11) , , .
2.11
' Visual Basic
DataView1.Sort="CustomerID"
Dim foundIndex as Integer=DataView1.Find(TextBox1.Text)

// C#

dataView1.Sort="CustomerID";
int foundIndex=dataView1.Find(textBox1.Text);

DataSet

107


DataView,
( ).
. ( 2.12),
(CustomerName) () Dataview1.
2.12
' Visual Basic

Dim DataView1 as New DataView(ds.Customers)


Dim cname As String=CType(DataView1(0)("CustomerName"), String)
// C#
DataView dataView1=new DataView(ds.Customers);
string cname=dataView1[0]["CustomerName"].ToString();

( 2.13) ,
DataView , Customers
(). (CustomerId, CompanyName City)
s .
2.13

' Visual Basic


Dim drv As DataRowView
Dim s String=""
For Each drv In DataView1
s &=drv("CustomerID").ToString & " "
s &=drv("CompanyName").ToString & " "
5 &=drv("City").ToString & ControlChars.CrLf
Next
TextBox1.Text=s
// C#

string s="";
foreach (DataRowView drv in dataView1)
{

s +=drv["CustomerID"].ToString()+" ";

108
s +=drv["CompanyName"].ToString()+" ";
s +=drv["City"].ToString()+"\n";
}
textBox1.Text=s;


DataView
, .

DataView,
AllowEdit false.
( 2.14) ,
(CompanyName),
2.14

' Visual Basic


DataView1(0)("CompanyName")="Fabrikam,

Inc."

// C#

dataView1[0]["CompanyName"]="Fabrikam, Inc.";

DataView
( 2.15) AddNew
DataView,
DataRowView.
2.15
' Visual Basic
Dim drv As DataRowView
drv=DataView1.AddNew()
// C#

DataRowView drv;
drv=dataView1.AddNew();
,
.

DataSet

109

DataView,
AllowNew false.

( 2.16) ,
DataView .
2.16
' Visual Basic
Dim drv As DataRowView
drv=DataView1.AddNew()
drv("CustomerID")="AAA"
drv("CompanyName")="AAFabrikam,
drv("City")="Aurora"

Inc."

// C#
DataRowView drv;
drv=dataView1.AddNew();
drv["CustomerID"]="AAA";
drv["CompanyName"]="AAFabrikam, Inc.";
drv["City"]="Aurora";


Delete DataView, , .

DataView,
AllowDelete false.

( 2.17) , () .
2.17
' Visual Basic
DataView1.Delete(0)

// C#
dataView1.Delete(0);

110

2.1.18. DataView
DataViewManager
Visual Studio DataViewManager, DataView, DataSet. DataViewManager .
.

DataViewManager
( ).

DataViewManager.
1. DataSet.
2. .
3. D a t a V i e w M a n a g e r .

4. DataSet DataViewManager
(DataSet), 1.
5. ,
DataViewManager DataViewSettings,

, DataViewManager, DataViewSettings.

DataViewManager,
data-binding , DataViewManager. , , Windows-
DataSource DataViewManager, DataMember .
( 2.18) ,
DataViewManager
. ,
dsCustomersOrders, Customers
() Orders (), DataViewManager
titles.
2.18

' Visual Basic


Dim dvm As New DataViewManager()

DataSet

111

dvm.DataSet=dsCustomersOrders1
dvm.DataViewSettings("Customers").Sort="CompanyName"
dvm.DataViewSettings("Orders").Sort="OrderDate"
Datagrid1.DataSource=dvm
Datagrid1.DataMetnber="titles"
// C#
DataViewManager dvm=new DataViewManager();

dvm. DataSet=dsCustomersOrders1;
dvm.DataViewSettings["Customers"].Sort="CompanyName";
dvm.DataViewSettings["Orders"].Sort="OrderDate";
dataGrid1.DataSource=dvm;
dataGrid1.DataMember="titles";

2.1.19. DataSet
Dataset DataRelation.

DataRelation
. ( ), ,
. DataRelation,
.
( ).
, customers ()
Orders ().
( CustomerID), ,
DataRelation.
DataRelation .
Q .
Q (, ).
DataRelation. -

112


(Recordset).

DataRelation, . ,
.


, ,
, DataRelation . , , customers (
CustomerID) Orders (
CustomerID).
DataRelation, .
DataRelation
. .
GetChildRows , DataRelation, . .
( 2.19)
. () Customers (),
(drarray) .
2.19

' Visual Basic


Dim RowCtr As Integer
Dim drarray() As DataRow
RowCtr=0

drarray =
dsCustomersOrders1,Customers(RowCtr).GetChildRows("CustomersOrders")

// C#
int rowCtr;
DataRow[] drarray;
rowCtr=0;

drarray =
dsCustomersOrders1.Customers[rowCtr].GetChildRows("CustomersOrders");

DataSet

113

RowCtr
() .
DataRow drarray ( , ). , ,
GetChildRows Customers,
dsCustomersOrders DataRelation.
(
), GetParentRow .
, .

DataRelation
DataRelation , :
Q , , ;
Q , .
, DataRelation, . ,
DataRelation, ForeignKeyConstraint
ChildKeyConstraint.

Unique true,
ParentKeyConstraint UniqueConstraint DataRelation.

,
, :
;
Q ;
Q .
. 2.7.
2.7.

Cascade

( )
,

114

2.7. ()

SetNull

,
DBNull.
"",

SetDefault


(
DefaultValue)

None

Default

Cascade


(Constraint-only Relations)
DataRelation,
Constraint only ,
,
. , ,
.

DataRelation XML-
DataRelation , , . DataRelation . DataRelation ,
XML-. XML-
.
DataRelation XML .
1.
2.

( ).
Solution Explorer XML- (xsd).

DataSet

115

Schema, (. 2.20).

. 2.20. XML-

3.

XML Schema Toolbox, Relation


(. 2.21).

. 2.21. Relation Toolbox

Edit Relation (. 2.22).


4. DataRelation,
Name.
5. , Parent element
, Child element
DataRelation.

6.

Key ,
DataRelation.

116

. 2.22. Edit Relation

, Key . , ,
( ), , .

7. Fields ,
(Key Field) (Forign Key Field)
. ,
, .
8. ,
( ), Create foreign key
constraint only.

DataSet

117

.
.
9. ,
, Enforce Constraints Update Rule Delete Rule.

Enforce Constraints,
DataRelation
, ,
.

10. DataRelation.
XML- (. 2.23).

. 2.23. XML

:
Design (XML-)
(
, )
Edit Relation;
Relation (KeyRef) Editor
DataRelation.
XML
, ( , )
Delete.

118

2.2.
Visual Studio .NET
Dataset,
,
.
, . DataSet, DataSet .
. ,
.

Windows-, DataSet,
DataSet ,
.

2.2.1.
,
,
DataSet.


. : , . , .

. ,
.
Update .
DataSet . 2.24.

DataSet

119

. 2.24. DataSet

DataSet .
(DataTable).
(DataRow). (DataColumn).
. DataSet
,
( , , ). , , , .
, RemoveAt
ROWS , . -

120

, , .

, () ,
DataSet. , , , DataSet . ,
,
, ,
DeleteCommand,
,
.


, , ()
(). ,

. , .
,
XML Web-service, , ,
.
, Merge
preserveChanges (Boolean), , . DataSet
, , . .
2.8 ,
.
2.8. ,
DataRowVersion

Dataset

Dataset

Original

James Wilson

James . Wilson

Current

Jim Wilson

James C. Wilson

preserveChanges=false (
) -

DataSet

121

targetDataset.Merge ( s o u r c e D a t a s e t ) (. 2.9).
2.9.
(preserveChanges=false)
DataRowVersion

DataSet

DataSet

Original

James . W i l s o n

James C. W i l s o n

Current

James C. W i l s o n

James C. W i l s o n

preserveChanges=true ( ) targetDataset.Merge(sourceDataset)
(. 2.10).
2.10.
(pressrveChartges=true)

DataRowVersion

DataSet

DataSet

Original

James . Wilson

James C. Wilson

Current

Jim Wilson

James C. Wilson


, . ( null),
,
,
.
,
.
:
Q , ;
Q ,
.
, ,
.

Windows- DataGrid
,

122

2
,
EndEdit CancelEdit.

BeginEdit,

,
Merge.
, EnforceConstraints
false.
EnforceConstraints true.


,
. , ,
, , . .
.

2.2.2.

:
, (Rowstate), ()
(DataRowVersion). ,
.

DataRowState

DataRow DataRowState, .
. 2.11 DataRowState 2.,11. DataRowState D a t a R o w

Added

DataRowCollection (
).
AcceptChanges

DataSet

123

2.11. ()

Deleted

DataRow.Delete DataRow

Detached

, DataRowCollection. DataRow
, , ,

Modified

unchanged

,
AcceptChanges

DataRowVersion
.
DataRow DataRowVersion,
.
. 2.12 DataRowVersion.
2.12. DataRowVersion DataRow

Current

. ,
AcceptChanges. ,

Default

( )

Original

() .
, - . ,

Proposed

, ( ).
(BeginEdit)
(EndEdit), RowChanging. CancelEdit

124

()
, . , , ,
, , () , ,
. ,
, , ,
,
.
( ).

() ,
, - ,
.
,
,
.
()
. , ,
,
( ). ,
Delete,
, .
HasVersion.
, DataRowVersion .


. , DataGrid Windows-,
, ,
.
GetChanges,
, .
,
GetChanges ( D a t a T a b l e . G e t C h a n g e s )

(DataSet.GetChanges).

DataSet

125

,
. ,
,
. GetChanges
, .
GetChanges
(DataRowState),

: , , ,
( ), .
, . ,
,
,
.


,
RowState .
, , RowVersion.
, ,
, . , ,
:
Q ,
;
.
, AcceptChanges ( ). ,
AcceptChanges .
Q . Fill ,
.

,
.

126

Fill
AcceptChangesDuringFill false.
RowState , DataSet,
Added,

Q ,
, XML Web-service.

, . , , , .

AcceptChanges :
Q (current) (original) (
);
Q , RowState
Deleted;

Q RowState Unchanged.
AcceptChanges .
( DataRow),
. (
DataTable),
. , ,
( DataSet), .
. 2.13 ,
, AcceptChanges.
2.13. AcceptChanges

DataRow.AcceptChanges

DataTable.AcceptChanges

DataSet.AcceptChanges

DataSet

127

Fill
, AcceptChanges,
Fill .

AcceptChanges RejectChanges,
. RejectChanges , Rowstate
Unchanged.

2.2.3.
,
,
, , . .
.

. .
Web-,

. Windows-,
RichTextBox
TextBox. Windows- Masked
Edit Control.

Q .
. ,
,
, ,
, . ,
, , "" ,
. ,
.
Q - ( ).

128

_2_

- . . ,
, .
,
- , . .

2.2.4. ,

, DataSet , , .
DataSet D a t a R o w , , .


- ,
.

:
Q , ;
Q .




.
( 2.20) , dataset1, , ( ).

DataSet

129

2.20
' Visual Basic
dataset1.Tables(0).Rows(4).Item(0)="Wingtip Toys"
dataset1.Tables(0).Rows(4).Item(l)="Buffalo"

dataset1.Tables [0] .Rows [4].ItemArray [0]="Wingtip Toys";


dataset1.Tables [0] .Rows[4].ItemArray[l]="Buffalo";
, 2.21, , ( , 2.20),
.
2.21
' Visual Basic
dataset1.Tables("Customers").Rows(4).Item("CompanyName")="Wingtip Toys"
dataset1.Tables("Customers").Rows(4).Item("City")="Buffalo"

dataset1.Tables["Customers"].Rows[4] ["CompanyName"]="Wingtip Toys";


dataset1.Tables["Customers"].Rows[4] ["City"]="Buffalo";

,
.
.
2.22 ,
CompanyName city customers
DsCustomers1.
2.22
' Visual Basic
DsCustomers1.Customers(4).CompanyName="Wingtip
DsCustomers1.Customers(4).City="Buffalo"

Toys"

dsCustomers1.Customers[4].CompanyName="Wingtip Toys";
dsCustomers1.Customers[4].City="Buffalo";

130


,
DataRow .
,
DataTable . ,
ExistingTable,
FirstName LastName.

.
1.

NewRow,
.
DataColumnCollection
( 2.23).

2.23
' Visual Basic
Dim anyRow as DataRow=ExistingTable.NewRow

// C#

DataRow

anyRow=ExistingTable.NewRow();

1. ( 2.24).
2.24
' Visual Basic
'
anyRow(0)="Jay"
anyRow(l)="Stevens"
'
anyRow("FirstName")="Jay"
anyRow("LastName")="Stevens"

// C#
//
anyRow[0]="Jay";
anyRow[1]="Stevens";
//

DataSet

131

anyRow["FirstName"]="Jay";
anyRow["LastName"]="Stevens";
3.

,
DataRowCollection ( 2.25).

Add

! 2.25
' Visual Basic
ExistingTable.Rows.Add(anyRow)
// C#

ExistingTable.Rows.Add(anyRow) ;



DataRow.
( 2.26) , .

2.26

' Visual Basic


Dim anyRow as DataRow=DatasetName.ExistingTable.NewRow
anyRow.FirstName="Jay"
anyRow.LastName="Stevens"
ExistingTable.Rows.Add(anyRow)
// C#

DataRow anyRow=DatasetName.ExistingTable.NewRow();
anyRow.FirstName="Jay";
anyRow.LastName="Stevens";
ExistingTable.Rows.Add(anyRow);


,
Delete . ,
.

132

Delete.
; , ( ).

Count
DataRowCollection, ,
.
( ) ,
RowState .
, , Count .

( 2.27) , Delete
customers
DsCustomers1.
2.27
' Visual Basic
DsCustomers1.Customers.Rows(0).Delete()
// C#

DsCustomers1.Customers.Rows[0].Delete();

2.2.5.
() ,
DataTable ,
, .
,
.
,

:
dataTableNameRowChanging,
dataTableNameRowChanged,

dataTableNameRowDeleting,

dataTableNameRowDeleted. ,
, .
. 2.14.

DataSet

133

2.14. Data Table

Columnchanging

( ).
,

ColumnChanged

RowChanging

, DataRow
.
BeginEdit, RowChanging , , ColumnChanging. BeginEdit ,
RowChanging ,
EndEdit
, ,
(, , )

RowChanged

. ,
, (, ,
)

RowDeleting

. , ,

RowDeleted

. ,
,

ColumnChanging,

RowChanging RowDeleting

. , .
, , , .
ColumnChanged, RowChanged RowDeleted

, ,
. ,
, .

2.2.6.

(, ), -

134

(, ). , ,
. , ,
(null).
. , ,
,
( ).
, ,
.
.
1.


BeginEdit DataRow.

,
BeginEdit, .

2.

3.

EndEdit ,
4. RowChanging.
5.

CancelEdit, ,
.
( 2.28) BeginEdit EndEdit.
,
customers
DsCustomers1.
2.28
' Visual Basic
DsCustomers1.Customers(4).BeginEdit()
DsCustomers1.Customers(4).CompanyName="Wingtip Toys"
DsCustomers1.Customers(4).City="Buffalo"
DsCustomers1.Customers(4).EndEdit()
C#

DataSet

135

dsCustomers1.Customers[4].BeginEdit();
dsCustomers1.Customers[4].CompanyName="Wingtip Toys";
dsCustomers1.Customers[4].City="Buffalo";
dsCustomers1.Customers[4].EndEdit() ;

2.2.7.
.
() (). , .
Q

,
, ,
.
XML Web-,
.

Q ( XML Web-),
.
Merge. :
Q , ;
Q , .
, , , ;
Q , , ,
.
Merge
, .
( 2.29) ,
PreserveChanges=false.

2.29

' Visual Basic


targetDataset.Merge(sourceDataset)
// C#

targetDataset.Merge(sourceDataset);

136

( 2.30) ,
PreserveChanges=true.
2.30
' Visual Basic
targetDataset.Merge(sourceDataset, True)
// C#

targetDataset.Merge(sourceDataset, true);

2.2.8.
(, ,
) (), . , RowState
(),
. ,
. , . , ,
,
AcceptChanges.
( 2.31) ,
AcceptChanges Customers .
2.31
' Visual Basic
OleDbDataAdapter1.Update(Dataset1, "Customers")
Dataset1.Customers.AcceptChangea{)
// C#

OleDbDataAdapter1.Update(Dataset1, "Customers");
Dataset1.Customers.AcceptChanges();

DataSet

1.
2.

137

Merge;
AcceptChanges.

( 2.32) ,
.
2.32

' Visual Basic


Dataset1.Merge(Dataset2)
Dataset1.AcceptChanges()
// C#

Dataset1.Merge(Dataset2);
Dataset1.AcceptChanges();

2.3.

,
(),
( ).
, , .

2.3.1.
, , . Update
.
, (, ), , , .


, ,
DataSet (. 2.15).

138

2
2.15. DataSet

RowState
(Unchanged)
(Unchanged)

CustomerID

c200
c400

Name
Robert Lyon
Nancy Buchanan

Status
Good
Pending


Nancy Buchanan Pending Preferred. DataRow.RowState Unchanged
Modified. (unchanged).
DataSet . 2.16.
2.16. DataSet
RowState

CustomerID

Unchanged

c200

Modified

c400

Name
Robert Lyon
Nancy Buchanan

Status
Good
Preferred

update , . . , SQL- ( DataCommand),


, ( Unchanged).
Update (DataCommand) . SQL- SQL, . SQL- .
Q SQL- UPDATE. (OleDbDataAdapter SQLDataAdapter),
UPDATE , Rowstate
Modified.
Q SQL- WHERE, ,
, CustomerID='c400'.
SQL- ,
CustomerID .
WHERE ,

()

(DataRowVersion.Original), .

DataSet

139

SQL- SET, ,
.

UpdateCommand , SQL-
.


, , -.
update ,
SQL- UPDATE,
- . SQL-
(Parameters)
( UpdateCommand
).
Visual Studio, UpdateCommand , - SQL-.
OleDbDataAdapter - (, " "):
- SQL- , . .
SqlDataAdapter , , .
SourceColumn . update ,
- SQL-.
UPDATE
, . ,
. ,
, .
:
SET, WHERE.

140

,
(
SqlParameter.SourceVersion). SET
, WHERE .

Parameters ,
RowChanging,



, , .
Update .
(insert, update delete),
Rowstate .


, update
Try...Catch.

, .
1.

Update Try...Catch.

2. , .
3.

( , ,
), .

( 2.33) ,
Try...Catch
myDataset.

DataSet
2.33
' Visual Basic
Try

oleDbDataadapter1.Update(myDataset)
Catch x As Exception
' .
End Try
// C#
try

{
oleDbDataAdapter1.Update(myDataset);
}

catch (Exception e)
{
// .
}

2.3.2.
,
, Update
. , .
.
,
. , . , , .
: , .
, ,
: , .
, , .

142



,
.
:
Q : ;
Q : , ;
Q : .
,
Update
.

, Update ( ).

( 2.34) , , . , , , . Update
Try...Catch.
, ( ).
AcceptChanges, "" , ,
, .
2.34
' Visual Basic
Private Sub Update_Attempt()
' ds1 Dim ds1 as DataTable =
anyDataset.ChildTableName.GetChanges(DataRowState.Deleted)
' ds2 .
Dim ds2 as DataTable =
anyDataset.ChildTableName.GetChanges(DataRowState.Added)

DataSet
' ds3 .
Dim ds3 as DataTable =
anyDataset.ChildTableName.GetChanges(DataRowState.Modified)

Try
'
DataAdapter2.Update(ds1)
'
DataAdapter1.Update(anyDataset, "ParentTable")
'
DataAdapter2.Update (ds2)
'
DataAdapter2.Update(ds3)
'
anyDataset.AcceptChanges
'
ds1.Dispose()
ds2.Dispose()
ds3.Dispose()
Catch x As Exception
' () .

End Try
End Sub
// C#

void Update_Attempt()

{
// ds1 .
DataTable ds1=
anyDataset.ChildTableName.GetChanges(DataRowState.Deleted);
// ds2 .
DataTable ds2=
anyDataset.ChildTableName.GetChanges(DataRowState.Added);
// ds3 .
DataTable ds3=
anyDataset.ChildTableName.GetChanges(DataRowState.Modified);

try

143

144

{
//
DataAdapter2.Update(ds1);
//
DataAdapter1.Update{anyDataset,"ParentTable"};
//
DataAdapter2.Update(ds2);
//
DataAdapter2.Update(ds3);
//
anyDataset.AcceptChanges();
//
ds1.Dispose();
ds2.Dispose();
ds3.Dispose();

}
catch (Exception x)
{
//

()

2.3.3.
, , ( ).
DataSet
, , , , .
Q , .
Q ,
( , ).
Q timestamp
(
).
, Fill , , .

DataSet

145

,
, SELECT SQL
. SQL-
UpdateCommand InsertCommand. -

, ( SELECT) --
.

2.3.4.
, . , . ,
. , ,
, "" . , , DataSet. , , ,
. .

2.3.5.

.
,
, (
). ,
.
, ,
,
,
.
,
, RowUpdated , . , .

146

status , . . update
,
.
.
1.

RowUpdated.

2.

status RowUpdated.

status ErrorsOccurred.

3.

, Status

UpdateStatus),

IgnoreUpdateErrors
.

4.

RowError . HasErrors .

( 2,35) , RowUpdated.
, RowError , HasErrors true. .
status , (
).
2.35
' Visual Basic
Private Sub 01eDbDataAdapterl_RowUpdated(ByVal sender As Object,
ByVal e As System.Data.OleDb.OleDbRowDpdatedEventArgs
Handles OleDbDataAdapter1.RowUpdated
If

e.Status=UpdateStatus.ErrorsOccurred

Then

e.Row.RowError=e.Errors.Message
' .
End If

'

147

DataSet
End Sub

// C#

private void OleDbDataAdapter1_RowUpdated(object sender,


tem. Data.OleDb.OleDbRowUpdatedEventArgs e)

Sys-

{
if (e.Status==UpdateStatus.ErrorsOccurred)
{
e.Row.RowError=e.Errors.Message;
// - .

}
}
.
ADO.NET .
.



ADO.NET
DataSet, . DataCommand.
, , .
--
.
.
:
Q DataCommand;
Q .
DataCommand.

3.1.
ADO.NET ,
: (DataSet),
( , , . .). ""
DataSet
, , ,
(, , ), ,

149

, ,
.
.
DataCommand,
SQL- . , (, ), (, ).
, DataReader .
. , (
), (
DataCommand).

3.1.1. DataCommand
SQL- ,
.
OleDbCommand SqlCommand. , OleDbCommand
OLE DB-,
SqlCommand SQL Server 7.0 .
DataCommand
,
SELECT, . ,
DataSet.
OleDbDataReader SqlDataReader, readonly, forward-only ( , )
.
,
.
Q DDL- (Database Definition Commands, ), , ,
, .
Q , .
Q SQL-, ,
, -

150

, DataSet, .
Q , ,
( ,
..).
Q , SQL
Server ( 7.0 ) XML. -. ,
XML, XSLT-, HTML- .

3.1.2. DataCommand
DataCommand ,
.
Q Connection. connection, .
Q . SQL-
.
Q Parameters. ,
( ). ,
. ,
.
, . ,
, ExecuteReader, DataReader.
, ,
ExecuteNonQuery, .


. ,
.
. ,
, . SQL- . SQL- -

151

, .

:
Q , ;
Q UPDATE INSERT SELECT. , ,
,
(, "").
, . , SQL- . SQL .


DataCommand
. .
Q . SQL-
, . "" ,
DataReader.
Q . , SQL- ,
(, ,
).
Q () . SQL-
, , .
Q XML. SQL Server
7.0 .

3.1.3. DataCommand

,
. -

152

, , . : SelectCommand, UpdateCommand, InsertCommand


DeleteCommand.
, . ,
DataAdapter,
SelectCommand DataReader
DataSet, , Update
, (UpdateCommand,
InsertCommand DeleteCommand). ,
.

3.1.4. DataCommand
ADO.NET DataCommand (, , . .) "" ( , ).
DataCommand Visual Studio , , .
DataCommand
.
1. DataCommand
:

, : SQL-
;
, .

2. , , DataCommand.
.
3. . SQL- ,
DataCommand. , , :
DataSet, ,
XML-.

153

4. ( ) .

DataCommand
(DataCommand),
OleDbCommand SqlCommand
. Visual Studio ,
.
DataCommand .
1. connection ( ,
).
2. Data Toolbox OleDbCommand
SqlCommand (. 3.1).

SqlCommand,
SQL Server 7.0 ; OleDbCommand.

. .1. SqlCommand Toolbox


3. DataCommand (. 3.1).
3.1. DataCommand

Name

154

3.1. ()

Connection

Connection,
.

CommandType

, :

Text SQL-,
StoredProcedure .
TableDirect . (
OleDbCommand)

CommandText

Parameters

() .
, CommandType:

Text, SQL-;

StoredProcedure,
;

TableDirect,

OleDbParameter

SqlParameter. ,

, ,
, . CommandText
Query Builder.

DataCommand
DataCommand,
, SQL-
. , update, ,
:
UPDATE Employees
SET LastName=@LastName, FirstName=@FirstName, BirthDate=@BirthDate
WHERE

(EmployeeID=@Emp_id)

,
(@LastName, @FirstName, @BirthDate @Emp_id). -

155

DataCommand Parameters,
OleDbParameter SqlParameter. Parameters.
, ,
, , .
Parameters DataCommand,
Properties . . 3.2 , Parametrs Properties.

, . Parameters,
, .

. 3.2. DataCommand

, , .

156

___

Parameters Value
, .
,
, .
,
Parameters ( au_id,
au_lname au_fname). CornrnandTex
CommandType , Properties DataCommand. 3.1 .
3.1
' Visual Basic
With OleDbCommand1
.CommandText="UpdateAuthor"
.CommandType=Sygtem.Data.CommandType.StoredProcedure
.Parameters("au_id").Value=listAuthorID.Text
.Parameters("au_lname").Value=txtAuthorLName.Text
.Parameters("au_fname").Value=txtAuthorFName.Text
End With
OleDbConnection1.Open()
OleDbCoramand1.ExecuteNonQuery()
OleDbConnection1.Close()

// C#
OleDbCommand1, CommandText="UpdateAuthor";
OleDbCommand1,CommandType=System.Data.CommandType.StoredProcedure;
OleDbCommand1.Parameters["au_id"].Value=listAuthorID.Text;
OleDbCommand1.Parameters["au_lname"].Value=txtAuthorLName.Text;
OleDbCommand1.Parameters["au_fname"].Value=txtAuthorFName.Text;
OleDbConnection1.Open();
OleDbCommand1.ExecuteNonQuery();
OleDbConnection1.Close();


, . .

157

, value,
.
1. , Direction
Output InputOutput. , .
2. , Value ,
.
ReturnValue.
3. , Direction
ReturnValue.

, ,
Parameters.

4. , .

update, insert Delete SQL- , , .


ExecuteNonQuery.

( 3.2) , , CountAuthors.
, Parameters
retvalue Direction ReturnValue.
3.2
' Visual Basic
Dim cntAffectedRecords As Integer
OleDbCommand1. CommandText="CountAuthors"
OleDbCommand1, CommandType=CommandType. StoredProcedure
OleDbConnection1.Open()
OleDbCommand1. ExecuteNonQuery ()
OleDbConnection1.Close ()
cntAffectedRecords=CType(OleDbCommand1.Parameters("retvalue").Value,
Integer)

158

MessageBox.Show("Affected records=" & cntAffectedRecords.ToString)

// C#

int cntAffectedRecords;
oleDbcommand1.CommandText="CountAuthors";
oleDbConmand1.CommandType=CommandType.StoredProcedure;
oleDbConnection1.Open();
oleDbCommand1.ExecuteNonQuery();
oleDbConnection1.Close();
cntAffectedRecords=(int)(OleDbCommand1,Parameters["retvalue"].Value);
MessageBox.Show("Affected records=" + cntAffectedRecords.ToString());

3.1.5. ,

DataCommand,
, , SQL- , SELECT.
, ,
, ,
"".

DataReader
(OleDbDataReader SqlDataReader). , , . DataReader forward-only,
read-only,
.

, , .
1. DataCommand .
2. CommandText SQL-
, .
3.

CommandType CommandType.Text (
SQL-) CommandType. StoredProcedure ( ).

4.

, .

5. DataReader OleDbDataReader

SqlDataReader, .

159

6. , .
7. ExecuteReader,
DataReader, 5.
8. , Read.
9. DataReader.

10. ExecuteReader,
.
( 3.3) , Authors Pubs (SQL Server). TableDirect Authors .
( ExecuteReader)
s (
, ,
), .
3.3
' Visual Basic
Dim dreader As System.Data.OleDb.OleDbDataReader
OleDbCommand1.CommandText="authors"
OleDbCommand1.CornmandType=CommandType.TableDirect
OleDbConnection1,Open()
dreader=01eDbCorrmandl.ExecuteReader(CommandBehavior.CloseConnection)
Dim s As String=""
While dreader.Read()
s &= dreader("au_id").ToString()
& vbTab &
dreader("au_fname")-ToString()
5 " " & dreader("au_lname").ToString & ControlChars.CrLf
End While
TextBox1.Text=s
dreader.Close()

// C#
System.Data.OleDb.OleDbDataReader dreader;
OleDbCommand1.CommandText="authors";
OleDbCommand1.CommandType=CommandType.TableDirect;
OleDbConnection1.Open();
dreader=01eDbCommandl.ExecuteReader(CommandBehavior,CloseConnection);

160

string s="";
while (dreader.Read())
{

s += dreader["au_id"].ToString() + "\t"
+ dreader["au_fname"].ToString() + " "
+ dreader["au_lname"].ToString () + "\n";
}
TextBoxl.Text=s;

dreader.Close();

( 3.4)
DataReader .
DataReader ListBox Windows-
( author_id authors).

SelectedIndexChanged ListBox. SELECT SQL ()
au_id.
( ListBox1). DataReader
,
( Read ). ,
, Read
IF- End If.
:
,
.
3.4

' Visual Basic


Private Sub btnLoadListBox_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnLoadListBox.Click
Dim dreader As System.Data.OleDb.OleDbDataReader
OleDbCommand1.CommandText="authors"
OleDbCommand1.CommandType=CoinmandType.TableDirect
OleDbConnection1.Open()
dreader=01eDbCommand1.ExecuteReader()
While dreader.Read ()
ListBox1.Items.Add(dreader("au_id"))
End While

161

dreader.Close()
OleDbConnection1.Close()

End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim dreader As System.Data.OleDb.OleDbDataReader
OleDbCommand2. CommandText =
"SELECT au_id, au_lname, au_fname FROM authors WHERE au_id=?"
OleDbCommand2.CornmandType=CommandType. Text
OleDbCommand2.Parameters ( "au_id") . Value=ListBox1.Text
OleDbConnection1.Open()
dreader=0leDbCommand2.ExecuteReader()
If dreader.Read() Then
txtAuthorID.Text=dreader("au_id"),ToString()
txtAuthorLastName.Text=dreader("au_lname") .ToString ()
txtAuthorFirstName.Text=dreader("au_fname").ToString()
End If

dreader.Close()
OleDbConnection1.Close()

End Sub

// C#

private void.btnLoadListBox_Click(object sender, System.EventArgs e)


(

System.Data.OleDb.OleDbDataReader dreader;
OleDbCommand1.CommandText="authors";
OleDbCommand1.CommandType=CommandType.TableDirect;
OleDbConnection1.Open() ;
dreader=OleDbCommand1.ExecuteReader();
while( dreader.Read())
{
ListBox1.Items.Add(dreader["au_id"]);
}

dreader.Close () ;
OleDbConnection1.Close () ;

162

Button6.Enabled=true;
}

private void ListBox1_SelectedIndexChanged(object sender,


System.EventArgs e)

System.Data.OleDb.OleDbDataReader dreader;
OleDbCommand2.CommandText =
"SELECT au_id, au_lname, au_fname FROM authors WHERE au_id=?";
OleDbCommand2.CommandType=CommandType.Text;
OleDbCowmand2.Parameters["au_id"].Value=ListBox1.Text;
OleDbConnection1.Open();

dreader=OleDbCommand2.ExecuteReader();
if (dreader.Read())
{
txtAuthorID.Text=dreader["au_id"l.ToString();

txtAuthorLastName.Text=dreader["au_lname"].ToString();
txtAuthorFirstName.Text=dreader["au_fname"].ToString();
}
dreader.Close(};

OleDbConnection1.Close();
}

3.1.6.
DataCommand
(OleDbCommand SqlCommand) -
, .
:
Q ,
, (DDL);
Q (Update, Insert Delete).

DataSet, DataCommand, .
,
. -

163

, , DDL-:
Q , -1,
;
Q , , .
, ,
.
,
.
1. ( SQL- ).
2. , .
3. , .
4. , , .
5. ExecuteNonQuery,
.

.
,
Parameters,
6. .
( 3.5) ,
. ,
, . , , CommandText. SQL- CREATETABLE,
, .
-1, .
.5
' Visual Basic
Dim newtablecmd As String

164

Dim cmdresults As Integer


newtablecmd="CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1)
PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)"
OleDbCommand1. CommandType=CommandType.Text
OleDbCommand1.CommandText=newtablecmd
OleDbConnection1.Open()

cmdresults=OleDbCommand1,ExecuteNonQuery()
OleDbConnection1.Close()
MessageBox.Show("After creating the table, results=" &
cmdresults.ToString)
// C#

string newtablecmd;
int cmdresults;
newtablecmd="CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1)
PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)";
OleDbCommand1.CommandType=CommandType.Text;
OleDbCommand1.CommandText=newtablecmd;
OleDbConnection1.Open();
cmdresults=OleDbCommand1.ExecuteNonQuery();
OleDbConnection1.Close();
MessageBox.Show("After creating the table, results=" +
cmdresults.ToString());

( .) ,
, Pubs (SQL Server),
Authors. , NewAuthor,
Insert Into () .
OleDbCommand2 , Parameters ,
.
.
ExecuteNonQuery, .
3.6

' Visual Basic


Dim cmdresults As Integer
OleDbcommand2.CommandText="NewAuthor"
OleDbCommand2.CommandType=CommandType.StoredProcedure


OleDbCommand2.Parameters("au_id").Value=TextBox1. Text
OleDbCommand2.Parameters("au_lname").Value=TextBox2.Text
OleDbCommand2.Parameters("au_fname").Value=TextBox3.Text
OleDbCommand2.Parameters("phone").Value=TextBox4.Text
OleDbCommand2.Parameters("address").Value=TextBox5,Text
OleDbCommand2.Parameters("city").Value=TextBox6.Text
OleDbCommand2.Parameters("st").Value=TextBox7.Text
OleDbCommand2.Parameters("zip").Value=TextBox8.Text
OleDbCommand2.Parameters("contract").Value=CheckBox1.Checked
OleDbConnection2.Open()

Try
cmdresults=OleDbcommand2.ExecuteNonQuery()
Catch ex as Exception
MessageBox.Show("Failed to execute command, err=" & ex.Message)

End Try
OleDbConnection.Close()
MessageBox.Show("Number of records inserted=" s cmdresults.ToString)

// C#
int cmdresults=0;
OleDbCommand2.CommandText="NewAuthor";
OleDbCommand2.CommandType=CommandType.StoredProcedure;
OleDbCommand2.Parameters["au_id"].Value=TextBox1.Text;
OleDbCommand2.Parameters["au__lname"].Value=TextBox2.Text;
OleDbCommand2.Parameters["au_fname"].Value=TextBox3.Text;
OleDbCommand2.Parameters["phone"].Value=TextBox4.Text;
OleDbCommand2.Parameters["address"].Value=TextBox5.Text;
OleDbCommand2.Parameters["city"].Value=TextBox6.Text;
OleDbCommand2.Parameters["st"].Value=TextBox7.Text;
OleDbCommand2.Parameters["zip"].Value=TextBox8.Text;
OleDbCommand2.Parameters["contract"].Value=CheckBox1.Checked;
OleDbConnection2.Open();

try
{
cmdresults=OleDbCommand2.ExecuteNonQuery();

165_

166

)
catch (Exception ex)

{
MessageBox.Show("Failed to execute command, err=" + ex.Message);

}
OleDbConnection,Close();
MessageBox.Show("Number of records inserted=" + cmdresults.ToString());

3.1.7. Data Command,



,
() . : SQL (SUM, COUNT
), ,
, ,
, . .
, , .
1. DataCommand .
2. CommandText DataCommand SQL-
.
3. CommandType CommandType.Text ( SQL) CommandType. StoredProcedure ( ).
4. , .
5. ,
.
6. ExecuteScalar,
.
7. .
( 3.7) ,
, SQL,
. SQL-
Products (). () . ,
(UnitsInStock).

3,7

167

' Visual Basic


Dim scalarcmd As String
Dim qtyinstock As Integer
scalarcmd="SELECT UnitsInStock FROM Products WHERE (ProductID=?)"

OleDbCommand3.ConmandType=CommandType. Text
OleDbCommand3.CommandText=scalarcmd

OleDbCommand3.Parameters("productid").Value=txtProductID.Text
OleDbConnection1.Open()
qtyinstock=CType(OleDbCommand3.ExecuteScalar(), Integer)

OleDbConnection1.Close()
MessageBox.Show("QtyinStock=" & qtyinstock.ToString)
// C#

string scalarcmd;
int qtyinstock;
scalarcmd="SELECT UnitsInStock FROM Products WHERE (ProductID=?)";
OleDbCommand3.CommandType=CommandType.Text;
OleDbCommand3,CommandText=scalarcmd;

OleDbCommand3.Parameters["productid"].Value=txtProductID.Text;
OleDbConnection1.Open();
qtyinstock=(int)(OleDbCommand3.ExecuteScalar());

OleDbConnection1.Close();
MessageBox.Show("QtyinStock=" + qtyinstock.ToString());

3.2.
ADO.NET .
,
,
. ADO.NET
.
DataSet.

-, .
DataSet, ,
DataAdapter.

168

(, DataGrid),
DataSet.

, , .
( DataCommand),
SQL- .
, ,
, . ( SELECT),
DataReader DataReader
. , , .
. , ,
.

3.2.1. DataSet
Visual Studio .NET DataSet
. .
Q . . ,
.
Q .
(,
, XML-, . .). DataSet, , , ,
, ,
.
Q .
DataSet , -, .
Q . DataSet

.
XML-, XML-,
XML .

169

Q . , DataSet , , DataCommand.
.
, (,
).
Q . DataSet,
(, customers
dataset.Customers).
, ,
. , Visual Studio
IntelliSense .

3.2.2.

( DataSet). , DataCommand, SQL-
. ( , ,
).
( SQL- SELECT), DataReader ( ).
.
. , ,
.
.
Q . , ( ).
Q .
, ,
. ,
, -.

170

Q . DataSet
. , DataSet , ,
"",
DataSet,
.
.
Web-, , .

3.2.3.


.

Web-
Web- DataCommand DataReader. Web , DataSet , .
:
Q ,
, ;
Q
XML Web-;
Q , . DataCommand DataReader,

, (, );
Q ;
Q XML- ;

171

Q , .

XML Web-
XML Web- , .
Web-.
XML Web- -,
( -) .
Web- DataSet:
XML Web- . , DataSet DataSet
. XML Web-
( , );
Q - ,
Web-.
DataCommand DataReader :
Q XML Web- ;
Q XML Web- ,
(DDL-), , ;
Q XML Web- ,
.

Windows-
Windows- . Windows- . Web-,
Windows-
.
:
Q
(, , , , ..);
Q Windows, ;

172

Q no , "Web".
DataCommand DataReader :
Q ;
Q , (DDL-), , ;
Q
( ). ,
.
,
, ,
, , . ,
. Windows-.

Windows-

ADO.NET

Windows- , - - . ,
Visual Studio .NET .
:
Q Windows- Windows-,
;
Q Windows-
DataGrid;
Q Windows- ;

;
Q (
,
DataGrid);
Q , .

4.1. Windows-
Windows.

174

Windows- .
Visual Studio .NET,
(. 4.1).

. 4.1. Visual Studio .NET

New Project. (. 4.2).


Projects Types (Visual Basic
Projects , Visual C# Projects),
. Visual
Basic.
Templates , . Windows Application.
( Name).

windowsAplication1. .
Location ,
. .

Windows- ADO.NET

175

. 4.2.

Visual Studio .NET


Form 1 (. 4.3).

. 4.3. Windows-

176

4.2. Windows-
Windows.
Windows- .
1. Visual Studio .NET,
2. Windows- H e l l o w o r l d .
3.

Properties Form1
startPosition centerScreen (
).

4. Toolbox Form1 ( Button).


5. Button1. Properties
Text . . 4.4
.

. 4.4. Windows-

.
1.
Button1_Click.

2.

Button1_Click
( 4.1).

4.1

' Visual Basic


MessageBox.Show ("Hello, World!")

Windows- ADO. NET

177

// #
MessageBox.Show("Hello, W o r l d ! " ) ;

.
1. <F5> .
2.

, .
"Hello, World!", . 4.5.

, 4.5. Windows-

3. Windows- Visual Studio.


, Visual
Studio .NET.
. , . :
Windows- Windows-.

4.3. Windows-

DataGrid
Windows , MS SQL Server.
(DataGrid).

DataGrid,
. Windows-,
.
,
. -

178

,
Windows-.
,
Pubs,
MS SQL Server. :
Q

Windows- , , Windows-;

Q Connection;
Q DataAdapter;
Q

Dataset;

DataGrid DataSet;

Q DataSet;
Q , .
, 4.6.

. 4.6. DataGrid

4.3.1.
.
1.

File, New Project

2.

Project Types Visual Basic


Projects Visual C# Projects Templates Windows Application.

Windows- ADO.NET

179_

3. - , Close Solution, .

. , .
.

,
. , SimpleGrid.
4. , .
Visual Studio Windows-
.

4.3.2. DataSet
Windows-
DataSet. DataSet (), .
.
Q , :
connection DataAdapter.
SQL-, , , .

.
Q . Visual
Studio DataSet,
.
,
. DataSet,
, .


Connection DataAdapter

DataAdapter, SQL-

180

DataSet.
Visual Studio.
connection DataAdapter
.
1.

Data Toolbox
OleDbDataAdapter (. 4.7).

. 4.7. OleDbDataAdapter Toolbox

, SqlDataAdapter, SQL Server 7.0 . OleDbDataAdapter ,


ADO.NET OLE DB- .
Data Adapter Configuration Wizard, Connection DataAdapter.

2.

.
( )
Pubs SQL Server (. 4.8).

, SQL Use SQL statements (. 4.9).

SQL-:
SELECT authors.* FROM authors

SQL-, Query
Builder (. 4.10).

Windows- ADO.NET

. 4.8. Connection

. 4.9.

181

182

. 4.10. SQL-

authors.
,
. , " Windows ".


Pubs authors Add,
Close Add Table (. 4.11).

All Columns.
, (. 4.12).

Data Adapter configuration Wizard (. 4.13).

DataAdapter (. 4.14).
Finish .
OleDbConnection1 OleDbDataAdapter1 .

Windows- ADO.NET

183

OleDbConnection1,
, , OleDbDataAdapter1,
, ,
.
. ,
.

. 4.11. SQL-

. 4.12.

184

. 4.13.

. 4.14. ,

Windows- ADO.NET

185

DataSet
,
, ( SQL-
), ,
DataSet. Visual Studio , , .
.
1.

Data Generate DataSet.

Data , ,
.

Generate Dataset (. 4.15).

. 4.15. Generate Dataset

2,

DataSet
DataSet1,

OleDbDataAdapter1

authors.

3.

, Add this dataset to the designer, .

186

Visual Studio DataSet1 ,


DataSet1.xsd.
Solution Explorer,
Visual Studio DataSet1.
, ,
( ). ,
DataGrid.

4.3.3. DataGrid


DataGrid, , .
TextBox,
, () .
DataGrid .
DataGrid .
1.
Form1, .
2.

Windows Forms Toolbox, DataGrid


(. 4.16).

. 4.16. DataGrid Toolbox

3.

<F4>
DataGrid.
4. DataSource DataSetll ( DataSet11.authors).

Windows- ADO.NET
5.

187

DataMember authors.
, DataGrid authors DataSet1.

DataGrid
.
6.

DataGrid , (. 4.17).

. 4.17. DataGrid

4.3.4.
Data Grid
, DataGrid DataSet
, . ,
.
DataGrid .
1.

Windows Forms Toolbox


Button,

2.

Text Button1 .

3.

click.

188

4.

DataSet11 clear,
, Fill OleDbDataAdapter1.

( 4.2)
.
4.2
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles Button1.Click

Sys-

DataSet11.Clear()
OleDbDataAdapter1.Fill(DataSet11)
End Sub
// C#

private void Button1_Click (object sender, System.EventArgs e)


{

DataSet11.Clear();
OleDbDataAdapter1.Fill(DataSetll);
}

, Visual Basic #
(
#). , (. 4.18).

. 4.18. DataGrid

Windows- ADO.NET

189

, Windows-, , . , , 2 ( -!). , , . Visual Studio


: " !"
DataGrid, "".
DataGrid
Auto format. (. 4.19)
, ,
"" .

. 4.19. Auto format DataGrid

4.3.5.
DataGrid .
DataGrid, DataSet.

( , ).
DataSet, . DataAdapter
Update, DataSet , , .

190

,
.

.
1.

Windows Forms Toolbox,


Button,

2.

Text Button2 .

3. Click.
4.

, Update OleDbDataAdapter1,
.

5.

MessageBox
.

, 4.3,
.
4.3
' Visual Basic
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles Button2.Click
OleDbDataAdapter1.Update(DataSet11)

Sys-

MessageBox.Show(" !")
End Sub
// C#

private void Button2_Click(object sender, System.EventArgs e)


(

OleDbDataAdapter1.Update(DataSet11);
MessageBox.Show(" !");

, DataGrid,

(. 4.20).
, DataGrid , .

Windows- ADO.NET

191

. 4.20.

4.4. Windows-

:
Q TextBox ;
Q DataSet;
Q Button,
() ;
Q Label, ,
.

authors Pubs: au_lname, au_fname , city state.
.
1. - , , : 4 TextBox, 2 Button, 6
Label.

2.

, . 4.21.

192

. 4.21.
3.

. 4.1.
4.1.

Name

Text

Label

""

Label1

TextBox

( lname)

TextBox1

Label

""

Label2

TextBox

( fname)

TextBox2

Label

""

Label3

TextBox

( city)

TextBox3

Label

" "

Label4

TextBox

( state)

TextBox4

Label

" "

Label5

Label

Label6

Button

Button3

Button

Button4


(. 4.22).

Windows- ADO.NET

193

. 4.22.

4.4.1. TextBox


.
, DataSet. TextBox.
DataSet .
1. .
2. TextBox1 <F4> .
3. DataBindings (. 4.23),
"+" .

. 4.23. DataBindings

194

4.

Text, ,
DataSetll.

5.

DataSetll
authors ,
TextBox1 ( au_lname). au_lname. Text authors.au_lname DataSet11 (. 4.24).

. 4.24.
DataSet

6.

2, 5 (TextBox2,
TextBox3, TextBox4),
DataSetll
authors.au_fname,
DataSet11 authors.state.

DataSet11

authors.city,

. , .
, , , DataGrid (. 4.25).
,
DataGrid. . ,
TextBox DataSet
.
, ,
authors Pubs.

DataGrid,
,
. ,
. ,
.

Windows- ADO.NET

195

. 4.25. DataGrid

, ( ) position
BindingContext. , .
Count
BindingContext. , , .
(Button3)
click. ( 4.4),
1 Position
BindingContext:

4.4
' Visual Basic

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


tem. EventArgs) Handles btnPrevious.Click:
Me.BindingContext(DataSetll, "authors").Position-=1
End Sub
// C#
private void Button3_Click(object sender, System.EventArgs e)
(

this.BindingContext[DataSet11, "authors"].Position-=1;
)

Sys-

196

(Button4) ( 4.5),
1 Position BindingContext:
4.5
' Visual Basic
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles btnPrevious.Click
Me.BindingContext(DataSet11,

Sys-

"authors").Position+=1

End Sub
// C#

private void Button4_Click(object sender, System,EventArgs e)

{
this.BindingContext[DataSet11,

"authors"].Position+=1;

,
.

4.4.2.
ShowPosition . Label6.
() ShowPosition.
Position, -
Count BindingContext
Label6. ( 4.6).
4.6
' Visual Basic
Private

Sub ShowPosition ()

Dim iCnt As Integer

'

Dim iPos As Integer

'

iCnt=Me. BindingContext

(DataSet11,

If iCnt=0 Then
Label6.Text=" (No records) "
Else

"authors") .Count

Windows- ADO.NET

197

iPos=Me.BindingContext(DataSet11, "authors").Position+1
Label6.Text=iPos.ToString & " " & iCnt.ToString
End If
End Sub
// C#

private void ShowPosition()

{
int iCnt;
int iPos;
iCnt=this.BindingContext[DataSet11,

"authors"].Count;

iPos=this.BindingContext[DataSet11,

"authors"].Position+1;

if(iCnt==0)
{
Label6.Text="(No records)";

}
else
{
Label6.Text=iPos.ToString() + " " + iCnt.ToString();
}
}

' Visual Basic


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles btnPrevious.Click
Me.BindingContext(DataSet11, "authors").Position-=1
ShowPosition()
End Sub
// C#

private void Button3_Click(object sender, System.EventArgs e)


{
this. BindingContext [DataSetll, "authors"] .Position-=1;

ShowPosition();

Sys-

198

Button4 ( 4.8).
4.8
' Visual Basic
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles btnPrevious.Click

Sys-

Me.BindingContext(DataSet11, "authors").Position+=1
ShowPosition ()

End Sub

// C#
private void Button4_Click(object sender, System.EventArgs e)
{

this.BindingContext[DataSet11,

"authors"].Position+=1;

ShowPosition();
}

, .

4.5.

. ,
. .
(
) , , .

, .
Windows-,
Pubs SQL-.
(, ) Dataset , . , , ,
.

Windows-

ADO.NET

199


Pubs, MS SQL Server.
:
Q ;
Q ,
SQL-,
;
Q

Q ,
, ;
Q ,
.

4.5.1. Windows-
Windows- .
1. File New Project.
2. New Project.
3. Project Types : Visual Basic Projects
Visual C# Projects, Templates
Windows Application.
4. , Close Solution New Project.
5. ,
. , WinParameters. .
Visual Studio Windows-
.

.
Q , . SQL-,

DataSet. SQL-,
.

200

. Visual Studio
, DataSet, e .

4.5.2.
DataConnection DataAdapter
DataConnection DataAdapter
.
1.

Data
OleDbDataAdapter.

Toolbox,

SqlDataAdapter, SQL Server 7.0 .


Data Adapter Configuration Wizard, DataConnection DataAdapter.
2. .
,
Pubs SQL- (. 4.26).

. 4.26. , Pubs

Windows- ADO.NET

201

, SQL .
SQL- (. 4.27):
SELECT id,

au_lname,

state FROM authors WHERE (state=?)

. 4.27. Query Builder

(?) - . ( SqlDataAdapter,
,
estate.)

SQL- Query Builder, Query Builder. - , Criteria


state.

3. ( OleDbConnection)
( OleDbDataAdapter1).

SQL-:
,
SELECT, , ( UPDATE, INSERT DELETE).

202

SELECT.
SelectCommand (. 4.28).

. 4.28. SelectCommand

SQL- SELECT
SelectCommand . SelectCommand ( OleDbCommand), SQL-, ,
SQL-.
Parameters.
( OleDbParameter),
- ( ) SQL-.
-, , Parameters -
state (. 4.29).

. 4.29. SelectCommand

Windows- ADO.NET

203

, Data Adapter Configuration Wizard


. - state,
state. ,
SQL-.

4.5.3.
,
, . Visual Studio
, SQL-,
. DataSet,
XML- (xsd-),
(, ).
DataSet .
1, Data Generate DataSet.

Data , .
, .

Generate Dataset (. 4.30).

. 4.30. Generate DataSet

204

2.

dsAuthors, , Add this dataset to the designer .

Visual Studio (dsAuthors) , . dsAuthors.xsd, Solution Explorer.


Solution Explorer Show All Files
, , ( vb cs, ),
,
(. 4.31).

. 4.31. Solution Explorer


Show All Files

, Visual Studio ( DsAuthors1 Visual Basic, dsAuthors1 #).


, , .
, .

4,5.4.
,
(
state), , , . .
, , . 4.2.
label ( )
. . 4.32.

Windows- ADO.NET

205

4.2.

(Name) Text

TextBox

txtStateParameter

( )

Button

- btnShow

TextBox

( )

TextBox
TextBox

txtAuthorID
txtAuthorLName

, txtAuthorState
( )

( )

( )
( )

. 4.32.

4.5.5.

, ( ).
DataSet ,
.
, ,
.
( ). -

206

TxtStateParameter,
. Fill .
.
, click. :
Q , SQL-. , txtStateParameter.
Q

clear.
, , SQL-, .

Fin , , .


( 4.9).
4.9
' Visual Basic
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles btnShow.Click
OleDbDataAdapter1.SelectCommand.Parameters("state").Value

txtstateParameter.Text
DsAuthors1.Clear()
OleDbDataAdapter1.Fill(DsAuthors1)
End Sub

// C#

private void btnShow_Click(object sender, System.EventArgs e)


{

OleDbDataAdapter1.SelectCommand.Parameters["state"].Value =
txtstateParameter.Text;
dsAuthors1.Clear();
OleDbDataAdapter1.Fill(dsAuthors1);

Sys-

Windows- ADO.NET

207

4.5.6.

txtAuthorID, txtAuthorLName, t x t A u t h o r S t a t e -

,
authors . ,
. .
. Text
, .

.
1. .
2. (txtAuthorID) <F4>, Properties.
3. DataBindings Text
dsAuthors au_id (. 4.33).

. 4.33.

4. 2 3 ,

DsAuthors1.authors.au_lname

DsAuthors1.authors,state.

,
. .

208

4.5.7.
,
DataSet. , , .
Position, BindingContext. ,
.
, count BindingContext.
.
1. ,
. 4.3.
4.3. Button
Name ()

Text ()

BtnNext

BtnPrevious

2. .
( )
position BindingContext. ( 4.10).
4.10
' Visual Basic
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrevious.Click
Me.BindingContext(DsAuthors1,

"authors").Position-=l

End Sub
// C#
private void btnPrevious_Click(object sender, System.EventArgs e)

{
this.BindingContext[dsAuthors1,

"authors"].Position-=l;

3.

, Position ( 4. ).

Windows- ADO.NET

209

4.11
' Visual Basic
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles btnNext.Click

Sys-

Me.BindingContext(DsAuthors1, "authors"),Position+=l
End Sub

// C#

private void btnNext_Click( object sender, System. EventArgs e)


(

this.BindingContext[dsAuthors1, "authors"].Position+=l;
}

4.5.8.
, ShowPosition .
1. TextBox, txtPosition
(. 4.34).

. 4.34.

Enabled False, , .

2. ShowPosition.
-

210

210

BindingContext . 4.12 .
4.12
' Visual Basic
Private Sub ShowPosition()
Dim iCnt As Integer
Dim iPos As Integer
iCnt=Me.BindingContext(DsAuthors1, "authors").Count
If iCnt=0 Then
txtPosition.Text="(No records)"
Else
iPos=Me.BindingContext(DsAuthors1, "authors").Position+1
txtPosition.Text=iPos.ToString & " of " & iCnt.ToString
End If
End Sub
// C#
private void ShowPosition()
{
int iCnt;
int iPos;

iCnt=this.BindingContext[dsAuthors1, "authors"].Count;
iPos=this.BindingContext[dsAuthors1, "authors"].Position+1;
if (iCnt==0)
{
txtPosition.Text=" (No records) ";

}
else

{
txtPosition.Text=iPos,ToString() + " of " + lent.ToString() ;

}
}

3. ShowPosition , .
:

Windows- ADO.NET

211

Fill click .
click . ,

( 4.13).
4.13
' Visual Basic
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrevious.Click
Me.BindingContext(DsAuthors1, "authors").Position-=l
ShowPosition()
End Sub

// c#
private void btnPrevious_Click(object sender, System.EventArgs e)
{

this.BindingContext[dsAuthors1, "authors"].Position-=l;

ShowPosition();
}

4.5.9.
, ,
,
.
.
1. <F5> .
2. , ()
, .

, .
3.

, (. 4.35).

212

. 4.35.

4. (, UT
), .
,
.
,
.
Q , .
SQL-:
SELECT DISTINCT state FROM authors

ListBox.

Q ( )
, Count
BindingContext . 4.14.
4.14
' Visual Basic
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles btnShow.Click
OleDbDataAdapter1.SelectCommand.Parameters("state").Value =
Parameter.Text

DsAuthors1.Clear()
OleDbDataAdapter1.Fill(DsAuthors1)

If Me.BindingContext(DsAuthors1, "authors").Count=0 Then

Sys-

txtState-

Windows- ADO.NET

213

MessageBox.Show(" " & txtStateParameter.Text)

txtStateParameter.Focus()
Else
ShowPosition()
End If
End Sub

//C#
private void btnShow_Click(object sender, System.EventArgs e)
{

OleDbDataAdapter1.SelectCommand.Parameters["state"].Value =
txtStateParameter.Text;

dsAuthors1.Clear();
OleDbDataAdapter1.Fill(dsAuthors1);

if (this.BindingContext[dsAuthors1, "authors"].Count==0)
{
MessageBox.Show(" " + txtStateParameter.Text);
txtStateParameter.Focus();
}

else
{

ShowPosition () ;
}
}

Q (DataGrid),
, .
Q

, , . .

4.6.
Windows-
, ( ). ,
,
.

214

Visual Studio
,
. ,
.
,
. Windows-,
( ), ( ).
:
Q Windows-;
Q ,
, ;
Q ;
Q ,
;
Q .

4.6.1. Windows-
Windows- .
1.

File New Project.

2.

New Project.

3.

Project Types : Visual Basic Projects


Visual C# Projects, Templates Windows Application.

4.

,
Close Solution New Project.

5. ,
. ,
WinRelation. .
.
: (Publishers) (Titles).
Pubs, SQL
Server. . ,
(pub_id), Publishers
Titles.

Windows- ADO.NET

215

4.6.2. DataConnection
DataAdapter

. ,
SQL-, . ,
.
. , ,
Publishers,
Titles.


.
1.

Data

Toolbox

OleDbDataAdapter.

SqlDataAdapter, SQL Server 7.0 .


Data Adapter Configuration Wizard, DataConnection DataAdapter.

2. :
,
Pubs SQL-;
,
SQL- ;

SQL- (. 4.36):
SELECT pub_id, pub_name FROM publishers

SQL- Query Builder, Query Builder.

3. Finish .
( OleDbConnection1)
( OleDbDataAdapter1),
SQL- Publishers.

216

. 4.36. Query Builder

4.

OleDbDataAdapter.
Data Adapter C o n f i g u r a t i o n Wizard.

5.

,
Publishers.

,
SQL- ;

SQL- Titles.
SELECT title_id, title, pub_id, price FROM titles

SELECT pub_id.
6.

OleDbDataAdapter2.

Windows- ADO.NET

217

4.6.3. DataSet
,
. Visual Studio
, SQL-, . DataSet,
XML- (xsd-),
(, ).
DataSet .
1. Data Generate DataSet.

Data , .
, .
Generate Dataset.
2.

New dsPublishersTitles.
, ,
(Choose which table(s) to add to the dataset), : Publishers Titles (. 4.37), .

. 4.37.

218

3.

Add this dataset to the designer .

Visual Studio
(DsPubliahersTitles)
,
. ( DsPublishersTitles.xsd) Solution Explorer.
, Visual Studio
(DsPublishersTitles1 dsPublishersTitles1,
, 1 ).

4.6.4.

,
" ". , ,
. DataSet
,
.

,
Publisher Titles .
(, , , .). .


.
1.

Solution Explorer
, (
DsPublishersTitles.xsd).
XML Designer Schema, (. 4.38).

2.

XML Schema Toolbox


Relation Titles ( ).
Edit Relation , (. 4.39).

, Visual Basic, . .

Windows- ADO.NET

219

. 4.38. XML Designer

. 4.39. Edit Relation

3. , ,
Edit Relation (. 4.4).

220

4
4.4.

(Value)

Name ()

Publisherstitles

Parent element ( )

Publishers

Child element ( )

Titles

Key Fields ( )

pub_id

Foreign Key Fields ( )

pub_id

Create foreign key constraint only


( )

4.

, 4.4, .

5.

Edit
Relation.
XML Designer
(. 4.40).

. 4.40. XML Designer


,
Edit Relation ,
6. XML Designer.
,
.

4.6.5.
:
ListBox DataGrid

Windows- ADO.NET

221

.
Labels .

ListBox
ListBox ( Publishers).
( ).
( Form1),
. XML
Designer, ( ).
1. Windows Forms Toolbox,
ListBox.

2. <F4> Properties.
3.

DataSource DsPublishersTitles1
dsPubiishersTitles1 (
VB #) .

4. DispiayMember .
Publishers, Publishers
, pub_name.
5.

, DisplayMember
publishers.pub_name (. 4.41).

. 4.41. ListBox

ListBox pub_name publishers.

222

6.

Label Text
(. 4.42).

. 4.42.

DataGrid
.
: , , .
,
,
. DataGrid.
DataGrid ,
, .
DataGrid T i t l e s , p u b l i s h e r s t i t l e s ,

,
Publishers Titles. publisherstitles
Publishers.
, (
), . , .
DataGrid .
1. Windows Forms Toolbox,
DataGrid.

2.

DataSource DsPublishersTitles1
dsPublishersTitles1 (
VB #) .

Windows- ADO.NET

3.

223

DataMember publishers.publisherstitles.
(. 4.43).

. 4.43. DataGrid

DataGrid . , .
4. , (. 4.44).

. 4.44. DataGrid

4.6.6.
,
. .

.
1. ,
Load.
2.

, (DsPublishersTities1), .
Fill ,
. (Publishers Titles).
( 4.15).

4.15
' Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load


DsPublishersTitles1.Clear()
OleDbDataAdapter!.Fill(DsPublishersTitles1)
OleDbDataAdapter.Fill(DsPublishersTitles1)
End Sub
// C#

private void Form1_Load(object sender, System.EventArgs e)


{
dsPublishersTitles1.Clear();
oleDbDataAdapter1.Fill(dsPublishersTitles1);
oleDbDataAdapter2.Fill(dsPublishersTitles1);
}

4.6.7.
, .
.
1. <F5> .
2.

:
, ListBox ;
, , (. 4.45).

Windows- ADO.NET

225

. 4.45.

, : Windows, . :
Q DataGrid
;
Q , (
XML Designer).

4.7.
DataGrid

. ,
. Visual Studio DataGrid,
, .
: , .
Northwind, SQL Server, : Customers, Orders Order Details.
Visual Studio ,

. , .
:
Q Windows-;
Q ;
Q ,
;
Q DataGrid (
);
Q ,
, DataGrid;
Q .

4.7.1. Windows-
Windows- .
1.
2.

File New, Project.


New Project.

3.

Project Types Visual Basic Projects


Visual C# Projects, Templates
Windows Application.

4.

,
Close Solution New Project.

5.

,
. ,
win_Relation. .

, .
,
.
.

4.7.2.
DataConnection DataAdapter
, SQL, -

Windows- ADO.NET

22?

.
.
.
Customers, Orders
Order Details.
1.

Data
SqlDataAdapter.

Toolbox,

Data Adapter Configuration Wizard, DataConnection DataAdapter.


1.

:
,
Northwind SQL-.

, SQL - Use SQL


statements.

SQL-:
SELECT

Customers.* FROM

Customers

SQL- Query Builder, Query Builder.

3. Finish .
( SqlConnectionl)
( SqiDataAdapter),
SQL- Customers.
4. SqlDataAdapter.
Data Adapter Configuration Wizard.
5. :
,
customers.

, SQL -
Use SQL statements.

SQL-,

orders:
SELECT Orders.* FROM Orders

SqlDataAdapter2.

228

6.

SqlDataAdapter. Data Adapter Configuration Wizard.


,
Customers.
, SQL -
Use SQL statements.
SQL-, Order Details.
SELECT [Order Details].* FROM [Order Details]

SqlDataAdapter3.
.

4.7.3.
.
DataGrid Button
.
1. Windows Form Toolbox,
DataGrid .
2. Button.
Properties Text .
.
. 4.46.
. .

. 4.46.

Windows- ADO.NET

229

4.7.4.

,
" ". ,
, , . DataSet
,
Relations.

DataSet,
. DataSet
: Customers, Orders Order
Details, , , DataSet ,
.
, Relations DataSet. Customers.
Orders.
CustomerID. Orders

order Details.
OrderID.
. 4.47.

. 4.47. Customers, Orders Order Details

DataGrid . .
1. ,
Button1 Click.

230

2. :
DataSet ( ds);
;
DataSet ( ds);
;
Customers, orders;
orders order
Details;

DataGrid
ds;
DataGrid Customers.
( 4.16).
4.16
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
tem.EventArgs) Handles Button1.Click
Dim ds As New DataSet()
SqlConnection1.Open()
SqlDataAdapter1.Fill(ds,

"Customers")

SqlDataAdapter2.Fill(ds, "Orders")
SqlDataAdapter3. Fill(ds, "OrderDetails")
SqlConnection1.Close()
ds.Relations.Add("",ds.Tables("customers").
Columns("CustomerID"),
ds.Tables("Orders").Columns("CustomerID"))
ds.Relations.Add(" ",
ds.Tables

("Orders").Columns("OrderID"),

ds.Tables("OrderDetails").Columns("OrderID"))
DataGrid1().DataSource=ds
DataGrid1().DataMember="Customers"
End Sub
// C#

private void Button1_Click (object sender, System.EventArgs e)

{
DataSet ds;

Sys-

Windows- ADO.NET

231

SqlConnection1.Open();
SqlDataAdapter1.Fill(ds, "Customers");

SqlDataAdapter2.Fill(ds, "Orders");
SqlDataAdapter3. Fill (ds, "OrderDetails");
SqlConnection1.Close();

ds.Relations.Add("",
ds.Tables("customers").Columns("CustomerID"),
ds.Tables("Orders").Columns("CustomerID"));
ds.Relations.Add(" ",
ds.Tables("Orders").Columns("OrderID") ,
ds.Tables("OrderDetails").Columns("OrderID"));
DataGrid1().0ataSource=ds;
DataGrid1().DataMember="Customers";
}

4,7,5.
,
DataGrid .
.
1. <F5> .
2. ,
DataGrid. ,
DataGrid customers, (. 4.48).

. 4.48. DataGrid Customers

232

DataGrid
.
(+),
. (+) , DataGrid

( (. 4.49).

. 4.49. DataGrid

. 4.49,
ANTON. ,
ANTON
DataGrid, Orders (. 4.50). , ANTON.

. 4.50. DataGrid
Customers Orders

Windows- ADO.NET

233

(+), ,
(. 4.51).

. 4.51. DataGrid

, . DataGrid (. 4.52).

. 4.52. DataGrid Customers,


Orders , Order Details

(+) ,
, .
DataGrid , ,

234

. ,
(
). , DataGrid

.
. ,
( ).
.
Visual
Studio .NET. . 10 .

4.8. ,
Windows-
, . , .
DataGrid. Windows-,
. , DataGrid (
) .
,
Northwind, MS SQL Server. :
Q

Windows- , , Windows-;

connection;

Q DataAdapter;
Q DataSet;
Q DataGrid DataSet;
Q Button;

Windows- ADO.NET

235

Q DataSet;
Q ,
, , .

4.8.1.
.
1. File, New, Project.
2. Visual Basic Projects
Visual C# Projects, Windows Application.
3. - , Close Solution New Project.
,
. , WinAddDel.
4. , .
Visual Studio Windows- .

4.8.2.


DataAdapter, SQL, DataSet.
, DataSet
.
1. Server Explorer.

Server Explorer . , View


Server Explorer.

Northwind,
Server Explorer Data Connections, Northwind.
1. Server Explorer Tables
Northwind.

236

3.

Categories, CategoryID, CategoryName,

Description .
: SqlConnectionl
SqlDataAdapter1. ,
( ) Categories Northwind.
4.

SqlDataAdapter1 Generate Dataset.


Generate Dataset,
New
Categories.

DataSetll.
, .

DataGrid, , .
DataGrid .
1.


Form1, .

2.

Windows Forms Toolbox DataGrid


.

3.

<F4>

DataGrid.

4.

DataSource DataSetll ( DataSet11.Categories).

5.

DataMember Categories.

, DataGrid Categories DataSetll.


DataGrid .
6.

Windows Forms Toolbox,


Button Text : Button1 , Button2 , Button3
.

7. DataGrid ,
,
. 4.53.

Windows- ADO.NET

237

. 4.53.

4.8.3.
.
. DataSet ( 4.17).
4.17
' Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load

Sys-

SqlDataAdapter1.Fill(DataSet11)

End Sub
//C#

private void Form1_Load(object sender, System.EventArgs e)


{
SqlDataAdapter1.Fill(dataSet11);

Fill .

.
SqlInsertCommand1 .
SqlDataAdapter1,
InsertCommand. ,
(+), CommandText .

238

Query Builder SQL- (. 4.54).

, 4,54. Query Builder


, categories, SQL
: @CategoryName @Description.
. 4.18.
4.18
' Visual Basic
Private Sub Button1_Click{ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'
SqlDataAdapter1.InsertCoramand.Parameters( "@CategoryName").Value=""
SqlDataAdapter1.InsertCommand.Parameters("@Description").Value=""
SqlConnection1.Open()
SqlDataAdapter1.InsertCommand.ExecuteNonQuery()
SqlConnection1.Close()
DataSet11.Clear()
SqlDataAdapter1.Fill(DataSet11)
End Sub
//C#
private void button1_Click(object sender, System.EventArgs e)

{
//
SqlDataAdapter1.InsertCoiranand.Parameters["@CategoryName"

].Value="";

Windows- ADO.NET

239

SqlDataAdapter1.InsertCommand.Parameters["@Description"].Value="";
SqlConnection1,Open() ;
SqlDataAdapter1.InsertCommand.ExecuteNonQuery();
SqlConnection1.Close();
dataSet11.Clear();
SqlDataAdapter1.Fill(dataSet11);
}

(@CategoryName
@Description) InsertCommand SqlDataAdapter1
( ). , InsertCommand ( ) . DataSetll
, Fill
. DataGrid.

SqlDeleteCommand1 .
SqlDataAdapter1,
DeleteCommand. ,
(+), CommandText
. Query Builder SQL-.
(. 4.55)
DELETE FROM Categories WHERE (CategoryID=@Original_CategoryID)

. 4.55. Query Builder

240


categories, @categoryID SQL. CategoryID
DataGrid ( ,
DataGrid).
. 4.19.
4.19
' Visual Basic
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles Button2.Click
'
Dim row As Int16
Dim CategoryID As Int16
row=DataGrid1.CurrentCell.RowNumber
CategoryID=DataSet11.Categories(row).CategoryID
SqlDataAdapter1.DeleteCommand.Parameters
("@Original_CategoryID").Value=CategoryID
SqlConnection1.Open()
SqlDataAdapter1.DeleteCommand.ExecuteNonQuery()
SqlConnection1.Close()
DataSet11.Clear()
SqlDataAdapter1.Fill(DataSet11)
End Sub
//C#

private void button2_Click(object sender.System.EventArgs e)


{

//
int row ;
int CategoryID;

row=dataGrid1.CurrentCell.RowNumber;
CategoryID=dataSet11.Categories[row],CategoryID;
sqlDataAdapter1.DeleteCommand.Parameters
["@Original_CategoryID"].Value=CategoryID;
SqlConnection1.Open();

Sys-

Windows- ADO.NET

241

SqlDataAdapter1.DeleteCommand.ExecuteNonQuery();
SqlConnection1.Close();
dataSet11.Clear();
sqlDataAdapter1.Fill(dataSet11);

: row - DataGrid,
CategoryID ( ).
row DataGrid, CategoryID
( row Categories DataSetll),

@Originai_CategoryID DeleteCommand
SqlDataAdapter1 CategoryID. , DeleteCommand
( ) . DataSetll ,
Fill .
DataGrid.


4.20.
4.20

' Visual Basic


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles Button3.Click
'
SqlDataAdapter1.Update(DataSet11)
End Sub

Sys-

//C#
private void button3_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Update(dataSet11);

update

SqlDataAdapter1. , .

242

,
Windows-, .
, , . Web- , , (Active Server Pages, ASP). Visual
Studio .NET Web- ,
Windows-. ASP- Web- .

ASP-
ADO.NET

ASP.NET Web Form Page. Web Form .
Web- ,
HTML-. .NET Web- . , .
"" HTML- , , . Web . ,
, , . , ,
( ).
:
Q ASP- Web-;
Q Web-;
Q Web ;
Q Web;
Q .

5.1.
, ,
Web-, -

244

_5

, .
:
Q Web-;
Q HTML- ;
Q ;
Q

, Web-
(. 5.1).

. 5.1, Web-

Web- Web Web-.


.
1.

File New, Project.

2.

New Project :
Project Types Visual Basic Projects
Visual C# Projects;
Templates ASP.NET Web Application;

Location URL () . http://,


. Web- (IIS)
5.0 , .NET Framework. Web-
(IIS) , http://localhost.
Web1 (. 5.2).

ASP- ADO.NET

245

, 5.2. Web-

, Close Solution New Project.

3,

, Web-,
Web-,
. , Web- WebForm1.aspx, Web Forms
Designer.

5.1.1. Web-
, Web-,
Web Forms Designer. Web Forms Designer , WebForm1.aspx. Web-
.
Q aspx HTML- , .
Q WebForm1.aspx.vb WebForm1.aspx.cs (
VB #) ( ). Solution Explorer . -

246

Show Files
Solution Explorer, WebForm1.aspx (. 5.3).

. 5.. Web- Solution Explorer

Web Forms Designer : Design HTML, aspx-, .


Q (Design) ,
( WYSIWYG). ,
, Properties
.
Q HTML , HTML.
Web Forms Designer
Intellisense.
Web- .
- Web- ,
.
, Web-.

5.1.2.
Web-, ,
.
, .
- ( f l o w layout mode),

HTML-, . -

ASP- ADO.NET

247

:
, . .
Web- (. 5.4).

. 5.4. Web-

Web-
Web- " ", , , . (, ), , , . , ,
,
.
Web- . , HTML- , .
HTML-, , HTML- submit, ( HTML- ,
L-)
, Web-,
. .

248

HTML- . HTML, , . ,
HTML- HTML ( ).

Q Web- . ,
Web-,
, HTML.
.
HTML Web- .
1.

Design
.

2.

HTML Toolbox Text


Field (. 5.5).

HTML ( ),
, HTML-:
<INPUT style="Z-INDEX: 101; LEFT: 22px; POSITION: absolute; TOP: 17"
type="text">

3.
4.

.
HTML , Run As Server Control.

, , (. 5.6).

. 5.6.


. 5.5.
Text Field Toolbox

ASP- ADO. NET

249

HtmlInputText, .
Q (id),
.
HtmlInputText
Text1.
Q

runat server. , Web-.

INPUT, HTML :
<INPUT style="Z-INDEX: 101; LEFT: 22; POSITION: absolute; TOP: 17px"
type="text" id="Text1" name="Text1" runat="server">

Web- Web-
Web Forms Toolbox ( HTML)
Web- Button (. 5.7).

. 5.7. Button Toolbox

Web Forms , (Designer).

( HTML). HTML, :
< asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 74px" runat="server" Text="Button"></asp:Button >

() HTML.
Web-,
Button Web- .

250

HTML-
(grid layout mode) Web-. HTML-? , ,
(flow layout mode).
Web-
HTML Label,
.
.
grid layout mode :
1.

HTML Toolbox Web-


Label ( 5.8).

. 5.8. Label Toolbox


(. 5.9).

. 5,9.

2.

Label, ,
. ,
click, Doubleclick.

Label
, .

ASP- ADO.NET

251

3.

, Web-, , " Web-".

4.

, Build Style. , , , ..
(. 5.10).

. 5.10. Label

5. , . , . 5.11.

. 5.11. Web-

252

5.1.3.
Web- . ,
. , Button click,
Web-.
. , .
ASP.NET Framework ,
, . , , .
Web-
Button .
1.

Button.

click Button. ( 5.1).

5.1
' Visual Basic

Private Sub Button1_Click(ByVal sender as System.Object, ByVal e as


tem. EventArgs) Handles Button1.Click

Sys-

End Sub
// C|
private void Button1_Click(object sender, System.EventArgs e)
{
}

2.

HtmlInputText value. 5.2.

5.2
' Visual Basic
Private Sub Button1_Click(ByVal sender as System.Object, ByVal e as
tem. EventArgs) Handles Button1.Click

Sys-

ASP- ADO.NET

253

Text1.Value=" Web-!"

End Sub
// #
private void Button1_Click(object sender, System.EventArgs e)

{
Text1.Value="

Web-!";

5.1.4. Web-
Web-
Web-,
,
.
Web- :
1. Solution Explorer
WebForm1.aspx Build and Browse.

, <CTRL+F8>,
Build and Browse.

Visual Studio Web-


Browse tab.
2.

Web-.
" Web-!"
TextBox (. 5.12).

. 5.12. Web- Visual Studio

3. Web-
, Browse.

254

, <F5>. , ,
Web- (. 5.13).

. 5.13. Web-

5.2. Web-
, Web-, . ,
, .
Categories Northwind DataGrid.
DataSet (-
), .

DataGrid, . , Web- DataGrid,
, Windows.

Web-, .
,
.

:
Q Northwin, SQL
Server;

ASP- ADO.NET

255

Q ASP.NET- ,
Web-.
:
Q Web- Web-;
Q (
,
);
Q DataGrid ;
Q .

5.2.1.
Web- Web-.
1. File New
Project.
2. New Project
:

Project Types Visual Basic Projects


Visual C# Projects;
Templates ASP.NET Web-Application;

Location URL .
http://, .
Web- (1IS) 5.0 , .NET
Framework. Web- (IIS) ,
http://localhost.
, , Web2.
, Web-, Web-, .
, Web- WebForm1.aspx.

5.2.2.
, , ,
(categories).
.

256

Q , .
SQL-
. SQL-,
.
Q

. Visual Studio
Dataset , . DataSet,
.

,
DataSet, .


DataConnection DataAdapter
, SQL, .
.
.

.
1.

Data
OleDbDataAdapter.

Toolbox

SqlDataAdapter,
SQL- 7.0 .
OleDbDataAdapter
.
Data Adapter Configuration Wizard.
2.


(Connection) Northwind SQL-;

SQL-.
Windows
.

Connection,

ASP- ADO.NET

257

Connection, SQL.

,
SQL- ;
SQL-:
SELECT

CategoryID,

CategoryName, Description FROM Categories

SQL- Query
Builder, . Query Builder (. 5.14).

. 5.14. Query Builder


categories. , .

Finish.
OleDbConnection1
, .
OleDbDataAdapter 1, .

.

258

DataSet
Connection DataAdapter, Visual Studio
. --
DataSet, (xsd-).
(,
).
DataSet .
1.

Data Generate DataSet.

Data ,
.

Generate DataSet.
2.

New ds Categories.
Choose which table(s) to add to the dataset
Categories (. 5 . 1 5 ) .

. 5.15. DataSet

3.

Add this dataset to the designer .

ASP- ADO.NET

259

Visual Studio
(dsCategories) , ( dsCategories.xsd
Solution Explorer).

Solution Explorer Show All Files,


(..vb cs-), , .

, , Visual Studio (DsCategories1).


,
.

5.2.3. DataGrid

Web- DataGrid Categories.
DataList Repeater,
DataGrid .
DataGrid .
1.

Web Forms Toolbox


DataGrid (. 5.16).

. 5.16. DataGrid Toolbox

260

2.

Properties AutoFormat , ,
Professionall (. 5.17).

, 5.17. AutoFormat DataGrid

3. DataSource DsCategories1 (
).
4. DataMember Categories ( ). (. 5.18).

. 5.18. DataGrid

categories
DsCategories1 DataGrid1.

ASP- ADO.NET

261

5.2.4.
DataGrid
DataGrid , .
, Fill .
, ,
DataGrid . DataGrid .

DataGrid .
1.
Web- (Code Editor).
2.

Page_Load
Fill , :
OleDbDataAdapter1.Fill(DsCategories1)

3. DataBind DataGrid1
.
DataGrid1.DataBind()


, Web- .
, .
,
IF
IsPostBack. ( 5.3).
5.3
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load
If Not IsPostBack Then
OleDbDataAdapter!.Fill{DsCategories1)
DataGrid1.DataBind()
End If
End Sub

Sys-

262

private void Page_Load(object sender.System.EventArgs e)

{
if ( !IsPostBack)
{
OleDbDataAdapter!.Fill(dsCategories1) ;
DataGrid1.DataBind();

}
}

5.2.5.
.
1. .
2.

Solution Explorer
Build and Browse.
Categories
(. 5.19).

. 5.19. DataGrid

5.3. ,

Web- , ( ). -

ASP- ADO.NET

263

: , -, . .

, .
. ,
( DataSet DataAdapter). :
. ,
(data
reader),
(, ).
read-only, forward-only ( ,
), , ,
.
, Web- DataGrid,
, ,
.
:
Pubs SQL-;
Q ASP.NET Web.
:
Q Web-;
Q ;
Q DataGrid
;
Q .

5.3.1.
Web-.
.
1. File New, Project.

2. New Project :

264

Project Types Visual Basic Projects


Visual C# Projects;

Templates ASP.NET Web Application;

Location URL .
http://, .
Web- (IIS) 5.0 , .NET
Framework. Web- (IIS) ,
http://localhost.
Web3.

, Close Solution New Project.

5.3.2.
, , . SQL-,
. , , SQL SELECT.

Connection
Connection, , .
Connection .
1.

Data Toolbox
OleDbConnection (. 5.20).

SqlDataAdapter, SQL- 7.0 .


OleDbDataAdapter
.
2.

Connection Properties
ConnectionString (. 5.21).

ASP- ADO.NET

. 5.20. OleDbConnection
Toolbox

265

. 5.21. ConnectionString

OleDbConnection


connection New Connection.
3. pubs SQL-, New Connection.
(Data Link
Properties) (. 5.22).

. 5.22.

266

4. , SQL-
Pubs, .

SQL-. Windows
. Connection, SQL-.

ConnectionString Connection.
, ,
SQL- .
Command SQL-
.
1.

Data

Toolbox

OleDbCommand (. 5.23).

SqlCommand, SQL- 7.0 . OleDbCommand


.

2. command Properties
Connection, ,
(. 5.24).

. 5.23. OleDbCommand
Toolbox

. 5.24.
Connection OleDbCommand

ASP- ADO.NET

267

3.

CommandText ,
Query Builder.

4.

(. 5.25):

Query

Builder,

SQL-

Select au_id, au_fname, au_lname, phone From authors

. 5.25. Query Builder

authors. , .

5.

Query Builder, SQL-


CommandText,

.
, .

5.3.3,
DataGrid,
Web- .

268

_ 5

DataGrid .
1.

Web Forms Toolbox


DataGrid.

2.

Properties, AutoFormat,
DataGrid.
AutoFormat Simple 2 (. 5.26).

. 5.26. Simple 2 AutoFormat


DataGrid


DataGrid DataSource.
, . , . Web (. 5.27).

. 5.27. Web-

ASP- ADO.NET

269

5.3.4.

,
SQL-, .
""
( DataReader). .
.
1. Web- Page_Load.
2. :
OleDbDataReader;

;
.
ExecuteReader oleDbCommand1,

SQL- ,
OleDbDataReader;

datasource DataGrid1 OleDbDataReader.


, OleDbDataReader
;

DataGrid ;

OleDbDataReader;

. .
,
. , ,
.
, 5.4, .

Reader
.
5.4
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load

Sys-

270

If Hot IsPostBack Then


oleDbConnection1.Open()
Dim Reader As System, Data.OleDb.OleDbDataReader
Reader=oleDbCommand1.ExecuteReader()
DataGrid1.DataSource=Reader
DataGrid1.DataBind()
Reader.Close()
oleDbConnection1.Close()
End If
End Sub

// C#
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack) {
OleDbConnection1.Open();

System.Data.OleDb.OleDbDataReader Reader;
Reader=oleDbCommand1.ExecuteReader();
DataGrid1.DataSource=Reader;
DataGrid1.DataBind();
Reader.Close();
oleDbConnection1.Close() ;
}
}

5.3.5.
.
Web- .
1. .
2, Solution Explorer Web-
Build and Browse.
<F5>
Start .
authors
(. 5.28).

ASP- ADO.NET

271

. 5.28. DataGrid

5.4.
Web-
Web- . ,
.
Web- , , "" ,
.
.
. , ,
,
,
, -,
SQL- .
, , . Web-, -

272

SQL-. categories Northwind SQL-. , .


:
, SQL-;
Q SQL-
(DataReader) ;
Q ;
Q .
:
Q Northwind, SQL-;
Q ASP.NET- Web-.
:
Q

Web-;

Q ;
Q ;
Q .

5.4.1.
Web- Web-.
.
1. File New Project.
2.

New Project :
Project Types Visual Basic Projects
Visual C# Projects;
Templates ASP.NET Web Application;
Location URL .
http://, .
Web- (IIS) 5.0 , .NET

ASP- ADO.NET

273

Framework. Web- (IIS) ,


http://localhost.
Web4.

, Web- Web-, . , Web- WebFortn1.aspx,


Web Forms Designer.

5.4.2.
, , .
( SQL-
SELECT, SQL-
UPDATE). , . ( SQL), SQL-.


Connection
Connection.
connection .
1.
Data Toolbox

SqlConnection (. 5.29).

. 5.29. SqlConnection Toolbox

SqlConnection, SQL- 7.0 . -

274

5
,
OleDbConnection, .

3. Properties
ConnectionString.


New Connection.

4. Northwind
SQL-, New Connection.

Properties).

(Data

Link

5. SQL-, Northwind
(. 5.30).

. 5.30.

SQL-. Windows

ASP- ADO.NET

275

. Connection, SQL-.


Connectionstring Connection.

, . . ,
:
Q (CategoryID);
Q ( );
Q .

.
1. Data Toolbox SqlCommand
(. 5.31).

. 5.31. SqlCommand Toolbox

2.
3.

cmdCategoriesAll.
, Properties
connection. ,
( Existing).

4. CommandText Query Builder.


5.

Query Builder, SQL-:


SELECT CategoryID, CategoryName, Description FROM Categories

6.

Query Builder
(. 5.32).

276

. 5.32. Query Builder

SQL- CommandText.
,
.
categories .
1. SqlCommand Connection SqlConnection1.
2. cmdCategoriesById.

3. CommandText Query Builder.


4.


(. 5.33):

Query

Builder

SQL-

SELECT CategoryID, CategoryName, Description


FROM Categories
WHERE (CategoryID=@categoryid).

@categoryid " ".


, , -

ASP- ADO.NET

277

. " @ "
SQL-, .

. 5.33. Query Builder

UPDATE ,
.
Categories .
1. SqlCommand Connection SqlConnection1.
2. cmdCategoriesUpdate.

3. CommandText :
UPDATE Categories
SET CategoryName=@categoryname,
Description=@categorydescription
WHERE (CategoryID=@categoryid)

278

5.4.3.
: ,
,
.
.
1. Web Forms Toolbox
( . 5.1).
5.1. Web-

DropDownList

ID: ddlCategoryID
AutoPostBack: true

TextBox

ID:

txtCategoryNarne

TextBox

- ID:
txtCategoryDe
scription

Button

ID: btnSave
Text:

2. (Lebel) .
(. 5.34).

. 5.34.

ASP- ADO. NET

279

5.4.4.

, .
Q ()
().
( ) ,
Categories.

, ,
. , , SQL-,
Categories .

, , ,
.


Web-
, ,
Categories.
, Web- .
.

( SqlDataReader) .
DataReader
( read-only, forward-only),

.
1.

Web-,
page_Load.
2. :
IsPostBack, , . ;
dreader
SqlDataReader;

280

ExecuteReader cmdCategoriesAll,

SQL-
dreader;
, ,
;
;
( );
( Text);
dreader;
. , .
5.5 , .
5.5
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load

Sys-

If Not IsPostBack Then


Dim dreader As SqlClient.SqlDataReader
SqlConnection1.Open()
dreader=cmdCategoriesAll.ExecuteReader()
Dim firstrow As Boolean=True
While dreader.Read()
ddlCategoryID.Items.Add(New

ListItem(dreader(0).ToString()))

If firstrow Then
txtCategoryName.Text=dreader(1).ToString
txtCategoryDescription,Text=dreader(2).ToString
firstrow=False

End If
End while
dreader.Close()
SqlConnection1.Close()

ASP- ADO.NET _ 281

End If
End Sub
// C#
private void Page_Load( object sender, System.EventArgs e)
{

if (!IsPostBack)

{
System.Data.SqlClient.SqlDataReader dreader;
sqlConnection1.Open();
dreader=cmdCategoriesAll.ExecuteReader();
bool firstrow=true;
while(dreader.Read())
{
ddlCategoryID.Items.Add(new ListItem(dreader[0].ToString()));

if (firstrow)
{
txtCategoryName.Text=dreader[1].ToString();

txtCategoryDescription.Text=dreader[2].ToString();
firstrow=false;

}
}
dreader.Close();
SqlConnection1.Close();

,

.
,
SelectedIndexChanged,
categories, .
,
AutoPostBack true. , , .
SQL- WHERE, -

282

. , , . , ( SqlParameter), .

.
1. ,
SelectedIndexChanged (
).
2. :
, ;

@categoryid

cmdCategoriesById , ;
;

- SqlDataReader
( ) ExecuteReader cmdCategoriesById, . , (CommandBehavior.SingleRow);

Read , ( );

(DataReader);
(connection).
5.6

SelectedIndexChanged, .

5.6
' Visual Basic
Private Sub ddlCategoryID_SelectedIndexChanged(ByVal sender As
tem.Object, ByVal e As System.EventArgs) Handles
ddlCategoryID. SelectedIndexChanged

Dim categoryid As String


categoryid=ddlCategoryID.SelectedItem.Text
cmdCategoriesById.Parameters("@categoryid").Value=categoryid

Sys-

ASP- ADO.NET

283

SqlConnection1.Open()
Dim dreader As SqlClient.SqlDataReader
dreader=cmdCategoriesById.ExecuteReader(CommandBehavior.SingleRow)

If dreader.Read() Then
txtCategoryName.Text=dreader(1)
txtCategoryDescription.Text=dreader(2)

End If

dreader.Close()
SqlConnection1.Close()
End Sub

// C#
private void ddlCategoryID_SelectedIndexChanged(object sender,
tem.EventArgs e)
{

Sys-

string categoryid;
categoryid=ddlCategoryID.SelectedItem,Text;

cmdCategoriesById,Parameters["@categoryid"].Value=categoryid;
SqlConnection1.Open();
System.Data.SqlClient.SqlDataReader dreader;
dreader=cmdCategoriesById.ExecuteReader(CommandBehavior.SingleRow);
if (dreader.Read())
{
txtCategoryName.Text=dreader[1].ToString();
txtCategoryDescription.Text=dreader[2].ToString();

}
dreader.Close();
SqlConnection1.Close();


, ,
, .
.
UPDATE .
.
I.

, Click.

284

1.

:

Web-;
;
ExecuteNonQuery
cmdCategoriesUpdate;

.
5.7 click, .
5.7
' Visual Basic
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
With cmdCategoriesUpdate
.Parameters("@categoryid"),Value=ddlCategoryID.SelectedItem.Text
.Parameters("@categoryname").Value=txtCategoryName.Text
.Parameters("@categorydescription").Value

txtCategoryDescription.Text
End With
SqlConnection1.Open()
cmdCategoriesUpdate.ExecuteNonQuery()
SqlConnection1.Close()
End Sub
// C#

private void btnSave_Click(object sender, System.EventArgs e)


{

cmdCategoriesUpdate.Parameters["@categoryid"].Value =
ddlCategoryID.SelectedItem.Text;
cmdCategoriesUpdate.Parameters["@categoryname"].Value =
txtCategoryName.Text;
cmdCategoriesUpdate.Parameters["@categorydescription"].Value =
txtCategoryDescription.Text;
SqlConnection1.Open();
cmdCategoriesUpdate.ExecuteNonQuery(};
SqlConnection1.Close();
}

ASP- ADO.NET

265

5.4.5.
.
1. Solution Explorer Build and Browse <F5>.
2.

, ,
.

3. .
(. 5.35).

. 5.35. ,


, ,
AutoPostBack DropDownList true.

4.

5.

, ,
.

286

5.5. Web-

Web- on-line.
-, () .
,
. Web-
.
Web-
. Web-
DataSet, DataGrid DataView, -

. , .

5.5.1. Web-
Web- .
1.

File New, Project.


New Project.

2.

Project Type Visual Basic Projects Visual C# Projects, Templates ASP.NET Web Application.

3.

MyWebForm, Web Location.

4.

.

:

W e b F o r m 1 . a s p x Web-;
W e b F o r m 1 . a s p x . c s W e b F o r m 1 . a s p x . v b
,
. Solution
Explorer, Show All Files
WebForm1.aspx.

Solution Explorer, View


Solution Explorer.

ASP- ADO.NET

287

, , .
1. Solution Explorer .
2.

Add, Add
Component.
Add New Item Component Class,
,
(. 5.36).

. 5.36, Add New Item

Visual Basic,
Module .

3. Component1 Open.
,
Component1.cs Component1.vb .
Component Designer,
.

288

5.5.2.

DataSet
, HTML, .
.
1.

View Toolbox.

2.

Data Toolbox, sqlDataAdapter


Component Designer (. 5.37).

. 5.37. sqlDataAdapter Toolbox

DataAdapter.

3.

Next
Choose Your Data Connection, New
Connection, Pubs,
.
New Connection, (Data Link Properties). , 4.

4.

Connection Data Link Properties


:
, Pubs;
, ;
Pubs ;

, Test Connection;

ASP- ADO.NET

289

.
( Connection) .

Pubs
SQL-, .

5. Next Choose a
Query Type.
6.

Use SQL statements Next.

7. Generate the SQL Statements


Query Builder.
8.

Tables Add Table titles,


Add, Close.
titles SQL-.

9.

Query Builder : title_id,


title, price, notes pubdate,
SQL- Data Adapter Configuration
Wizard (. 5.38).

. 5.38. Query Builder

290

Generate the SQL Statements SQL-.


10. Finish, DataAdapter
Configuration Wizard. Component Designer :

SqlDataAdapter1

SqlConnection1

sqlDataAdapter1
SqlConnectionl, Server Explorer. SQL Servers, ,
Pubs titles. titles,
Component Designer.



InitializeComponent

Component1.cs

initializeComponent component1.vb. , ,
Component1.cs component1.vb Solution
Explorer View Code.
. , Save All File.

5.5.3.
.
1.

Component Designer , component1.cs ( component1.vb) Solution Explorer.

2.

Data Generate DataSet.


Generate DataSet.

3. New. myDataSet (. 5.39).


4.

, Add this dataset to the designer .

myDataSet.xsd Solution Explorer.


XML- ,
myDataSet, myDataSet.xsd Solution Explorer.
, DataSet XML
XML Designer (. 5.40).

ASP- ADO.NET

291

. 5.39. Generate DataSet

. 5.40. XML Designer

5.5.4. DataGrid

Web-,
DataGrid.
.
1.

Solution Explorer WebForm1.aspx


Web-.

2.

View Toolbox.

292
3.

5
DataSet Data Toolbox Web.

4.

Add Dataset.

5.

Typed dataset (
) .

6. MyWebForm.myDataSet
(. 5.41).

. 5.41. Add Dataset



myDataSet1 Web-.

DataSet .
1.

Solution Explorer
Component1.cs ( Component1.vb)
View Code.

2.

component1 ( 5.8).

5.8

' Visual Basic


Public Class Component1
Inherits System.ComponentModel.Component

ASP- ADO.NET

293

'
Public Sub FillDataSet(ByVal dSet As myDataSet)
sqlDataAdapter1.Fill(dSet)

End Sub
'
End Class

// C#
public class Component1 : System.ComponentModel.Component
{
//
public void FillDataSet(myDataSet dSet)

{
sqlDataAdapter1.Fill(dSet) ;
}
//
}

3.

Web-,
WebForm1.aspx Solution
Explorer View Code.
, WebForm1.aspx.cs
( WebForm1.aspx.vb).

4.

webForm1, 5.9.

5.9
' Visual Basic
Public Class WebForm1
Inherits System.Web.UI.Page
'

...
Protected myComponent As New Component1()

// C#
public class WebForm1 : System.Web.UI.Page

294

//
protected Component1 myComponent=new Component1();

5.

Page_Load ( 5.10).

5.10
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'
If Not IsPostBack Then
MyComponent,FillDataSet(myDataSet1)
End If

'
End Sub

// C#
private void Page_Load (object sender, System.EventArgs e)
{

//
if (! IsPostBack)

{
myComponent.FillDataSet(myDataSet1);
}
//

, IF,
true, Web-.

DataGrid .
1.

Solution Explorer
WebForm1.aspx Web-.

2.

View Toolbox.

ASP- ADO.NET

295

3. Web- DataGrid
Web Forms Toolbox (. 5.42).

. 5.42. DataGrid

4. DataGrid
Property Builder .
DataGrid1 Properties. General
DataSource
myDataSet1,
DataMember titles, Data Key Field
title_id (. 5.43).

. 5.43. DataGrid

296

5.

Columns .

Create columns automatically at runtime.

Available Columns Button Column.

Button Column, Select,


, ,
(. 5.44).

. 5.44. Select DataGrid

" " Header Text.


Sort Expression Text Field title (. 5.45).

. 5.45. , SELECT

ASP- ADO.NET

297

price Available Columns, .


Header Text, "";
Data formatting expression
"{0:}" (. 5.46).

, 5.46. price

.
DataGrid title price
DataSet (. 5.47).
DataGrid,
Auto Format Properties
Professional 3 (. 5.48).
6. Web-.
DataGrid,
DataBind Page_Load.
( 5.11).

298

, 5.47. DataGrid

. 5.48. DataGrid

5.11
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myComponent.FillDataSet(myDataSet1)
DataGrid1.DataBind()

'

End If
End Sub
// C#

private void Page_Load{object sender.System.EventArgs e)


{

ASP- ADO.NET

299

if (!IsPostBack)
{

myComponent.FillDataSet(myDataSet1);
DataGrid1.DataBind(); //

}
:
1.

Build Build Solution;

2.

Debug Start Without Debugging.

Web .
(. 5.49). .

. 5.49. DataGrid

5.5.5. Web-
, ,
,
DataGrid. DataView.

300

DataView .
1. Web- .
2. View Toolbox.
3.

Web- DataView
Data Toolbox (. 5.50).

dataView1 Web.
4. dataView1 Properties,
Properties Window View.
5. Data Table. ,
titles , . 5.51.

, 5.50.
DataView Toolbox

. 5.51.
DataView
myDataSet1.titles Table.

Label,
DataView.
Label .
1. title-id, title, price pubdate, DataSet, Web- Label
Web Forms Toolbox.

ASP- ADO.NET

301

2. Label .

Properties Label,
Properties Window View.

DataBindings (. 5.52).

. 5.52. DataBindings Label


Labeln DataBindings ( n
Label).

Text Bindable Properties,


dataView1
(. 5.53).

. 5.53. Label1 DataBindings

302

Format. ,
Currency price
pubdate.

.
3. ,
, HTML Label.
HTML Label ,
" ", " ", " ",
"". Toolbox Web- Label HTML.
, ( ).
Web- . 5.54.

. 5.54. Web-

, dataView1 ( ) DataGrid.
.
1. DataGrid1.

DataGrid1_SelectedIndexChanged -

.
2. DataGrid1_SelectedIndexChanged ,
dataView1.RowFiter ,
( 5.12).

ASP- ADO.NET

303

5.12
' Visual Basic
MyComponent.FillDataSet(myDataSet1)
Dim index As Integer
Dim key As String
index=DataGrid1.SelectedIndex
key=DataGrid1.DataKeys(index).ToString()
dataView1.RowFilter=DataGrid1.DataKeyField

& "='" & key & "'"

// C#

myComponent.FillDataSet(myDataSet1);
int index=DataGrid1.Selectedlndex;
string key=DataGrid1,DataKeys[index].ToString();
dataView1.RowFilter=DataGrid1.DataKeyField+"='"+key+"'";

, WHERE SQL-
. index
DataGrid1. DataKeys,
DataGrid,
( DataGrid title_id
). key
DataGrid. , dataView1 (
).
3. Label , DataBind

DataGrid1_SelectedIndexChanged.

, ( 5.13).
5.13
' Visual Basic
Label1.DataBind()
Label2.DataBind()
Label3.DataBind()
Label4.DataBind()

// C#
Label1.DataBind();

304

Label2.DataBind();
Label3.DataBind();
Label4.DataBind();
4. .
DataGrid ( ),

(. 5.55).

. 5,55. Web-

5.5.6. -
-, Visual Basic #. , (20%).

- Visual Basic
Visual Basic .
1. Solution Explorer
MyWebForm.

2.

Add, Add Class.

Add New Item.

ASP- ADO.NET

305

3. ciass1.vb
MypaymentRules.vb Open (. 5.56).

. 5.56. Add New Item


MyPayment Rules.vb.

4. ,
MyPaymentRules.vb Solution Explorer ( 5.14).
5.14
' Visual Basic
Public Class MyPaymentRules
'
Dim price As Double
Public Function Calc(ByVal price)
Return price+0.2*price
End Function
'
End Class

306

- #
# .
1. Class View MyWebForm.
2.

Add, Add Class.


# Add Class Wizard.

3.
4.

MyPaymentRules Finish.

Class

View

MyPaymentRules.

5.

Add Add Method.


# Add Method Wizard.

6.

Method Name C a l c .

7.

Parameter type: double

Parameter name: price

Return type: double

8.

Add Finish.
,

5.15,

MyPaymentRules,
5.15

// #
public double Calc(double price)
{

return 0;

9.

return :

return price+0.2*price;

5.5.7. -
Web- Label,
.
- .

ASP- ADO.NET

1.

307

Web- Label Toolbox


WebForms ( Label5).
.

2. Text Label5 price DataView1.


,
.
3. Web- Label Toolbox
HTML Web- Label5. " "
4. WebForm1.aspx.cs ( WebForm1.aspx.vb)

DataGrid_SelectedIndexChanged ( 5.16).
5.16
' Visual Basic
'
Dim pr As New MyPaymentRules()
' Calc

Try
Dim price As decimal
price=myDataSet1.titles(index).price
Label5.Text=String.Format("{0:}",
pr.Calc(Convert.ToDouble(price)))
Catch
' price
Label1.Text=" "
Label5.Text=" "

End Try
// C#
//
MyPaymentRules pr=new MyPaymentRules();
// Calc

try

{
decimal price=myDataSet1.titles[index].price;
Label5.Text=String.Format("{0:C}",

308

pr.Calc(Convert.ToDouble(price)));
}
catch
{
// price

Label1.Text=" ";
Label5.Text=" ";

5.

, .

Grid,
,
(. 5.57).

. 5.57. Web-

Web-.
.


ASP-
ASP.NET
Web Form Page. , Web-.
:
Q Web-;
Q

DataGrid
;

Q
DataGrid;

Q , Web.

6.1.
Web-
, - ,
Web-
( , , . .). ,
, ,
Web- (validation controls). validation
Controls , -

310

,
.
.
Web-, .
, ,
.
, ,
.
,
ASP.NET , Web-.
:
Q Web-,
, ;
Q -
: , ;
Q .
, .

6.1.1.
.
1.

File New Project.

2.

New Project :

Project Types Visual Basic Projects


Visual C# Projects;
Templates ASP.NET Web Application;

Location URL , http://,


, ( IIS 5.0 .NET Framework http://localhost).
Web5.
Web-.
3.

. , Properties
pageLayout GridLayout.

ASP-

311

,
.

6.1.2.

, :
, , ( ).
.
1.

Web Forms Toolbox ,


. 6.1, ID.
6.1. Web-

ID (-)

TextBox

txtName

TextBox

txtBirthdate

TextBox

txtPassword

TextBox

txtPasswordAgain

Button

btnRegister
Text

2.

TextMode
Password , (. 6.1).

. 6.1. TextMode
TextBox

312

3.

, ,
Web- ,
Label Text.
,
.
4, , ,
. image, .
. 6.2.

. 6.2.

6.1.3.

, , . , .
. ,
.
Q . , , 4 .
Q

(name@domain.xxx).

ASP-

313

. , .

Q . ,
.

6.1.4.
, .
.
1.

Web Forms Toolbox


RequiredFieldValidator,
txtName.

2. ,
Properties ,
. 6.2,
6.2. Web-

ControlToValidate

TxtName

,
txtName

Text

,
,

ErrorMessage

3. , , .
4.

1-3 ,
RequiredFieldValidation; . 6.3.
6.3. Web-

RequiredFieldValidator2

C o n t r o l T o V a l i d a t e : txtBirthdate
Text: *
ErrorMessage:

314

6
6.3. ()

RequiredFieldValidator3

ControlToValidate : txtPassword
Text: *
ErrorMessage:

RequiredFieldValidator4

ControlToValidate:txtPasswordAgain
Text: *
ErrorMessage:

6.1.5.
( name@domain.xxx).

RegularExpressionValidator,
.
, , .
,
,
. ( Text),
, , Dynamic Display .
, , .
1. Web Forms Toolbox

RegularExpressionValidator

txtName.
2. , Properties
. 6.4.
6.4. Web-

ControlToValidate

TxtName

Text

ASP-

315

6,4. ()

ErrorMessage

name@domain.xxx

ValidationExpression

Internet E-mail Address


,
. . .
1.

Web Forms Toolbox


CompareValidator
txtBirthdate.

2.

,
Properties . 6.5.
6.5. Web-

ControlToValidate
Text
ErrorMessage
ValueToCompare

txtBirthdate
*

01/01/1900
( )

Operator
Type
Display

Greater Than
Date
Dynamic

6.1.6.
, , , .
1.

Web Forms Toolbox


CompareValidator
txtPasswordAgain.

316

2.

6_

,
Properties ,
. 6.6.
6.6. Web-

ControlToValidate

txtPasswordAgain

Text

ErrorMessage

ControlToCompare

txtPassword

Operator

Equal

Display

Dynamic

6.1.7.

, ,
, .
, .
,
ValidationSummary.
ErrorMessage
, .
, ValidationSummary .

.
1.

Web Forms Toolbox


ValidationSummary. ,
.

2.

, DisplayMode.

3.

Font ForeColor .

, .
, . 6.3.

ASP-

317_

. 6.3. Web-
, .

6.1.8.
Web- , .
1.

Solution Explorer
Build Browse.

2.

, ( ).
, .

3.

.
,

ValidationSummary (. 6.4).

318

, 6.4.


Web-.
.

,
, .
CompareValidator,
Operator LessThan, .
. :
<asp:CompareValidator

id=c1

ValueToCompare='<%# DateTme.Today.ToShortDateString() %>'


ControlToVaiidate="txtBirthDate"
Text="*" runat="server" />

Operator="LessThan"

Typs="Date+"

ASP-

319

DataBind .

, ,
.
R e g u l a r E x p r e s s i o n V a l i d a t o r .

( ) :
^[\w] {5, }

() , .
( ,
,
. .).

6.2.
DataGrid

, Web-
.

,
.
DataCommand, . . ,
, ,
. , D a t a G r i d . , D a t a G r i d ,
.
:
Q Northwind, SQL-;
ASP.NET- Web-.

320

:
Q Web-;
Q ;
Q DataGrid ;
Q
DataGrid;
Q DataGrid ;
Q
.

6.2.1.
Web- Web-.
Web- .
1.

File New Project.

2.

New Project :

Project Types Visual Basic Projects


Visual C# Projects;

Templates ASP.NET Web Application;


Location URL .
http://, .
Web- (1IS) 5.0 , .NET
Framework. Web- (IIS) ,
http://localhost.
Web6.
, Web- Web-,
Location. , Web-
WebForm1.aspx, Web Forms Designer.

6.2.2.
Web- . ,
. , .

ASP-

321

, .
.
1. , .
SQL-,
, .
SQL-, .
2. . Visual Studio , ,
; ,
.


DataConnection DataAdapter
, , .
.
1. Data Toolbox
SqlDataAdapter.

SQL-,
OleDbDataAdapter,

OLE
DB- .
Data Adapter Configuration Wizard.

2. :
Northwind SQL-;

SQL-. Windows
. Connection, SQL-.

, SQL-
;
SQL-:

SELECT CategoryID, CategoryName, Description FROM Categories

322

SQL-,
Query Builder,
(. 6.5).

. 6.5. Query Builder

Finish.
(SglConnection1
sqlConnection1). (SqlDataAdapter1
sqlDataAdapter1), , .
, ,
.

DataSet
Visual Studio , , .
DataSet, (xsd ). (, ).
DataSet .
1.

Data Generate DataSet.

ASP-

323

Generate DataSet ,
Web- ( ).

Generate Dataset.
2.

New dsCategories
(. 6.6).
, Choose which table(s) to add to the
dataset categories.
3. , Add this dataset to the designer
(. 6.6).

. 6.6. Generate Dataset


dsCategories , . dsCategories.xsd
Solution Explorer.

Solution Explorer Show All Files,


(vb cs-), , .

324

6
, Visual Studio
dsCategories1 Web-.

,
.

6.2.3. DataGrid

DataGrid,
,
. DataGrid
.
DataGrid .
1.
2.

Web-,
( ).
Web Forms Toolbox
DataGrid.

3. DataGrid, <F4> Properties Property Builder.


DataGrid1 Properties (. 6.7).
4.

General (. 6.7).
6.7. Web-

DataSource

DsCategories1

DataMember

Categories

Data key field

CategoryID

DataGrid
, DataGrid

DataGrid.

DataGrid1

5.


Properties.

6.

, , Font, BackColor , .

ASP-

325

. 6.7. DataGrid1 Properties

,
Auto Format Properties
.

Professional 6,

6.2.4.
DataGrid
DataGrid , , , Fill . ,
, DataGrid ,

326

,
.
DataGrid .
1. Web-, .
2.

Page_Load Fin , , ( 6.1).

6.1
' Visual Basic
SqlDataAdapter1.Fill(DsCategories1)
//C#

sqlDataAdapter1.Fill(dsCategories1) ;

3.

DataBind DataGrid

DataGrid1.DataBind. ,
(
, ).
:
Web-;
.
Web-
IsPostBack. Page_Load.

( 6.2).

6.2
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal a As
tem. EventArgs) Handles MyBase.Load
'
SqlDataAdapter1.Fill(DsCategories1)
If Not IsPostBack Then
DataGrid1.DataBind()

Sys-

ASP-

327

End If
End Sub
// C#

private void Page_Load(object sender, System.EventArgs e}


{

//
SqlDataAdapter1.Fill(dsCategories1);
if (!IsPostBack)
{

DataGrid1.DataBind();
}
}

. DataGrid, . .

6.2.5.

, ,
DataGrid. . Edit . , DataGrid
. . , Edit
(Update Cancel).
,
DataGrid. Edit,
Cancel Update , :
Q Edit ;
Q cancel
, , ;
Q

update ( ).

328

DataGrid
.
1. DataGrid, <F4>,
Properties. Properties Builder
.
DataGrid1 Properties.
Columns (. 6.8).

. 6.8. DataGrid

2.

Column list Available


columns Button Column.

3.

Edit, Update, Cancel >, Selected columns (. 6.9).

. 6.9. Edit, Cancel, Update DataGrid

ASP-

4.

329

Edit,
Cancel, Update , , (. 6.10).

. 6.10. Edit, Cancel, Update

5.

.
DataGrid (
). (
, . . 6.11).

. 6.11. Web-

, Edit, , .
EditItemIndex DataGrid , (
). ,
, 2, - -1. , DataGrid
.
,
Edit ( ).
. Item, DataGrid
. Item, ,
, Item.Itemindex,
, .

330

.
1.

Visual Basic:
Web- View Code, ;
DataGrid1;

EditCommand.

Visual C#:
DataGrid <F4>, Properties;
Events Properties;

DataGrid1_EditCoiranand).

EditCommand

DataGrid1_EditCommand.
2. 1 CancelCommand.
DataGrid1_CancelCommand.

3.

EditCommand ( 6.3).

6.3

' Visual Basic


DataGrid1.EditItemIndex=e.Item.ItemIndex

DataGrid1.DataBind()
// C#

DataGrid1.EditItemIndex=e.Item.ItemIndex;
DataGrid1.DataBind();

4.

CancelCommand ( 6.4).

6.4

' Visual Basic


DataGrid1.EditItemIndex=-l
DataGrid1.DataBind()

ASP-

331

// #
DataGrid1.EditltemIndex=-l;
DataGrid1.DataBind();

6.2.6.

DataGrid, , Edit .
, , .
.
1. Solution Explorer Web-
Build and Browse.
Web- .
, ,
(. 6.12).

. 6.12. Web- .

2.

.
:
: ;
TextBox,
(. 6.13).

332

. 6.13. DataGrid,

3.

.
.

. , .

6.2,7. DataSet

DataGrid
, ,
Update. .
DataGrid .
.
DataGrid ,
update. ,
UpdateCommand. . UpdateCommand .
Visual Basic:
Web- View Code, ;
DataGrid1;
UpdateCommand.

ASP-

333

Visual C#:
DataGrid <F4>, Properties;
Events Properties;
UpdateCommand
DataGrid1_UpdateCommand.

DataGrid1_UpdateCommand.

DataGrid

.
1. () DataGrid,
.
(datakey) , .
2.
DataGrid.
3. (datakey),
DataGrid. ,
.
4. . SQL- , .
5. DataGrid.
DataGrid
.
1. DataGrid.
,
ItemIndex Item, Edit
(.Item.ItemIndex). ,
(
DataKeys DataGrid). ( 6.5).
6.5
' Visual Basic
Dim key As String=DataGrid1.DataKeys(e,Item.ItemIndex).ToString()

334

// #
string key=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

2. DataGrid.
:

, (
) ( ). ,
Cells. ,
C e l l s ( 0 ) ;

controls;
controls (
TextBox,
). TextBox
DataGrid TextBox ( Dim, , Dim tab AS
TextBox);
.
6.6 .
6.6
' Visual Basic
Dim categoryName, categoryDescription As String
Dim tb As TextBox
tb=Type(e.Item.Cells(2).Controls(0), TextBox)

categoryName=tb.Text
tb=CType(e.Itera.Cells(3).Controls(0), TextBox)
categoryDescription=tb.Text
// C#

string categoryname;
string categoryDescription;
TextBox tb;

tb=(TextBox) e.Item.Cells[2].Controls[0];
categoryName=tb.Text;

tb=(TextBox) e.Item.Cells[3].Controls[0];
categoryDescription=tb.Text

ASP-

3.

335

. dsCategories FindBy.
FindByCategoryID,

.

,
FindByCategoryID ( 6.7).

6.7
' Visual Basic

Dim r As dsCategories.CategoriesRow
r=DsCategories1.Categories.FindByCategoryID(key)
// C#

dsCategories.CategoriesRow

r;

r=DsCategories1.Categories.FindByCategoryID(key);

4.

,
2 ( 6.8).

6.8
' Visual Basic
r.CategoryName=categoryName

r.Description=categoryDescription
// C#

r.CategoryName=categoryName;
r.Description=categoryDescription;

5.

,
Update ( 6.9).

6.9

' Visual Basic


SqlDataAdapter1.Update(DsCategories1)
// C#
SqlDataAdapter1.Update(DsCategories1);

336
6.

6
DataGrid ( 6.10).

6.10
' Visual Basic
DataGrid1.EditItemIndex-1

// C#
DataGrid1.EditItemIndex=-l;
7.

DataGrid ( 6.11).

6.11
' Visual Basic
DataGrid1.DataBind()
// C#

DataGrid1.DataBind();
UpdateCommand ( 6.12).
6.12
' Visual Basic
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.UpdateCommand
Dim CategoryName, categoryDescription As String
'
Dim key As String=DataGrid1.DataKeys(e.Item.ItemIndex).ToString
' TextBox
'
Dim tb As TextBox
tb=CType(e.Item.Cells(2).Controls(0),

TextBox)

categoryName=tb.Text
tb=CType(e.Item.Cells(3).Controls(0),

TextBox)

ASP-

337

categoryDescription=tb.Text
'
Dim r As dsCategories.CategoriesRow
r=DsCategories1.Categories.FindByCategoryID(key)
' DataSet
r.CategoryName=categoryName
r.Description=categoryDescription
'
SqlDataAdapter1.Update(DsCategories1)
' DataGrid
DataGrid1.EditItemIndex=-l
' DataGrid
DataGrid1.DataBind()
End Sub

// C#

private void DataGrid1_UpdateCommand(object source,


tem. Web.01.WebControls.DataGridCommandEveritArgs e)

Sys-

string CategoryName, categoryDescription;

//
string key=DataGrid1.DataKeys[e.Item.IteinIndex].ToString();

// TextBox (
)
TextBox tb;
tb=(TextBox)(e.Item.Cells[2].Controls[0]);
categoryName=tb.Text;

tb=(TextBox)(e.Item.Cells[3].Controls[0]);

338

categoryDescription=tb.Text;

//
dsCategories.CategoriesRow

r;

r=dsCategories1.Categories,FindByCategoryID(int.Parse(key));

// DataSet
.CategoryName=categoryName;
r.Description=categoryDescription;
//
SqlDataAdapter1.Update(dsCategories1);

// DataGrid
DataGrid1.EditItemIndex=-l;

// DataGrid
DataGrid1,DataBind();

6.2.8.
, .
.
1.

Solution Explorer Web-


Build and Browse.

2.

Web- .

3.

4. .
DataGrid ,
.
( SQL- ),
, , .

ASP-

339

6.2.9.
DataGrid
Web- , .

, . :
Q DataGrid;
Q DataGrid . .
,
.
Q

( Delete ).

, .


, ,
. . ,

.
DataGrid.
.
1.

Properties DataGrid.

2.

Property Builder Properties. DataGrid Properties.

3.

Columns (. 6.14.).

4.

Create columns automatically at run time.


, .

, >. (Selected columns) Header text
,
. .

.
(. 6.15).

340

. 6.14. DataGrid

. 6.15.

DataGrid
, , .
, , .
, ( , , -

ASP-

341

.), .
DataGrid . DataGrid
. .
DataGrid .
1. Properties DataGrid.
2.
3.

Property Builder Properties. DataGrid Properties.


Paging (. 6.16).

. 6.16. DataGrid

4. Allow paging. Page size , (


5 ). (, ). .
.
, 6.13.

342

6.13
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
Handles

DataGrid1.PageIndexChanged

DataGrid1.CurrentPageIndex=e.NewPageIndex
DataGrid1.DataBind()
End Sub

private void DataGrid1_PageIndexChanged (objectA


source,System.Web.UI.WebControls. A DataGridPageChangedEventArgs e)
{

DataGrid1.CurrentPageIndex=

e.NewPageIndex;

DataGrid1.DataBind();
}

. ,
DataGrid (. 6.17).

, . ,
. ,
, . DataGrid, 6.3.

6.2.10.
,
,
.
SQL- .

ASP-

343

.

" ".
: ( ) (
).
, , Page_Load ( 6,14).
6.14
' Visual Basic
If Not Page.IsPostBack Then
If Session("mydatset") Is Nothing Then
SqlDataAdapter!.Fill(dsCategories1)
Session("mydataset")=dsCategories1
Else
DsCategories1=CType(Session("mydatset"), dsCategories)

End If
DataGrid1.DataBind()

End If

// C#
if (! Page.IsPostBack)

{
if (Session["mydatset"]==null)

{
sqlDataAdapter1.Fill(dsCategories1);
Session["mydataset"]=dsCategories1;

}
else

{
dsCategories1=(dsCategories)Session["mydatset"];

}
DataGrid1.DataBind();

344

,
, .

,
(, 6.15).
6.15

' Visual Basic


SqlDataAdapter1.Update(DsCategories1)
Session("mydataset")=DsCategories1
// C#

SqlDataAdapter1.Update(dsCategories1);
Session["mydataset"]=dsCateqories1;

, ,
. .

6.3.
DataGrid
Web- , .
,
.

. , DataGrid .
.
Q

DataGrid. , ,
,
.
Q .
(, ). , .
.

ASP-

345

,
.
: , . .

, , .
:
Q

Northwind, SQL-;

Q ASP.NET- Web.
:
Q Web- Web-;
Q ;
Q DataGrid;
Q ,
, .

6.3.1.
Web- Web-, .
1.

File New Project.

2.

New Project :

Project Types Visual Basic Projects


Visual C# Projects;

Templates ASP.NET Web Application;

Location .
http://, .
Web- (IIS) 5.0 , .NET
Framework. Web- (IIS) ,
http://localhost.
W e b 7 .
, Web-, Web-, .
, Web- WebForm1.aspx,
Web Forms Designer.

346

6.3.2.

SQL- .
10 , ,
WHERE:
Q 10 , ,
;
Q 10 , .


WHERE. ,
.

Customers
Northwind SQL-.
, :
();
Q

Q SQL, ,
( ).

Connection
.
1.

Data

Toolbox

SqlConnection.

SqlConnection, SQL- 7.0 . ,


OleDbConnection, .

2.

Properties
ConnectionString.

ASP-


New Connection.
3.

347

Northwind SQL, New Connection.


, 5.
Data Link Properties.

4.

SQL-, Northwind .

SQL-,
. Windows - .

Connection,
SQL-.


ConnectionString Connection

.
, SQL- ( ).

:

Data

Toolbox

SqlCommand.

cmdNext.

Properties
Connection. ,
, Existing.

CommandText Query Builder.


5.


(. 6.18):

Query

Builder

SELECT TOP 10 CustomerID, CompanyName, City


FROM Customers
WHERE

(CustomerID > @customerid)

ORDER BY CustomerID

SQL-

348

. 6.18. Query Builder

6.

Query Builder.
SQL-

CommandText.

7.

3 5
cmdPrevious, :

SELECT TOP 10 CustomerID, CompanyName, City


FROM Customers

WHERE (CustomerID >= @customerid)


ORDER BY CustomerID

, .

6.3.3. DataGrid

DataGrid. .
DataGrid .
1. Web Forms Toolbox
DataGrid.

ASP-

349

1.

Properties AutoFormat
Prosessional 6.

3.

Properties Property Builder.


DataGrid1 Properties (. 6.19).

. 6.19. DataGrid
DataGrid1 Properties

4. Paging ,
. 6.8.
6.8. Web-

AllowPaging

Checked

Allow custom paging

Checked

Page size

10

Show navigation buttons

Unchecked

5. OK .

350

DataGrid DataSource
.
.
1.

Web Forms Toolbox


LinkButton.

2.

btnPrevious btnNext.

3.

, , , (. 6.20).

. 6.20.
DataGrid

6.3.4.

.
, SQL- ( ). 10 , SqlDataReader.
DataGrid, . 10 .

ASP-

351

10 ,
, DataGrid.

cmdNext.
10 .
10 , , , . cmdPrevious.
Web-, Web-, .
- ,
.
ViewState, (
). :
Q , ; ,
( );
Q , ,
.
ViewState . , ,
, :
CurrentPage=3
0=ALFKI
1=BSBEV
2=FAMIA
, 3, :
ALFKI, BSBEV, FAMIA ( 0).
,
.

DataGrid
DataGrid , . ,
cmdNext cmdPrevious.

352

DataGrid .
1. private CurrentPage . ( 6.16).
6.16
' Visual Basic
Public Class WebForm1
Inherits Systern.Web.UI.Page
Private CurrentPage As Integer
//C#

public class WebForm6 : System.Web.UI.Page


{

private int CurrentPage;


}

2. FillGrid,
SqlCommand.
3. DataReader.
4.
ExecuteReader 10 .
5. DataSource
DataGrid DataReader
DataBind DataGrid.
6. DataReader Connection.

7.

ViewState:
CurrentPage ( );
DataGrid.
ViewState,
.
( 6.17).

6.17
' Visual Basic
ViewState(CurrentPage.ToString)=DataGrid1.Items(0).Cells(0).Text

// C#
ViewState[CurrentPage.ToString()]=DataGrid1.Items[0].Cells[0].Text;

S-

8.

353

, , . , ,
, (10). , .
6.18 FillGrid.

6.18

' Visual Basic


Private Sub FillGrid(ByVal currentSqlCommand as SqlClient.SqlCommand)
Dim dr As SqlClient.SqlDataReader

SqlConnection1.Open()
dr=currentSqlCommand.ExecuteReader()
DataGrid1.DataSource=dr
DataGrid1.DataBind()

dr.Close()
SqlConnection1.Close()
ViewState("CurrentPage")=CurrentPage

ViewState(CurrentPage.ToString)=DataGrid1.Items(0).Cells(0).Text
If DataGrid1.Items.Count<DataGrid1.PageSize Then
btnNext.Enabled=False
End If
End Sub
//C#
private void FillGrid(System.Data.SqlClient.SqlCommand currentSqlCommand)
{
System.Data.SqlClient.SqlDataReader dr;
SqlConnection1.Open();
dr=currentSqlCommand.ExecuteReader();
DataGrid1.DataSource=dr;
DataGrid1.DataBind();
dr.Close();
SqlConnection1.Close();
ViewState["CurrentPage"]=CurrentPage;
ViewState[CurrentPage.ToString()]=DataGrid1.Items[0].Cells[0].Text;
if (DataGrid1.Items.Count<DataGrid1.PageSize)
{
btnNext,Enabled=false;
}
}

354


Web- DataGrid . , DataGrid, , , .
cmdNext. @customerid.
, , (" ") 10 .
CurrentPage
, .
( 6.19).
6.19
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load

Sys-

'
If Not Page.IsPostBack Then
cmdNext.Parameters("@customerid").Value=""
CurrentPage=0
FillGrid(cmdNext)

End If
End Sub
//C#
private void Page_Load(object sender, System.EventArgs e)

{
//
if

{!Page.IsPostBack)

{
cmdNext.Parameters["@customerid"].Value="";
CurrentPage=0;
FillGrid(cmdNext);

}
}


,
cmdNext,

ASP-

355

FillGrid. DataGrid.
, FillGrid
ViewState . ,
ViewState. ,
ViewState .

( 6.20).
6.20
' Visual Basic
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles btnNext.Click

Sys-

'

CurrentPage=CType(ViewState("CurrentPage"),

Integer)

CurrentPage += 1
'
Dim lastid As String=DataGrid1.Items(9).Cells(0).Text
cmdNext.Parameters("@customerid").Value=lastid
FillGrid(cmdNext)

End Sub
//C#

private void btnNext_Click(object sender, System.EventArgs e)


{
//
CurrentPage=(int)(ViewState["CurrentPage"]);
CurrentPage++;
//
string lastid=DataGrid1.Items[9].Cells[0].Text;

cmdNext.Parameters["@customerid"].Value=lastid;
FillGrid(cmdNext);
}


, , . -

356

. ViewState, ,
.
cmdPrevious,
FillGrid.
, ,
. ,
, ( 6.21).
6.21
' Visual Basic
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrevious.Click
btnNext.Enabled=True
CurrentPage=CType(ViewState("CurrentPage"),

Integer)

CurrentPage -= 1
If CurrentPage >= 0 Then
Dim firstid As String
firstid=CType(ViewState(CurrentPage.ToString), String)
cmdPrevious.Parameters("@customerid").Value=firstid

FillGrid(cmdPrevious)

End If
End Sub
//C#
private void btnPrevious_Click(object sender, System.EventArgs e)

{
btnNext.Enabled=true;
CurrentPage=(int)(ViewState["CurrentPage"]);
CurrentPage-;
if (CurrentPage >= 0)

{
string firstid;
firstid=(string)(ViewState[CurrentPage.ToString()]);
cmdPrevious.Parameters["@customerid"].Value=firstid;

FillGrid(cmdPrevious);

ASP-

357

6.3.5.
, .
Web- .
1.

Web-.

2.

Solution Explorer Web-


Build and Browse.

3. , DataGrid 10 .
4.


(. 6.21).

. 6.21.

6.4. ,
Web-
, . ,
.

368


DataGrid. Web-,
.
,
.
,
Northwind, MS SQL Server. :
Q Web- , , Web-;
Q connection;
Q

DataAdapter;

DataSet;

Q DataGrid DataSet;
Q Button;
Q DataSet;
Q ,
.

6.4.1.
.
1.

File New Project.

2.

Project Types Visual Basic Projects


Visual C# Projects, ASP.NET Web
Application.

3.

Location URL .
http://, .
Web- (IIS) 5.0 , .NET
Framework. Web- (IIS) ,
http://localhost

4.

- , Close
Solution, .
,
. , WebAddDel.

5.

, .

Visual Studio Web- .

ASP-

359

6.4.2.


DataAdapter,
SQL-,
DataSet,
DataSetll.
, .

DataGrid, , .
.
1.


WebForm1, .

2.

Web Forms Toolbox, DataGrid


.

3.

<F4> Properties.

4.

DataSource DataSet11 .

5.

DataMember Categories.

6.

DataKeyField CategoryID.

, DataGrid
categories DataSetll.
DataGrid .
7.

Windows Forms Toolbox


Button Text .

8.

DataGrid ,
, Properties
Auto Format
(, Professional 2).
. 6.22.

9. DataGrid ( ). DataGrid, Property


Property Builder, DataGrid1
Properties.

360

. 6.22.

10. Columns
Available columns Button Column.
11. Delete >
Selected Columns.
12. Text , Button
PushButton (. 6.23).

. 6.23. DataGrid

13. Edit, Update, Cancel


> Selected
Columns.

ASP-

361

14. Edit Text , Cancel Text , Update Text , Button Type PushButton .
DataGrid , (. 6.24).

. 6.24. DataGrid

6.4.3.
.
, DataSet ( 6.22).
6.22
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
If Not IsPostBack Then
DataGrid1.DataBind()
End If
End Sub

Sys-

362
//C#

private void Form1_Load(object sender, System,EventArgs e)


{

SqlDataAdapter1.Fill(dataSet11);
}

Fill .
D a t a G r i d .


.
SqlInsertCommand1 .
SqlDataAdapter1,
InsertCommand. , +, CommandText. Query Builder
SQL- (. 6.25).

. 6,25. InsertCommand


Categories, SQL @CategoryName @Description.
.
6.23.
6.23
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles Button1.Click
'

Sys-

ASP-

363

SqlDataAdapter1,InsertCommand.Parameters("@CategoryName").Value=""
SqlDataAdapter1.InsertCommand.Parameters("@Description").Value=""
SqlConnection1.Open()
SqlDataAdapter1.InsertCommand.ExecuteNonQuery()
SqlConnection1.Close()
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()

End Sub
//C#
private void button1_Click(object sender, System.EventArgs e)

{
//
sqlDataAdapter1.InsertCommand.Parameters["@CategoryName"].Value="";
sqlDataAdapter1.InsertCommand.Parameters["@Description"].Value="";
SqlConnection1.Open();
sqlDataAdapter1.InsertCommand.ExecuteNonQuery();
SqlConnection1.Close();
sqlDataAdapter1.Fill(dataSet11);
DataGrid1.DataBind();

@CategoryName
@Description InsertCommand SqlDataAdapter1

( ). , InsertCommand ( ) . DataSetll
, Fill .

DataGrid.
sqlDeleteCommand1 . SqlDataAdapter1,
DeleteCommand. , +, CommandText . Query Builder
SQL-. ,
. 6.26:
DELETE FROM Categories WHERE (CategoryID=@Original_CategoryID)

364

. 6.26. DeleteCommand

, , ,
Categories, @Original_CategoryID
SQL-. CategoryID
DataGrid ( ,
). .
VB

DataGrid, DeleteCommand.
DataGrid1_DeleteCoinmand.

#
DataGrid
Properties, Events .
DeleteCommand DataGrid1_DeleteCommand.
DataGrid1_DeleteCommand, ( 6.24).
6.24
' Visual Basic
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As
Systern.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand

ASP-

355

'
Dim CategoryID As Int16
Dim index As Integer
index=e.Item.ItemIndex
CategoryID=CType(DataGrid1.Items(index).Cells(2).Text, Int32)
SqlDataAdapter1.DeleteCommand.Parameters("@Original_CategoryID")
.Value=CategoryID
SqlConnection1.Open()
SqlDataAdapter1.DeleteCommand.ExecuteNonQuery()
SqlConnection1.Close()
DataSet11.Clear(}
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
End Sub
//C#

private void DataGrid1_DeleteCommand(object source,


tem.Web.Ul.WebControls.DataGridCommandEventArgs e)

Sys-

int index ;
int CategoryID;
index=e.Item.ItemIndex;
CategoryID=dataSet11.Categories[index].CategoryID;
SqlDataAdapter1.DeleteCommand.Parameters["@Original_CategoryID"]
.Value=CategoryID;
SqlConnection1.Open();
SqlDataAdapter1.DeleteCommand.ExecuteNonQuery();
SqlConnection1.Close();
dataSet11.Clear();
SqlDataAdapter1.Fill(dataSet11);
DataGrid1.DataBind();
}

: index DataGrid,
CategoryID ( ).
index DataGrid,
CategoryID . @Original_CategoryID DeleteCommand SqlDataAdapter1 CategoryID.

366

DeleteCommand ( )
. DataSetll , Fill

DataGrid.
DataGrid.
, DataGrid,
, .
, ( ).
.
Item,
DataGrid . Item , , Item. ItemIndex,
, .

.
1.

DataGrid1_EditCommand,
Visual Basic:
Web- View Code, ;
DataGrid1;
EditCommand.
Visual #:
DataGrid <F4>, Properties;
Events Properties;
EditCommand .
DataGrid1_EditCommand.

2. 1 CancelCommand (
DataGrid1_CancelCommand).

3. 1 UpdateCommand (
DataGrid1_ UpdateCommand).

ASP-

367

EditCommand ( 6.25).
6.25

' Visual Basic


Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.Editltemlndex=a.Item.ItemIndex
DataGrid1.DataBind()
End Sub
// C#
private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Itern.ItemIndex;
DataGrid1.DataBind();
}

CancelCommand ( 6.26).
6.26

' Visual Basic


Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
DataGrid1.Editltemlndex=-1
DataGrid1.DataBind()
End Sub
// C#
private void DataGrid1_CancelCommand(object source,
tem.Web.UI.WebControls.DataGridCommandEventArgs e)
{

Sys-

DataGrid1.Editltemlndex=-1;
DataGrid1.DataBind();
}

UpdateCommand ( 6.27).
.

368

6.27
' Visual Basic
Private Sub DataGrid1_UpdateCommand (ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Grid1,UpdateCommand

Handles Data-

Dim categoryName, categoryDescription As String


'
Dim key As String=DataGrid1.DataKeys(e.Item.ItemIndex).ToString
' TextBox ( . )
Dim tb As TextBox
tb=CType(e.Itein.Cells(3).Controls(0), TextBox)
categoryName=tb.Text
tb=CType(e.Item.Cells(4).Controls(0), TextBox)
categoryDescription=tb.Text
'
Dim r As DataSet1.CategoriesRow
r=DataSet11.Categories.FindByCategoryID(key)
' DataSet
r.CategoryName=categoryName
r.Description=categoryDescription
'
SqlDataAdapter1.Update(DataSet11)
' DataGrid
DataGrid1.Editltemlndex=-1
' DataGrid
DataGrid1.DataBind()

End Sub
// C#
private void DataGrid1_UpdateCommand (object source,
tem. Web.UI.WebControls.DataGridCommandEventArgs e)

Sys-

{
string categoryName, categoryDescription;
//
string key=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
// TextBox
// )
TextBox tb;

ASP-

369

tb=(TextBox)(e.Item.Cells[3].Controls[0]);
categoryName=tb.Text;
tb=(TextBox)(e.Item.Cells[4].Controls[0]);
categoryDescription=tb.Text;
//
DataSet1.CategoriesRow r;
r=dataSet11.Categories.FindByCategoryID(int.Parse(key));
// DataSet
r. CategoryName=categoryName;
r.Description=categoryDescription;
//
SqlDataAdapter1.Update(dataSet11);
// DataGrid
DataGrid1.EditItemIndex=-1;
// DataGrid
DataGrid1.DataBind();
}


( , , ). . 6.27 , .

. 6.27. DataGrid

, Widows Web-. .NET


Web-. .

Web-
Web- . XML, HTML Web- , , . ,
Web- Visual Studio .NET
-. ,
Web- :
Q Web-;
Q

Web- .

7.1. Web-
Microsoft .NET , Web- "" . Web- , ( ) () -.
- Web- XML HTTP. XML
HTTP , Web-
.
HTTP- Web- ,
.
, . Web- ,
. , Web- -

Web-

371

, ,
.
Web- , .
, Web- .

7.2. Web-
Web-.
Web-, .
, .
:
Q Web- - ASP.NET Web Service;
Q XML-;
Q Web- ;
Q

Web- Web-.

7.2.1, XML Web Service


XML Web Service .
1.

File New Project.

1.

New Project Visual Basic Projects the Visual


C# Projects.

3.
4.

ASP.NET Web Service.


Web-, XML
Web- . ,
http://MyServer/TempConvert1. , Web
http://localhost (. 7.1).

Web- Web ( ).
. Web-

372

7
( ).
.

. 7.1. Web-

5.

Visual Studio Web-, Web Service1.asmx (. 7.2).

. 7.2. Web- Solution Explorer

7.2.2.
Web-
, Web-. Web-

Web-

373

, Visual Studio, ,
, asmx-.
.
1. Component Designer, .

Solution Explorer (
). Show All Files
Solution Explorer, Project Show All Files.

, Web (. 7.3).

. 7.3. Web-

2. Web-
convert ( 7.1).
7.1
Visual Basic
<WebMethod(Description:=" ")>
Public Function Convert(ByVal Summa As Double,

374

ByVal Kurs As Double) As String


Convert=Summa * Kurs
End Function
// C#

[WebMethod(Description=" ")]
public double Convert(double Summa, double Kurs)
{

return Summa * Kurs;


}

WebMethod Public- convert.


Web-, -, . convert Web-. Description , ,
,
.
3.

VB, Service1.asmx Solution


Explorer. Project Web Project
Set as Start Page.
#,
Service1.asmx Solution Explorer
Set as Start Page.

4.

7.2.3. Web-
Web-.
Service1.asmx Solution
Explorer Build and
Browse. (. 7.4). Web-
Service1.asmx WebMethod ( ) .
Convert. , , Web- (. 7.5).

: (Summa) (Kurs).
Invoke (. 7.6).

Web-

375

. 7.4. Web- Web-

. 7.5. Web- Convert

. 7.6. Web- Convert

, , XML- (. 7.7).

376

, 7.7. Web- Convert XML-

Web- . ,
-, .

Web- . , Web-
,
XML.

7.2.4. Web-
Visual Studio
,
Web-
.
1.


(. 7.8).

. 7.8. Web-

1.

Debug Start.
Web- . Visual
Studio Web-
, asmx-.
(. 7.9).
, Web-

.

Web-

377

. 7.9.

3.

Web- ,
Debug Stop Debugging.

Web- <F5>, <SHIFT>+<F5>.

7.2.5. Web-
Web- , Web-,
. Web :
(Web Setup project)
.
Web- Web Setup project .
1.

File Add Project New Project.

2.

Setup and Deployment Projects Web Setup Project.

3.

Name TempConvert1WebSetup
(. 7.10).

378

. 7.10. Web-

.
4.

File System Editor Web Application


Folder.

5.

Solution Explorer vert1WebSetup, Add Project Output


(. 7.11).

6.

Add Project Output Group Content Files,


Primary output Debug Symbols:

Content Files Web-:


TempConvert1.asmx, Global.asax, Web.config. ,
Content Files vsdisco-;

Primary output TempConvert1.dll


;

Debug Symbols TempConvert1.pdb.

7.

8.

Solution Explorer
TempConvert1WebSetup Build.

9.


(. 7.12).

Web-

379

. 7.11.

. 7.12. Web-

Web- Setup Web-.

380

Web-
.
1.

Solution Explorer TempConvert1 (. 7.13).

. 7.13. Web- Solution Explorer

2.

Project Copy Project.

3.

Destination project folder , .

4.

FrontPage File share,


(Web access method).

5.

Only files needed to run this application.


vsdisco-.

6.

OK (. 7.14).

. 7.14. Wab-

Web-

381

7.3. Web-

Web- .
:
-, Web-;
Q Web- Web-;
Q Web-;
.
Q Web.

7.3.1. Web-
Web-,
Web- TempConvert1.

Web-, TempConvert1,
, Web-.

(Web-) .
1. File New Project
New Project.
2. Visual Basic
Projects Visual C# Projects.
3. ASP.NET Web Application.
4.

Web-, Web TempConvertClient1


http://MyServer/TempConvertClient1. Web http://localhost.

5. (. 7.15).
6.

Web Forms Toolbox


TextBox, Label Button. , . 7.16., Text . 7.1.

382

. 7.15. Web-

. 7.16. Web-
7.1. Text

Label1

(.)

Label2

Label3

Label4

()

TextBox1

()

TextBox2

()

Button1

Web-

383


(TextBox1) (TextBox2), L a b e l 4
, Web-.
Button1 Web-.

7.3.2. Web-
Web- , - Web- . ,
Web- .
- Web-.
Web- .
Web- Web- .
1.

Project Add Web Reference.

2.

Address Add Web Reference ,


Web-, , , http: // <servername>/TempConvert1/Service1.asmx.
Web- , http://localhost/TempConvert1/Service1.asmx
<ENTER> (. 7.17).

. 7.17. Web- Web- -

3.

Add Reference, Web- Web-.


Visual Studio , Web-.

384

4.

WebForm1,
.

7.3.3. Web-
, Web-,
Web- (proxy class).
Web- ,
.
Web-, , TextBox1 TextBox2, Convert Web-.
, Web-,
Label4.

Web- ( 7.2).
7.2
' Visual Basic
Private Sub Button1_Click(ByVal sender As Object,
ByVal e as EventArgs) Handles Button1.Click
Dim ws As New TempConvertClient1.localhost.Service1()
Dim Sum As Double
Dim Kur As Double
Sum=Convert.ToDouble(TextBox1.Text)
Kur=Convert.ToDouble(TextBox2.Text)
Labe14.Text=ws.Convert(Sum, Kur)

End Sub
// C#
protected void Button1_Click (System.Object sender, System.EventArgs e)

{
TempConvertClient1.localhost,Service1 ws=new
TempConvertClient1.localhost.Service1 ();
double Sum=Convert.ToDouble(TextBox1.Text);
double Kur=Convert.ToDouble(TextBox2.Text);
Label4.Text=ws.Convert(Sum, Kur);
}

Web -

385

Web-, Web-, Service1.

, -, Web-. .
.
1.

webForm1 Solution Explorer.

2.

Project Web Project Set as Start Page.


WebForm1.aspx Solution Explorer,
Set as Start Page.

3.

4. , Start,
(. 7.18).

. 7.18. Web-, -

7.4.
Web-

Web-, .
Web- PROMT. .
, -

386

-, .
,
, , .

.
, .

1. , . . 7.19, Text . 7,2.

. 7.19.
7.2, Text

Label5

Label6

Label7

()

TextBox3

()

Button2

2.

Project Add Web Reference.

3.

Address Add Web Reference URL,


Web-, :
http://www.gotdotnet.ru/webservices/promt/translate/simplesvc.asmx.

4.
Button2. ,
translator. Web-

Web-

387

TranslateText, 3 . ( ), ( , 1
). 0. TextBox3.
7.3.
7.3
' Visual Basic
Private Sub Button2_Click(ByVal sender As Object,
ByVal e EventArgs) Handles Button1.Click
Dim translator As New
ru.gotdotnet.www.SimpleTranslateServise()
Label7.Text=translator.TranslateText(TextBox3.Text, 0, 0)
End Sub
// C#

protected void Button2_Click (System.Object sender, System.EventArgs e)


{

ru.gotdotnet.www.SimpleTranslateServise

translator=new

ru.gotdotnet.www.SimpleTranslateServise();
Label7.Text=translator.TranslateText(TextBox3.Text,

0, 0);

5.

. TextBox3
.
( )
.

,
, ,
, . .
Web-, .
.
.


Web-
Web-
,
, - ,
,
.
- ( , , . .).
,
Web-. :
Q ;
Q -
Web-;
Q Web- Windows-;
Q Web-
Web-.

8.1.
, Windows- - (
"" ).

Web-

389

. : , , . (
SQL-), , ,
, . . - ,
. - Web-.
, ,
(, ). . 8.1. .

. 8.1.

,
( ). Web-, , .
-,
, Web-. Windows-
Web-. -

390

authors Pubs,
SQL-. Web- Web-,
HTML 3.2 .
Windows- Windows-.
-
Web-, authors
Pubs. ,
, Web-
. Web- -
HTTP XML.
:
Q Pubs SQL- ;
Q Visual Studio .NET.

8.2.


.
, - ( ) ,
, . (
). - (XML
Web-), (Web Windows-).
.
1.

2.

- ( ):
ASP.NET Web Service;

;
-
.

:
Windows-;
Web-.
3. .

Web-

391

8.3. -

Web-
-, , Web-.
, XML Web-, , -,
- . Web- ,
-, , .
Web- . , GetAuthors,
-. ,
UpdateAuthors, , -. .
DataSet, DataAdapter, Connection - DataCommand
. . 8.2 Web-.

. 8.2. Web-

392

8.3.1. Web-
, Web-, -.
Web- .
1.

File New
Project New Project.

2.

, , Visual Basic Projects Visual C# Projects Project Types, ASP.NET Web Service Templates.

3.

Location Web-
http://ServerName/AuthorsWebService
(. 8.3).

. 8.. Web-

Web- ,
localhost. Visual Studio ASP.NET Web-,
Web Access Failed (
IIS ).

AuthorsWebService
Service1.asmx, .

393

Web-

4.

Solution Explorer
Service1.asrnx, .

5.

Properties Name
Service1 AuthorsService (. 8.4).

6.

Solution Explorer
Service1.asmx, Rename
AuthorsService.asmx (. 8.5).

. 8.4. Web Properties


. 8.5,
Web-

Web-

SqlDataAdapter SqlConnection. SqlConnection -

, SqiDataAdapter
DataSet.
,
.
.

8.3.2.


.
1.

Server Explorer.

Server Explorer . , View Server Explorer.

Pubs,
2-6 .

394

2.

Server Explorer Data


Connections Add Connection.
- Data Link Properties (. 8.6).

. 8.6.
3.

(Connection)
(Data Link Properties), SQL-,
Pubs. SQL- , local.

4.

Windows NT (Use Windows NT Integrated security).

Windows NT
Integrated security, .
5.
6.

Pubs.
Test Connection

Connection.

395

Web-

Server Explorer Data Connections,


Pubs (. 8.7).

,
.

7.

Server Explorer
connection Tables.

8. authors ,
.
9. <CTRL>+<Click>, au_id,
au_lname, au_fname city (. 8.8).

. 8.7.

Server Explorer

. 8.8.

Server Explorer

10. Server Explorer . SqlConnection1


SqlDataAdapter1 (. 8.9).

. 8.9. SqIConnection1 SqlDataAdapter1


Web-

396

,
, ,
authors Pubs.
(Integrated Security).
(anonymous access)
(impersonation).
.
, .

8.3.3.

,
, Internet
Information Services.
1. Internet Information Services.
(Administrative Tools) Windows
( .
Windows).
2. Web-.
3. (Default Web Site) (. 8.10).

. 8.10.

Web-

397

4.

AuthorsWebService Properties.

5.

(Directory Security).

6.

(Edit) (Anonymous access and authentication control),


. 8.11.

. 8.11.
7.

(Anonymous Access).

8.


(Integrated Windows authentication), . 8.12.

Windows

. 8.12.

398

9.

Visual Studio,
Web.config Solution Explorer.

10. <system.web>, ,
Web.
<identity impersonate="true"/>

8.3.4.
DataSet
Web- DataSet,

-.
DataSet .
1,

Solution Explorer AuthorsService.asmx, .


2. Data Generate DataSet. Generate Dataset New
authors1. He Add this dataset to the
designer, (. 8.13).

. 8.13. DataSet

Web-

399

authors1.xsd.
authors1, DataSet authors.

3. , File Save All.


, ,
. Web-, .. .
.

8.3.5.


,
Windows-, , Web. :
GetAuthors,
,
UpdateAuthors.

Web- .
1. Solution Explorer AuthorsService,
.
2. View Code .
3. GetAuthors,
.
authors (
authors1) authors SqiDataAdapter. -.
8.1.
8.1
' Visual Basic
<WebMethod> Public Function GetAuthors() As authors1
Dim authors As New authors1()

400

SqlDataAdapter1.Fill(authors)
Return authors
End Function
// C#

[WebMethod]
public authors1 GetAuthors()
{
authors1 authors=new authors1();

SqlDataAdapter1.Fill(authors) ;
return authors;
}

4.

UpdateAuthors, , -,
.
author Changes,
authors1. authorChanges authors1,
SqlDataAdapter.Update . , authorChanges
. -
,
authors1. 8.2.

8.2
' Visual Basic
<WebMethod> Public Function UpdateAuthors(ByVal authorChanges
As authors1) As authors1
If Not (authorChanges Is Nothing) Then
SqlDataAdapter1.Update(authorChanges)
Return authorChanges
Else
Return Nothing
End If

End Function
// C#

[WebMethod]

Web-

401

public authors1 UpdateAuthors(authors1 authorChanges)

{
if (authorChanges != null)
{
SqlDataAdapter1.Update(authorChanges);

return authorChanges;
}
else
{
return null;
}
}

5. File Save All.


6. Build Build Solution.
, - . - , SQL-, GetAuthors UpdateAuthors,
-.
GetAuthors,
AuthorsService.asmx Build and Browse. Web- (. 8.14).

. 8.14. Web-

402

GetAuthors.
(. 8.15).

. 8.15. GetAuthors

Invoke Web-
XML- ( . 8.16).

. 8.16. XML

Web-

403

XML- -,
XML- , ,
. .

8.4.
Web-
: Windows- Web- . Web-
, . .

8.5. Web-
Windows-
Windows-

. , Windows- , Web-.
, Web,
. , Windows-
,
(,
). Windows-
8.17.
Windows- Windows Web- AuthorsWebService.
DataGrid. .
GetAuthors Web-.
DataGrid ,
. .
UpdateAuthors Web-,
.

404

. 8.17. Windows-

8.5.1. Windows-
- ,
, , .
Web-.
Web- .
Windows-, Web-, .
1.

File Add Project New Project


Add New Project.

2. , , Project Types Visual C# Projects Visual


Basic Projects, Templates Windows Application.
3.

A u t h o r s W i n C l i e n t ,
(. 8.18).

Web-

405

. 8.18, Windows-

AuthorsWinClient, Windows-
Form1, .
4. Web-, :
Solution Explorer
AuthorsWinClient Add Web
Reference;
Web-
Address Add Web Reference:
http://ServerName/AuthorsWebService/AuthorsService.asmx.
Web- ,

:
http://localhost/AuthorsWebService/AuthorsService.asmx.
<ENTER> (. 8.19).

Add Reference.

Web- authors1 .
,
, Web.

406

. 8.19. Web- Web-

8.5.2.
:
Q , ;
Q DataGrid ;
Q DataSet
.

.
1.

Windows Forms Toolbox


DataGrid.

2.

Windows Forms Toolbox


Button. Name
LoadData Text .

3.

Windows Forms Toolbox


Button. Name
SaveData Text .

4.

Data Toolbox DataSet.


Add DataSet Typed dataset Name
AuthorsWinClient.ServerName.authors1.

DataSet, authors1
(. 8.20).

Web-

407

. 8.20. DataSet - Web-

5. Dataset Name AuthorData (. 8.21).

. 8.21. DataSet

6. DataGrid. DataSource

AuthorData. DataMember
, authors
(. 8.22).
7. DataGrid
authors. . 8.23.

408

. 8.22. DataGrid

. 8.23.

,
. ,
.

8.5.3.


. .
1.

View Designer.
LoadData, click. Web, . ,
, GetAuthors.
Web- AuthorData, -

Web-

409

DataGrid. Credentials
Web- , Web,
. , 8.2, .

Web- , localhost Web.

8.2
' Visual Basic
Private Sub LoadData__Click (ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles LoadData.Click
Dim ws As New AuthorsWinClient.localhost.AuthorsService()
ws.Credentials=System.Net.CredentialCache.DefaultCreclentials
AuthorData.Merge(ws.GetAuthors())
End Sub
// C#

private void LoadData_Click(object sender, System.EventArgs e)


AuthorsWinClient.localhost.AuthorsService ws =
new AuthorsWinClient.localhost.AuthorsService();
ws.Credentials=System.Net.CredentialCache.DefaultCredentials;
AuthorData.Merge(ws.GetAuthors());
}

2.

View Designer. SaveData,


Click.
- , () .
DataSet d i f f A u t h o r s (
authors1), . UpdateAuthors Web-,
. ,
AuthorData, .

410


( 8.3).
8,3
' Visual Basic
Private Sub SaveData_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles SaveData.Click
If AuthorData.HasChanges() Then
Dim ws As New AuthorsWinClient.localhost;AuthorsService()
ws.Credentials=System.Net.CredentialCache.DefaultCredentials
Dim diffAuthors As New AuthorsWinClient.localhost.authors1()
diffAuthors.Merge(AuthorData.GetChanges())
diffAuthors=ws.UpdateAuthors(diffAuthors)
AuthorData.Merge{diffAuthors)

End If
End Sub

// C#
private void SaveData_Click(object sender, System.EventArgs e)

{
if (AuthorData.HasChanges())

{
AuthorsWinClient.localhost.AuthorsService ws =
new AuthorsWinClient.localhost.AuthorsService();
ws.Credentials=System.Net.CredentialCache.DefaultCredentials;
AuthorsWinClient.localhost.authors1 diffAuthors
=new AuthorsWinClient.localhost.authors1();
diffAuthors.Merge(AuthorData.GetChanges());
diffAuthors=ws.UpdateAuthors(diffAuthors);
AuthorData.Merge(diffAuthors);

}
}

Web-

411

8.5.4.
.
1.

File Save All.

2.

Solution Explorer AuthorsWinClient, Set as Startup Project.

3. <CTRL<+<F5> .
4.

DataGrid.

5.

, ,
(. 8.24).

. 8.24.

, .
,
Web-.

8.6. Web-
Web-
Web-
.
Web-, . Web- Web-,
- ,
Windows-, . . 8.25 -

412

Web-
.

. 8.25. Web-

8.6.1. Web-
Web- .
1.

File Add Project New Project.

2.

Add New Project Project Types Visual


# Projects Visual Basic Projects, Templates
ASP.NET Web Application.

3.

Location Web-
http://ServerName/AuthorsWebClient,

(. 8.26).

WebForm1.aspx.

Web-

Web-

413

. 8.26. Web-

4.

Web-, :
Solution Explorer
AuthorsWebClient Add Web Reference.

Web-
Address Add Web Reference:
http://ServerName/AuthorsWebService/AuthorsService.asmx.

Web- , : http://localhost/AuthorsWebService/
AuthorsService.asmx. <ENTER>.

Add Reference.

Web-, authors1 .
DataGrid
, .

8.6.2.
:
Q DataSet ;
Q DataGrid .

414

.
1.

Data Toolbox Dataset. Choose a DataSet,


TypedDataSet AuthorsWebClient.ServerName.authors1
Name.
DataSet.

2.

DataSet, Name
AuthorData.

3.

Web Forms Toolbox


DataGrid.

4. DataGrid. DataSource AuthorData. DataMember


, authors (. 8.27).

. 8.27.
DataGrid

5. DataGrid
authors. Auto Format
Properties
Professional 3 DataGrid (. 8.28).
6. DataGrid,
Edit, Update, Cancel,
Edit.
Edit, , Edit update
cancel. , :
Property Builder Properties,
Columns;

415

Web-

Button Column Available Columns;


(. 8.29).

Edit,

Update,

Cancel

>

. 8.28. DataGrid

. 8.29.
DataGrid

: Button type PushButton. .

DataGrid
(. 8.30).

416

. 8.30. DataGrid

8.6.3.

,
. . .
View Code.

Page_Load,
DataGrid ( 8.4).
8.4
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ws As New AuthorsWebClient.localhost.AuthorsService()
ws.Credentials=System.Net.CredentialCache.DefaultCredentials
AuthorData.Merge(ws.GetAuthors())
If Not Page.IsPostBack Then
Me.DataGrid1.DataBind()

End If
End Sub
// C#

Web-

417

private void Page_Load(object sender, System.EventArgs e)


{

AuthorsWebClient.localhost.AuthorsService ws =
new AuthorsWebClient.localhost.AuthorsService();
ws.Credentials=System.Net.CredentialCache.DefaultCredentials;
AuthorData.Merge(ws.GetAuthors());
if (! Page.IsPostBack)
{
DataGrid1.DataBind();
}
}

Page_Load Web ( ws).


Credentials. Credentials Web- , Web-,
. AuthorData
Merge.
GetAuthors Web-. , , AuthorData
DataGrid,
. ,
, DataGrid .

, ,
authors. .


SQL-
.
,
. Edit,
EditCommand DataGrid. , EditItemIndex DataGrid EditItemIndex , .
DataGrid .

418

1. EditCommand.
Visual Basic.
View Code.
DataCrid1,
EditCommand (. 8.31.).

. 8.31. EditCommand
Visual C#. DataGrid1.
Properties Events,
DataGrid. EditCommand.
2. ( 8.5).

' Visual Basic


Private Sub DataGrld1_EditCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridCoramandEventArgs)
Handles DataGrid1.EditCommand
DataGrid1.Editltemlndex=e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

// C#

private void DataGrid1_EditCommand(object source,


System.Web.UI.WebControls.DataGridCommandEveny.Args e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
DataGrid1.DataBind();
)
CancelCommand EditCommand (. 8.32).

, 8.32. CancelCommand

Web-

419

Cancel,
CancelCommand DataGrid.
, EditItemIndex -1, . CancelCommand
( 8.6).
8,6

' Visual Basic


Private Sub DataGrid1_CancelCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles DataGrid1.CancelCommand
DataGrid1.Editltemlndex=-1
DataGrid1.DataBind()
End Sub
// C#

private void DataGrid1_CancelCommand(object source,


System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1. EditItemIndex=-1;
DataGrid1.DataBind();

Update,
UpdateCommand DataGrid.
, , DataGrid, AuthorData,
Web-.
UpdateCommand
EditCommand (, 8.33).

. 8.33. UpdateCommand

UpdateCommand
( 8.7).

420

8.7.
' Visual Basic
Private Sub DataGrid1_UpdateCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles DataGrid1.UpdateCommand
' dataset
Dim i As Integer
For i=1 To AuthorData.authors.Columns.Count
Dim t As TextBox=CType(e.Item.Cells(i).Controls(0), TextBox
Dim row As DataRow=AuthorData.authors(e.Item.DataSetIndex)
row(AuthorData.authors.Columns(i-1).Caption)=t.Text
Next
' .
If Me.AuthorData.HasChanges() Then
Dim ws As New AuthorsWebClient.localhost.AuthorsService()
ws.Credentials=System.Net.CredentialCache.DefaultCredentials
Dim diffAuthors As New AuthorsWebClient,localhost.authors1()
diffAuthors.Merge(Me.AuthorData.GetChanges())
ws.UpdateAuthors(diffAuthors)
AuthorData.Merge(diffAuthors)
End If

'
DataGrid1.EditItemIndex=-1
DataGrid1.DataBind()
End Sub
// C#

private void DataGrid1_UpdateCommand(object source,


System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

// dataset
for (int i=1; i<AuthorData.authors.Columns.Count; i++)

TextBox t=(TextBox)(e.Item.Cells[i].Controls[0]);
DataRow row=AuthorData.authors[e,Item.DataSetIndex];
row[AuthorData.authors.Columns[i-1].Caption]=t.Text;
}

Web-

421

// .
if

(AuthorData.HasChanges())

AuthorsWebClient.localhost.AuthorsService ws=
new AuthorsWebClient.localhost.AuthorsService() ;
ws.Credentials=System.Net.CredentialCache.DefaultCredentials;
AuthorsWebClient.localhost.authors1
new

diffAuthors =

AuthorsWebClient.localhost.authors1();

diffAuthors.Merge(AuthorData.GetChanges());
ws.UpdateAuthors

(diffAuthors);

AuthorData.Merge(diffAuthors);
}

DataGrid1.EditItemIndex=-1;
DataGrid1.DataBind();
}

.
A u t h o r D a t a . . . :
- A u t h o r D a t a .
,
d i f f A u t h o r s . AuthorData.
UpdateAuthors Web-, diffAuthors, . . AuthorData. ,
, DataGrid .
, , (.
" ").

8.6.4.
.
1. AuthorsWebClient Solution Explorer. Set

422

as StartUp Project. .
2. <CTRL>+<F5> .

(. 8.34).

. 8.34. DataGrid

Web-,
. ,
, ,
. Web-.
Web- -
.
, , , .

, .

.


, , ( Windows-)
Web- ( Web-). Visual Studio .NET . (solution)
, .
, :
Q

Windows-;

Q Web-, ,
.

9.1. Windows-

Windows-. Windows,
"Windows". Windows-,
.

9.1.1.
Windows- .
1. File New Project.

424

2.

New Project Visual Basic Projects Visual


C# Projects Project Type Windows
Application Templates. Name , MyWin.

3.
4.

Solution Explorer .
Windows Forms Toolbox
Button TextBox.

5.

Text Button ,
Text TextBox (. 9.1).

. 9.1. Windows-

6.

. :

TextBox1.Text="Windows-"

7.

Build Build MyWin .

, . ,

.
()
.
1.

File Add Project New Project.

Add New Project Setup and Deployment Projects Project Type Setup Project Templates. Name MyWin installer (. 9.2).
3. Solution Explorer (. 9.3).

2.

4.

MyWin installer Solution Explorer.


Properties ProductName MyWin (. 9.4).

. 9.2.

. 9.3.

. 9.4.

425

426

ProductName , Add/Remove
Programs.
. ,
.
Windows-
.
1.

MyWin installer Solution Explorer. File


System Editor Application Folder (. 9.5).

. 9.5. Application Folder

2. Application Folder, Add Project Output. Add Project Output Group.


3.

Add Project Output Group


Project MyWin, Primary Output (. 9.6).
Application Folder Primary output
from MyWin (. 9.7).

4.

Build Build MyWin Installer.



.
. MyWin
installer, Debug. Setup.exe
(. 9.8).
.
, . 9.8, Setup.
, . MyWin installer
Install Uninstall.

. 9.6.

. 9.7. Application Folder


( d!l)

. 9,8. , MyWin Installer

427

428

-. MyWin
installer Solution Explorer, Project Install. .
(. 9.9).

. 9.9.

, ,
Browse (. 9.10).

:
. , Disk Cost, , (. 9.11).

Next. .
, ,
.
.

429

. 9.10.

. 9.11.

.
, . , , Windows, .

430

9.1.2.
Windows

, .
1.

MyWin i n s t a l l e r Solution Explorer. File System Editor Primary output from MyWin (. 9.12).

. 9.12. MyWin Installer


2. Primary output from MyWin
Create Shortcut to.
Shortcut to Primary
output from MyWin (. 9.13).

. 9.13.
MyWin Installer
3.

Shortcut to Primary output from MyWin Shortcut to MyWin.

4.

Shortcut to MyWin User's


Desktop (. 9.14).

. 9.14.

431

:
(Application Folder), .

MyWin. 1 (vbn) MyWin. (vbn) .
1.

MyWin installer Solution Explorer. View Editor File Types. File


Types File Types on Target Machine (. 9.15).

. 9,15, File Types

2, File Types on Target Machine File Types.


Action Add File Type.
New Document Type #l,
(. 9.16).

. 9.16. File Types

3.

New Document Type #l Vbn_doc.

4.

Properties Extension
vbn, .

5.

Properties Command Vbn_doc


. Select item in project
(. 9.17).

6.

Select item in project Look in


Application Folder Primary output from MyWin,
(. 9.18).

Windows. .

432

. 9.17. Select item in project

. 9.18. Select item in project

.
Vbn_doc.
. ,
MyWin installer Rebuild. MyWin installer
Install.
, (. 9.19).

433

. 9.19.

9.1.3.
, .
. , , .

Checkbox: , . .
1.

MyWin i n s t a l l e r Solution Explorer.


View Editor ,
User Interface.
(. 9.20).

. 9.20. User Interface

434

2.

(User Interface Editor)


Start Administrative Install.

3.

Action Add Dialog, .

4. Add Dialog Checkboxes () (. 9.21).

. 9.21.
5. Move Up Action,
Checkboxes () Installation Folder.
6.

Checkboxes () Properties
BannerText Samples and Documentation.

7. BodyText Install Samples and Documentation.


8. CheckBox1Label
( ?).

Install

samples?

9. CheckBox2Label Install Documentation?


( ?).
10. Checkbox3visible Checkbox4Visible
false. .
,
, . 9.22.

435

. 9.22. ,
( ),
, Samples and
Documentation . Samples Documentation .
1.

MyWin installer Solution Explorer. View


Editor
File System. Application Folder
(. 9.23).

. 9.23. Application Folder



2.

Action Add Folder.


. Aplication Folder New Folder #1 New Folder #2
(. 9.24).

436

. 9.24. Application Folder

3. New Folder #1 Samples, New Folder #2 Documentation (. 9.25).

. 9.25. Application Folder


, . . , (Tex_Doc1.doc,
Tex_Doc2.doc) (Sample1.doc, Sample2.doc) Word. .
, , .
.
1.

MyWin installer Solution Explorer. View


Editor, File System
Documentation.

2. Action Add File. Add File. Tex_Doc1.doc


Tex_Doc2.doc. Documentation
(. 9.26).
3.

Tex_Doc1.doc File System Editor.

4.

Properties Condition
2=1.

437

. 9.26. , Documentation

. 9.27. Tex_Doc1.doc

, Tex_Doc1.doc
,
Install Documentation? .
5.

Tex_Doc2.doc File System Editor.

6.

Properties condition
.2=1. ,
Tex_Doc2.doc ,
Install Documentation?
.

7.

File System Samples.

8. Action Add File. Sample1.doc Sample2.doc Samples.


9.

Sample1.doc File System Editor.

10. Properties condition


1=1. ,
Sample1.doc ,
Install samples? .
. Sample2.doc File System Editor.
12. Properties condition
1=1. ,

438

Sample2.doc , Install samples?


.
.
, Internet Explorer
. ,
Internet Explorer 5.0 , ,
.


. MyWin
Internet Explorer, Web- .
1.

MyWin installer Solution Explorer. View Editor Launch Conditions.

2.

Launch Conditions Editor Requirements on Target Machine .

3.

Action Add File Launch Condition.


Search for File1 Search Target Machine, a
Condition1 Launch Conditions
(. 9.28).

. 9.28.

4.
5.

Search for File1 Search for Internet Explorer.


Properties FileName Iexplore.exe, Folder [ProgramFilesFolder],

Depth 2 MinVeraion 5.00.


6. Condition1.

7. Message Install Microsoft Internet


Explorer Version 5.00.

439

9.1.4.

, ,
,
.
1. MyWin installer Solution Explorer. View
Property Pages. Installer
Property Pages.
2. Installer Property Pages
Bootstrapper Windows Installer Bootstrapper
(. 9.29).

. 9.29. Installer Property Pages

3.

Build Build MyWin Installer.

440

9.1.5. MyWin

Visual Studio. MyWin
installer Solution Explorer Project Install. MyWin
.
MyWin
.
1.

Windows ,
.
: \documents and settings\yourloginname\MyWin Installer\Debug\.

2.

MyWin installer.msi, (, 9.30).

. 9.30. , -
Visual Studio

3.


Setup.exe, .

,
,
MyWin. , Add/Remove Programs Windows.


Project Uninstall.

441

9.2. Web-

Web-,
Web-. Web-
.
1.

File New Project.

2.

New Project Visual Basic Projects Visual


C# Projects Project Types ASP.NET Web Application Templates.
Location

Web-

http://localhost/MyWeb (. 9.31).

. 9.31. Web-

Web-.
3.

Web Forms Toolbox. TextBox, Button Web- (. 9.32).

. 9.32. Web-

442

4. Button, , :
TextBox1.Text=Web-"

"
".
5. Build Build MyWeb .

9.2.1.
, Web-,
. .
1.

File Add Project New Project.

2.

Add New Project Setup and Deployment


Projects Project Type Web Setup Project Templates.
Name , , WebDeploy (. 9.33).

. 9.33. Web-

, Solution
Explorer File System
(. 9.34).

443

. 9.34.
3.

WebDeploy, Properties
productName MyWeb.


, .
1.

File System Editor Web Application Folder.


Action Add Project Output.
Add Project Output Group.

2.

Add Project Output Group


Project MyWeb.

3.

<Ctrl> Primary
Output Content Files (. 9.35).

. 9.35. Web-
Web Application
Folder.

444

.
1.

WebApplication. Properties
VirtualDirectory xyz.

2.

Properties D e f a u l t D o c u m e n t WebForm1.aspx.

3.

Build Build WebDeploy.

MyWeb.

9.2.2.
Web-
Solution Explorer WebDeploy. Project Install.
.
(. 9.36).

. 9.36. WEB-

445

,
(. 9.37).

. 9.37.
Web-


.
,
Web-.
C:\Inetpub\wwwroot\xyz
(. 9.3S).
Web-,
Web-,

Internet
Explorer

http://localhost/xyz.
Web-. ,
"Web-" (. 9.39).
, Web-
Visual Studio, Windows
Internet Explorer.

446

. 9.38. , -
Web-

. 9.39. Web- Internet Explorer

9.2.3.
Web-
Web- Web- , , Web-.
.
1. Windows . \documents and settings\yourloginname\ My
Documents\Visual Studio Projects\WebDeploy\Debug\(p. 9.40).

447

. 9.40. Web- Visual Studio

2. WebDeploy.msi, Web- ,
Web-. Web- C:\Inetpub\wwwroot\.
3. Web- Setup.exe, Web-.

Web- .

4. Web-,
,

Internet
Explorer

http://[ComputerName]/xyz. Web- Internet
Explorer .
Web-. ,
"Web-".

9.2.4. Web-
1.

Windows

Add/Remove Programs.
2. Add/Remove Programs MyWeb
Remove.

448

Web-
Visual Studio Project Uninstall.

,
. Web-,
.

10


-
Microsoft .Net
-, .
, : , , , .
-
. : , , , .
-, :

Q (
, );
Q

- Web-;

Q - .

10.1.
. ,
. -

450

10

( , ),
(
), - (
, ,
) . . .
, . , ( - , , . .). ,
. 10.1.

. 10.1.

, , .
.
"",
.
, ,

( ).
Microsoft .Net- Web-
. -,
, -, -

451

. ( ) . (, Web-) , , .
, , . .Net
,
,
, .
.
, , .
. , . -.
.

.
. , .
.
, . ,

.
,
,
,
-.
, :
Q ;
;

452

10

Q ;
Q .
,

, . ,
- ,

.
, , "" . ,
. - ,
.
. .
,
. ,
.

, , .
:
Q ;
Q -, Web-;
Q , Web.

10.2.
, Northwind,
SQL Server. Northwind :
Q categories ( );
Q Products ( );

453

Q Customers ();
Q orders ();
Q Orders

Details ( ).


Customers (),
Pass. SQL Server Enterprise Manager,
Customers pass (. 10.2).

. 10.2. Customers

CustomerID Pass . Pass


(
1,2,3, ... 10).
, ( 10 ) - .

10.3. Web-
- Web-,
:
Q ,
(CustomerExist);

Q , (CustomerEnter);
Q

(RegUser);

Q
(GetCategories);

454

10

Q (GetProducts);
Q (GetOrders);
Q (AddOrders);
Q (DelOrders);
Q
(GetDetails);

Q (AddDetails);
Q (DelDetails).
. Web- .
1.

File New Project New Project.

2. , ,
Visual Basic Projects Visual C# Projects Project Types,
ASP.NET Web Service Templates.
3.

Location
http://ServerName/MyWebService

(. 10.3).

. 10.3. Web-

455

Web- ,
localhost. Visual Studio ASP.NET
Web-,
Web Access Failed (
IIS ).

MyWebService Service1.asmx, .

10.3.1.

, Web-, . , , .
,
. ,
. Customers (). Web
.
.
1.

Server Explorer.

Server Explorer . , View


Server Explorer.

Northwind,
Server Explorer Data Connections, Northwind.
2. Server Explorer Tables
Northwind.
3.

Customers, CustomerID, CompanyName,

Pass Web- (. 10.4).


4.

SqlConnectionl

SqlDataAdapter1.

SqlDataAdapter1

Properties CustomersSqlDataAdapter (. 10.5).

456

10

. 10.4. Server Explorer

. 10.5. SqlConnectionl CustomersSqlDataAdapter


Web-

,
( ) Customers Northwind.
CustomersSqlDataAdapter ,
SelectCommand.
CustomersSqlDataAdapter, Properties, SelectCommand, CommandText
.
Query Builder.
(. 10.6):
SELECT CustomerID, CompanyName, Pass
FROM Customers
WHERE

(CustomerID=@CUSTOMERID)

457

. 10.6.
Query Builder


.
,
(customerExist). .
1.

Solution Explorer
Servise1.asmx View Code.

2.

( 10.1).

10.1
' Visual Basic
<WetbMethod()> Public Function CustomerExist(ByVal CUSTOMERID As
As Boolean
Dim ds As New DataSet()
Dim flag As Boolean
Dim str As String
SqlSelectCommand1.Parameters("@CUSTOMERID").Value =

String)

458

10
CUSTOMERID
CustomersSqlDataAdapter.Fill(ds)

Try
str=ds.Tables(0).Rows(0).Item(0)
flag=True
Catch
flag=False
End Try

ds.Dispose()
CustomerExist=flag
End Function
//C#

[WebMethod]
public bool CustomerExist (string CUSTOMERID)
{

DataSet da;
ds=new

DataSet("Customers");

bool flag;
string str;
sqlSelectConmand1.Parameters["@CUSTOMERID"].Value =
CUSTOMERID;
CustomersSqlDataAdapter.Fill(ds);

try

{
str=(string)

ds.Tables[0].Rows[0]["CustomerID"];

flag =true;

}
catch

{
flag=false;
}

ds.Dispose();
return flag;

.
CUSTOMERID, , -

459

ds ( ), f l a g ( ,
) str.
Select .
Fill CustomersSqlDataAdapter ds
. ,
, ds , , ds . try...Catch
str , ,
ds. , flag
true, false,
DataSet Dispose
CustomerExist .
,
.
1. Solution Explorer
Servise1.asmx Build and
Browse. Web-
(. 10.7).

. 10.7. CustomerExist Web-

2.

CuctomerExist,
Web-.
3. ( ANTON) Invoke (. 10.8).
4.

, true
(. 10.9).

460

10

. 10.8. Web- CustomerExist

. 10.9. Web- CustomerExist XML


(, 12345), false.
,
, .
CustomersSqlDataAdapter,
. ,
InsertComman.

CustomersSqlDataAdapter.

InsertComman

CustomersSqlDataAdapter, Properties, InsertComman, CommandText . Query


Builder. (. 10.10):
INSERT INTO Customers
(CustomerID, CompanyName, Pass)
VALUES (@CustomerID, @CompanyName, @Pass)

461

. 10.10.
Query Builder

,
(RegUser).
.
1. Solution Explorer
Servise1.asmx View
Code.
2. ( 10.2).
10.2
' Visual Basic
<WebMethod()> Public Function RegUser(ByVal CUSTOMERID As
String,ByVal PASS As String, ByVal CompanyName As String) As
Boolean
Dim flag As Boolean
Dim user As Boolean
user=CustomerExist(CUSTOMERID)
If user Then
flag=False
Else
SqlInsertCommand1.Parameters("@CUSTOMERID").Value

CUSTOMERID
SqlInsertCommand1.Parameters("@PASS").Value=PASS
SqlInsertCommand1.Parameters("@CompanyName").Value =
CompanyName
SqlConnection1.Open()
SqlInsertCommand1.ExecuteNonQuery()
SqlConnection1.Close()

462

10

flag=True

End If
RegUser=flag
End Function

//C#
[WebMethod]
public bool RegUser (string CUSTOMERID, string PASS, string
CompanyName)
{

bool flag ;
bool user ;
user=CustomerExist (CUSTOMERID) ;
if (user)
{

flag=false;
}

else

{
sqlInsertCommand1,Parameters["@CUSTOMERID"].Value

CUSTOMERID;
sqlInsertCommand1.Parameters["@PASS"].Value=PASS;
sqlInsertCommand1.Parameters["@CompanyName"].Value=
CompanyName;
SqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();
SqlConnection1.Close();
flag=true;
}

return flag;

,
customers: CUSTOMERID, PASS, CompanyName.
: true
, false .
flag user. ,
CustomerExist, user. -

463

(user=true), flag false .


, InsertCommand1 , ,
ExecuteNonQuery .
flag true ( )
. Insert
,
ExecuteNonQuery.
CustomerExist
.

. CustometEnter ( 10.3.)
10.3
' Visual Basic
<WebMethod()> Public Function CustomerEnter(ByVal CUSTOMERID As String,
ByVal PASS As String) As Boolean
Dim ds As New DataSet()
Dim flag As Boolean
Dim str As String
SqlSelectCommand1.Parameters("@CUSTOMERID").Value=CUSTOMERID
CustomersSqlDataAdapter.Fill(ds)

Try
str=ds.Tables(0).Rows(0).Item("pass")
If Trim(str)=Trim(PASS) Then
flag=True
Else
flag=False

End If
Catch
flag=False

End Try
ds.Dispose()
CustomerEnter=flag
End Function

//C#

464

10

[WebMethod]
public bool CustomerEnter (string CUSTOMERID, string PASS)
{

DataSet ds;
ds=new

DataSet("Customers");

bool flag ;
string str;
sqlSelectCommand1.Parameters["@CUSTOMERID"].Value=CUSTOMERID;
CustomerssqlDataAdapter.Fill(ds) ;
try
{

str=(string) ds.Tables[0].Rows[0]["pass"];
flag=true;
if (alltrim(str)=alltrim(PASS))
{

flag=true;

else
{
flag=false;

}
}
catch
{
flag=false;
}

ds.Dispose();
return flag;
)

CustomerExist. ,
( ). , Try...Catch str , ,
, .
, flag true, , .
, , Web- , .

465

10.3.2.
, Web
. (GetProducts)
, . ,
-
( ). ,
Products.
.
1. Data lB
Web- SqlDataAdapter. .
Products
(. 10.11).
SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM Products
WHERE

(CategoryID=@CategoryID)

. 10.11. GetProducts Query Builder

466

10

2.

Properties
( Name) ProductsSqlDataAdapter.

3.


Web- GetProducts ( 10,4).

10.4
' Visual Basic
<WebMethod()> Public Function GetProducts(ByVal CUSTOMERID As String,
ByVal PASS As String, ByVal CategoryID As Intl6) As DataSet
Dim Products As New DataSet()
Dim user As Boolean
user=CustomerEnter(CUSTOMERID, PASS)
If user Then
SqlSelectCoraraand2.Parameters("@CategoryID").Value=
CategoryID
ProductsSqlDataAdapter.Fill(Products)

End If
Return Products
End Function

//C#
[WebMethod]
public DataSet GetProducts

(string CUSTOMERID,

string PASS,

int

CategoryID)

{
DataSet

Products;

Products=new

DataSet("Products");

bool user ;
user=CustomerEnter(CUSTOMERID, PASS);
if (user)

{
sqlSelectCommand2.Parameters["@CategoryID"],Value=CategoryID;
ProductsSqlDataAdapter.Fill(Products);

}
return Products;

467

:
CUSTOMERID, PASS, CategoryID. - (
Products, ).
Products
( ) user.
CustomerEnter,
. (user=true),
C a t e g o r y I D select ProductsSqlDataAdapter
Products.
. .
1. Solution Explorer
Servise1.asmx Build and Browse. Web-.
2. GetProducts, Web-.
3.

(ANTON), (3), (1) Invoke (. 10.12).

. 10.12. Web- GetProducts

10

465

,
products XML- (. 10.13).

. 10.13. Web- GetProducts XML


, , . Web-
, (GetCategories).
. ,
. , categories.
(Web-) .
1. Data lB Web- SqlDataAdapter. . Categories
(. 10.14).
SELECT CategoryID, CategoryName FROM Categories
2.

Properties
( Name) CategoriesSqlDataAdapter.

4.


Web- GetCategories ( 10.5).

469

. 10.14. GetCategories Query Builder

10.5
' Visual Basic
<WebMethod()> Public Function GetCategories(ByVal CUSTOMERID As String,
ByVal PASS As String) As DataSet
Dim Categories As New DataSet()
Dim user As. Boolean
user=CustomerEnter(CUSTOMERID, PASS)
If user Then
CategoriesSqlDataAdapter.Fill(Categories)
End If

Return Categories
End Function
//C#

[WebMethod]
public DataSet GetCategories (string CUSTOMERID, string PASS)
{

DataSet Categories;
Categories=new

DataSet("Categories");

bool user ;
user=CustomerEnter(CUSTOMERID, PASS);
if (user)

470

10

{
CategoriesSqlDataAdapter.Fill(Categories);
}
return Categories;

:
CUSTOMERID, PASS. - Categories,
.
Categories
( ) user.
CustomerEnter,
. (user=true),
Categories.
.
.
1. Solution Explorer
Servise1.asmx Build
and Browse. Web-.
GetCategories,
Web-.
3. (ANTON), (3),
(1) Invoke (. 10.15).
2.

. 10.15. Web- GetCategories

471

,
Categories XML- (. 10.16).

. 10.16. Web- GetCategories XML

10.3.3.
Web-
(GetOrders).
, ,
. ,
. ,
Orders.
.
1.

Data ToolBox
Web- SqlDataAdapter. . Orders (. 10.17).

SELECT OrderID, CustomerID, OrderDate


FROM Orders
WHERE

(CustomerID=@CustomerID)

2. Properties
( Name) OrdersSqlDataAdapter.

3.


Web- GetOrders ( 10.6).

472

10

. 10.17. GetOrders Query Builder

10.6
' Visual Basic
<WebMethod()> Public Function GetOrders(ByVal CUSTOMERID As String,
ByVal PASS As String) As DataSet
Dim Orders As New DataSet()
Dim user As Boolean
user=CustomerEnter(CUSTOMERID, PASS)
If user Then
SqlSelectCommand5.Parameters("@CustomerID").Value
CUSTOMERID
OrdersSqlDataAdapter.Fill(Orders)
End If

Return Orders
End Function
//C#

[WebMethod]
public DataSet GetOrders (string CUSTOMERID, string PASS)
{

473

DataSet Orders;
Orders=new DataSet("Orders");
bool user ;
user=CustomerEnter(CUSTOMERID, PASS);
if (user)
{

sqlSelectCommand5Paraineters ["@CustomerID"] . Value=CUSTOMERID;


OrdersSqlDataAdapter.Fill(Orders);

}
return Orders;
}

: CUSTOMERID
PASS. -
.
Orders (
) user.
CustomerEnter,
.
(user=true), CustomerID select
Orders SqlDataAdapter Orders.
.
.
1. Solution Explorer
Servise1.asmx Build
and Browse. Web-.
2. GetOrders, Web-.
3.

(ANTON), (3) Invoke (. 10.18).


4. , orders XML- (. 10.19).

, Web- (AddOrders).
. ,
, . -

474

10

.
Orders.

. 10.18. Web- GetOrders

. 10.19. Web- GetOrders XML

AddOrders
OrdersSqlDataAdapter. Select insert.
, .
1. OrdersSqlDataAdapter

Properties.
2.

Properties OrdersSqlDataAdapter
InsertCommand CommandText.

3. CommandText,

(. 10.20).

475

INSERT INTO Orders


(CustomerID, OrderDate)
VALUES (@CustomerID, @OrderDate);
SELECT OrderID, CustomerID, OrderDate
FROM Orders
WHERE

(OrderID=@@IDENTITY)

. 10.20. AddOrders Query Builder


4. Query Builder, Web- AddOrders ( 10.7).
10.7
' Visual Basic
<WebMethod()> Public Function AddOrders(ByVal CUSTOMERID As String,
ByVal PASS As String) As Boolean
Dim user As Boolean
Dim uspex As Boolean
Dim Dat As Date
Dat=DateTime.Today
user=CustomerEnter(CUSTOMERID,

PASS)

If user Then
Try

OrdersSqlDataAdapter.InsertCommand.Parameters
("@CustomerID").Value=CUSTOMERID
OrdersSqlDataAdapter.InsertCommand.Parameters
("@OrderDate").Value=Dat
SqlConnection1.Open()

476

10
OrdersSqlDataAdapter.InsertCommand.ExecuteNonQuery()
SqlConnection1.Close()
uspex=True
Catch
uspex=False

End Try
Else
uspex=False

End If
Return uspex
End Function

//C#
[WebMethod]
public bool AddOrders (string CUSTOMERID, string PASS)

{
bool user;
bool uspex;
DateTime Dat=new DateTime();
Dat= DateTime.Today;
user=CustomerEnter(CUSTOMERID,

PASS);

if (user)

{
try

{
OrdersSqlDataAdapter.InsertCommand.Parameters
["@CustomerID"].Value=CUSTOMERID;
OrdersSqlDataAdapter.InsertCommand.Parameters
["@OrderDate"].Value=Dat;
SqlConnection1.Open();
OrdersSqlDataAdapter.InsertCommand.ExecuteNonQuery();
SqlConnection1.Close();
uspex=true;

}
catch

{
uspex=false;

477

}
}
else
{
uspex=false;
}
return uspex;

: CUSTOMERID
PASS. - uspex: true
, false , .
user, uspex Dat (
). Dat
. CustomerEnter,
. (user=true), Try...Catch . insert
CustomerID, OrderDate ,
,
InsertCommand,
uspex true. - , uspex false.
,
.
1. Solution Explorer
Servise1.asmx
Build and Browse. Web.
2. AddOrders, Web-.
3.

(ANTON), (3) Invoke (. 10.21).

, true
XML- (. 10.22).

4 78

10

. 10.21. Web- AddOrders

. 10.22. Web- AddOrders XML

, Web- DelOrders.
, , .
, , .
. ,
Orders Order Details.

DelOrders
Orders SqlDataAdapter. Select Delete.
, .
1. OrdersSqlDataAdapter
Properties.
2. OrdersSqlDataAdapter Delete
Command CommandText.

479

3. CommandText,
. , (. 10.23).
DELETE FROM Orders WHERE (OrderID=@Original_OrderID)

. 10.23. DelOrders Query Builder

4.

Query Builder.

orders
Order Details.
DataCommand, .
1. Data ToolBox Web- SqlCommand.
2. Properties SqlCommand,

Name

DelDetailsSqlCommand.

3. Connection,
, Existing SqlConnectionl.
4. CommandText,
Query Builder.
5.

order Details
(. 10.24).

DELETE FROM [Order Details] WHERE (OrderID=@Original_OrderID)

480

10

. 10.24. DelOrders Query Builder

6.

Query Builder, Web- DelOrders ( 10.8).

10,8
' Visual Basic
WebMethod()> Public Function DelOrders(ByVal CUSTOMERID As String,
ByVal PASS As String, ByVal OrgerID As Int32) As Boolean
Dim user As Boolean
Dim uspex As Boolean
user=CustomerEnter(CUSTOMERID, PASS)
If user Then
Try

OrdersSqlDataAdapter.DeleteCommand.Parameters
("@Original_DrderID").Value=OrgerID
DelDetailsSqlCommand.Parameters
("@Original_OrderID").Value=OrgerID
SqlConnection1.Open()
DelDetailsSqlCommand.ExecuteNonQuery()
OrdersSqlDataAdapter.DeleteCommand.ExecuteNonQuery()
SqlConnection1.Close()
uspex=True
Catch
uspex=False
End Try

-
Else
uspex=False

End If
Return uspex
End Function

//C#
[WebMethod]
public bool DelOrders (string CUSTOMERID, string PASS, int OrgerID)

{
bool user ;
bool uspex ;
user=CustomerEnter(CUSTOMERID, PASS);
if (user)

{
try

{
OrdersSqlDataAdapter.DeleteCommand.Parameters
["@Original_OrderID"].Value=OrgerID;
DelDetailsSqlCommand.Parameters
["@Original_OrderID"].Value=OrgerID;
SqlConnection1.Open();
DelDetailsSqlCommand.ExecuteNonQuery();
OrdersSqlDataAdapter.DeleteCommand.ExecuteNonQuery();
SqlConnection1.Close ();
uspex=true;

}
catch
{
uspex=false;
}
}
else
{
uspex=false;

}
return uspex;
}

481

482

10

:
CUSTOMERID,
PASS
OrderID. -
uspex: true , f a l s e
.
user uspex. CustomerEnter, . (user=true), Try...Catch .
@Originai_OrderID DeleteCommand

ordersSqlDataAdapter

DelDetailsSqlCommand
OrderID, ,
DelDetailsSqlCommand
DeleteCommand,
uspex true. - , uspex false.
,
.
1.

OrdersSqlDataAdapter.

2.
Preview Data.
3.

Data Adapter Preview


(. 10.25).

4.

(, 11089) .

5.

Solution Explorer
Servise1.asmx Build and
Browse. Web-.

6.

DelOrders, Web-.

7.

(ANTON), (3),
11089 Invoke (. 10.26).

, true
XML- (. 10.27).

483

. 10.25. ( Orders)

. 10.26. Web- DelOrders

. 10.27. Web- DelOrders XML

484

10

10.3.4.

Web- GetDetails, .

, .
,
-.
,
Order Details.
GetDetails .
1.

Data TooIBox Web- SqiDataAdapter. . Products, order Details (. 10.28).

SELECT [Order Details].OrderID, Products.ProductName,


tails] . OnitPrice, [Order Details].Quantity

[Order De-

FROM [Order Details] LEFT OUTER JOIN


Products ON

[Order Details].ProductID=Products.ProductID

WHERE ([Order Details].OrderID=@OrderID)

. 10.28. GetDetails Query Builder

485

2. Properties
( Name) DetailsSqlDataAdapter.
3.
Web- GetDetails ( 10.9).

10.9
' Visual Basic
<WebMethod()> Public Function GetDetails(ByVal CUSTOMERID As
String, ByVal PASS As String, ByVal OrderID As Int32) As DataSet
Dim Details As New DataSet()
Dim user As Boolean
user=CustomerEnter(CUSTOMERID, PASS)
If user Then
SqlSelectCommand3.Parameters("@OrderID"),Value=OrderID
DetailsSqlDataAdapter.Fill(Details)
End If

Return Details
End Function
//C#

[WebMethod]
public DataSet GetDetails (string CUSTOMERID, string PASS, int OrderID)
{

DataSet Details;
Details=new

DataSet("Details");

bool user ;
user=CustomerEnter(CUSTOMERID,

PASS);

if (user)
sqlSelectCommand3.Parameters["@OrderID"].Value=OrderID;
DetailsSqlDataAdapter.Fill(Details);
}

return Details;

:
CUSTOMERID, PASS, OrderID. .

486

10

Details
( ) user.
CustomerEnter, .
(user=true), OrderID
Select D e t a i l s S q l D a t a A d a p t e r
Details.

,
Web- AddDetails.
,
OrderID, ProductID, UnitPrice
Quantity.
,
().
. , Order Details.
AddDetails DetailsSqlDataAdapter.
select insert.
,
.
1.

DetailsSqlDataAdapter
Properties.

2.

InsertCommand New .

3.

connection

SqlConnectionl.

4.

Properties
DetailsSqlDataAdapter
InsertCommand CommandText.

5. CommandText,
. (. 10.29).
INSERT INTO [Order Details]
(OrderID, ProductID, UnitPrice, Quantity)
VALUES (@OrderID, @ProductID, @UnitPrice, @Quantity)

487

. 10.29. AddDetails Query Builder

6.

Query Builder, Web- AddDetails ( ).

10.10
' Visual Basic
<WebMethod()> Public Function AddDetails(ByVal CUSTOMERID As String,
ByVal PASS As String, ByVal OrderID As Int32, ByVal ProductID As
Int32, ByVal UnitPrice Double, ByVal Quantity As Intl6) As Boolean
Dim user As Boolean
Dim uspex As Boolean
user=CustomerEnter(CUSTOMERID, PASS)
If user Then
Try

DetailsSqlDataAdapter.InsertCommand.Parameters
("@OrderID").Value=OrderID
DetailsSqlDataAdapter.InsertCommand.Parameters
("@ProductID"),Value=ProductID
DetailsSqlDataAdapter.InsertCommand.Parameters
("@UnitPrice").Value=UnitPrice
DetailsSqlDataAdapter.InsertCommand.Parameters

488

10
("@Quantity").Value=Quantity
SqlConnection1.Open()
DetailsSqlDataAdapter.InsertCommand.ExecuteNonQuery()
SqlConnection1.Close()
uspex=True
Catch
uspex=False

End Try
Else
uspex=False

End If
Return uspex
End Function

//C#
[WebMethod]
public bool AddDetails (string CUSTOMERID, string PASS, int OrgerID,
int ProductID, double UnitPrice, int Quantity)

{
bool user ;
bool uspex ;
user=CustomerEnter(CUSTOMERID, PASS);
if (user)

{
try

DetailsSqlDataAdapter.InsertCommand.Parameters
("@OrderID"]Value=OrgerID;
DetailsSqlDataAdapter.InsertCommand.Parameters
["@ProductID"].Value=ProductID;
DetailsSqlDataAdapter.InsertCommand.Parameters
["@UnitPrice"].Value=UnitPrice;
DetailsSqlDataAdapter.InsertCommand.Parameters
["@Quantity"].Value=Quantity;
SqlConnection1.Open();
DetailsSqlDataAdapter.InsertCommand.ExecuteNonQuery();
SqlConnection1.Close();
uspex=true;

489

catch
{

uspex=false;

}
}
else

{
uspex=false;

}
return uspex;
}

:
CUSTOMERID, PASS, OrderID, ProductID, UnitPrice, Quantity, -
uspex: true false , .
user uspex. CustomerEnter,
.
(user=true), Try...Catch
. , order ID,
ProductID, UnitPrice, Quantity ,
,
InsertCommand, uspex
true. -
, uspex false.
AddDetails.
, , SQL Enterprise Manager, .
1. Solution Explorer
Servise1.asmx Build
and Browse. Web-.
2. AddDetails Web-.
3.

(ANTON), (3)
Invoke (. 10.30).

10

490

. 10.30. Web- AddDetails

, true
XML- (. 10.31).

. 10.31. Web- AddDetails XML

, Web- DelDetails. , OrderID , ProductID.


, . .
, order Details.
Web- DelDetails
DetailsSqlDataAdapter. -

491

select Delete.
,
.
1. DetailsSqlDataAdapter Properties.
2.

DetailsSqlDataAdapter Delete
Command CommandText. , Delete Command New, Connection
sqlConnection1.

3. CommandText,
. , (. 10.32).
DELETE FROM [Order Details]
WHERE (OrderID=@Original_OrderID) AND (ProductID=@Original_ProductID)

. 10.32. DelDetails Query Builder

4. Query Builder, Web- DelDetails ( 10.11).


10.11
' Visual Basic
<WebMethod()> Public Function DelDetails(ByVal CUSTOMERID As
String, ByVal PASS As String, ByVal OrderID As Int32, ByVal ProductID

492

10

As Int32) As Boolean
Dim user As Boolean
Dim uspex As Boolean
user=CustomerEnter(CUSTOMERID, PASS)
If user Then
Try

DetailsSqlDataAdapter.DeleteCommand.Parameters
("@Original_OrderID").Value=OrgerID
DetailsSqlDataAdapter.DeleteCommand.Parameters
("@Original_ProductID"),Value=ProductID
SqlConnection1.Open()
DetailsSqlDataAdapter.DeleteCommand.ExecuteNonQuery()
SqlConnection1.Close()
uspex=True
Catch
uspex=False
End Try

Else
uspex=False
End If

Return uspex
End Function
//C#

[WebMethod]
public bool DelDetails (string CUSTOMERID, string PASS, int OrderID,
int ProductID)
{

bool user ;
bool uspex ;
user=CustomerEnter(CUSTOMERID,

PASS);

if (user)

{
try

DetailsSqlDataAdapter.DeleteCommand.Parameters
["@Original_OrderID"],Value=OrderID;
DetailsSqlDataAdapter.DeleteCommand.Parameters

493

["@Original_ProductID"].Value=ProductID;
SqlConnection1.Open();
DetailsSqlDataAdapter.DeleteCommand.ExecuteNonQuery ( ) ;
SqlConnection1.Close();
uspex=true;
}

catch
{

uspex=false;
}
}

else
{

uspex=false;

}
return uspex;

:
CUSTOMERID, ) PASS OrderID,
ProductID. - uspex: true
, false , .
user uspex. CustomerEnter,
.
(user=true), Try...Catch . , DeleteCommand
DetailsSqlDataAdapter Original_OrderID

Original_ProductID -

, ,
DeleteCommand, uspex true.
- ,
uspex false.
.
, DetailsSqlDataAdapter , , , 11065, 54 (. 10.33)
.

494

10

. 10.33. OrderDetails

1. Solution Explorer
Servise1.asmx Build
and Browse. Web-.
2. DelDetails, Web-.
3.

(ANTON), (3),
(11065), (54) Invoke
(. 10.43).

. 10.34. Web- DelDetails

495

, true
XML- (. 10.35).

. 10.35. Web- DelDetails XML

10.4.

Web- Web-. ,
. Web , .
, .

SessionState.
Web-,
.
1. File New,
Project.
2. New Project :
Project Types Visual Basic Projects
Visual C# Projects;
Templates ASP.NET Web
Application;
Location, .
http://, .
Web- (IIS) 5,0 , .NET
Framework. Web- ,
http://localhost.
MyWebSite (. 10.36).

496

10

. 10.36. Web-

, Close
Solution.

3.

, Web-, Web-, . , Web-


WebForm1.aspx.
4. Web- WebForm1.aspx Default.aspx
Web-.

10.4.1. Web-

Web- :
,
, Web-.
, Web-
image ( ),
.

497

.
1.

HTML Toolbox image.

2. image
Properties. <IMG>
Property Pages. image source ,
(. 10.37).

. 10.37. <IMG> Property Pages

images
Web-, , C:\lnetpub\wwwroot\images\,
.
, "", Web- .

Web- .
1. Web.
2. Properties. DOCUMENT Property Pages.

498

10

Background image
,
(. 10.38).

. 10.38. DOCUMENT Property Pages

, , images Web-, ,
C:\Inetpub\wwwroot\images\.
.
1. Web Forms Toolbox
(. 10.1).
10.1. Web-

TextBox

ID: TextBox1

TextBox

ID: TextBox2

TextBox

ID: TextBox3

TextBox

ID: TextBox4

499

10.1. ()

TextBox

( )

ID:

TextBox5

TextBox

( )

ID: TextBox6

Button

ID: Button1
Text:

Button

ID: Button2
Text:

2.
.
3. . 10.39.

. 10.39. Default.aspx

4.

, ,
TextMode Password.

, , , .
1. Web Forms Toolbox
CompareValidator
TextBox3.

500

10

2. ,
Properties, ,
. 10.2.
10.2. Web-

ControlToValidate

TextBox3

Text

ErrorMessage

ControlToCompare

TextBox2

Operator

Equal

Display

Dynamic

,
ValidationSummary. ErrorMessage
, .
, V a l i d a t i o n S u m m a r y .

.
1.

Web Forms Toolbox


ValidationSummary. , -

.
2.

, DisplayMode.

3. Font ForeColor
.
, Label, , - . 10.40.
Web-
Web-. Web-
, -, Web-, - . Web-.

501

. 10.40. Default.aspx

Web- Web- .
1.

Project Add Web Reference.

2.

Address Add Web Reference Web, , ,


http: //
<servername>/ MyWebServicel/Service1.asmx

Web- , http://localhost/MyWebService1/Service1.asmx <ENTER> (. 10.41).

. 10.41. Add Web Reference

502

10

Add Reference Add Web Reference.


.
( 10.12).
10.12
' Visual Basic
Private Sub Button1_Click(ByVal sender System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ws As New localhost.Service1()
Dim flag As Boolean
flag=ws.CustomerExist(TextBox1.Text)
If flag Then
Label1.Text="
"
Else
ws.RegUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
Session.Clear()
Session.Add("login",
Session.Add("pass",

TextBox1.Text)
TextBox2.Text)

Response.Redirect("main.aspx")

End If
End Sub
//C#
private void Button1_Click(object sender, System.EventArgs e)

{
localhost.Service1 ws=new localhost.Service1() ;
bool flag;
flag=ws.CustomerExist(TextBox1.Text);
if (flag)

{
Label1.Text="

";

}
else

{
ws.RegUser(TextBox1.Text, TextBox2.Text, TextBox3.Text);
Session.Clear();

-
Session.Add("login", TextBox1.Text);
Session.Add("pass", TextBox2.Text);
Response.Redirect("main.aspx");
}
}

ws,
Web- ,
WebReference. flag. ,
.
Web- CustomerExist,
.
, f l a g true
Label "
". Web- RegUser, . ,
. Session,

. Clear, Add
: (login) (pass). .
, , Web-, Web-. main.aspx, .
, , ,
. , ,
.

. ( 10.13).
10.13
' Visual Basic
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

504

10

Dim ws As New localhost.Service1()


Dim flag As Boolean
flag=ws.CustomerEnter(TextBox5.Text, TextBox6.Text)
If flag Then
Session.Clear()
Session.Add("login", TextBox5.Text)
Session.Add("pass" , TextBox6.Text)
Response.Redirect("main.aspx")
Else
Label1.Text="

"

End If
End Sub

//C#
private void Button2_Click(object sender, System,EventArgs e)
{

localhost.Service1 ws=new localhost.Service1();


bool flag;
flag=ws.CustomerEnter(TextBox5.Text, TextBox6.Text);
if (flag)
{

Session.Clear();
Session.Add("login", TextBox5.Text);
Session.Add("pass", TextBox6.Text);
Response.Redirect("main.aspx");
}

else
{

Label1.Text=" ";
}
}

. . CustomerEnter
Web-
. , flag true,
Session .

505

main.aspx, Web-, Start , ,


(. 10.42).

. 10.42.

Session, .
. ,
. ""
. ,
, . ,
. , Session,


. , Session, ,

10

506

,
.

10.4.2. Web-

Web- main.aspx,
( ).
Web- :
DropDownList , DataGrid , Button .
Web: GetCategories
DropDownList, GetProducts DataGrid. ,
Web- image (
), (
), Label,
.
.
1.

HTML Toolbox image.

2. image
Properties. IMG Property Pages. Image source ,
(. 10.43).

. 10.43. IMG Property Pages

Web- .
1. Web.

2.

507

Properties.
DOCUMENT Property Pages.
Background image , (. 10.44).

. 10.44. DOCUMENT Property Pages

, ,

images
Web-,
,
:\Inetpub\wwwroot\images\.
.
1.

2.

HTML Toolbox Label,


" ".
Build Style.
Style Builder .
Web Forms Toolbox
DropDownList DataGrid.

3. DataGrid,
Auto Format. , (
Professional1).
4.

Web Forms Toolbox


Button. Properties
Text .

5.

. 10.45.

. DropDownList. ,
, DataGrid.

508

10

. 10.45. main.aspx

,
. Page_Load ( 10.14).
10.14
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim ws As New localhost.Service1()
Dim ds As New DataSet()
Dim log, pass As String
log=Session.Item("login")
pass=Session.Item("pass")
ds.Merge(ws.GetCategories(log,

pass))

DropDownList1.DataSource=ds
DropDownList1.DataMember="Categories"
DropDownList1.AutoPostBack=True
DropDownList1.DataTextField="CategoryName"
DropDownList1.DataValueField="CategoryID"
DropDownList1.DataBind()
End If

Sys-

509

End Sub

//#
private void Page_Load(object sender, System.EventArgs e)

if (! Page.IsPostBack)
{

localhost.Service1 ws=new localhost.Service1() ;


DataSet ds;

ds=new DataSet("Categories");
string log;
string pass;

log=(string) Session["login"];
pass=(string)Session["pass"];
ds.Merge(ws.GetCategories(log,

pass));

DropDownList1.DataSource=ds;
DropDownList1.DataMember="Categories";
DropDownList1.AutoPostBack=true;
DropDownList1.DataTextField="CategoryName";
DropDownList1.DataValueField="CategoryID";
DropDownList1.DataBind();

}
}

DropDownList if Not IsPostBack, ,


.
: ws Web-, ds , log pass .
log pass
session
.
ds Merge. GetCategories Web-,
.
, ds Categories Northwind.

510

10

DropDownList ds.
DataSource ds.
DataMember Categories.
AutoPostBack true,
. categoryName DropDownList,
CategoryID .

DropDownList, DropDownList1_SelectedIndexChanged.
( 10.15).
10.15
' Visual Basic
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As
tem.Object, ByVal e s System.EventArgs) Handles DropDownList1.SelectedIndexChanged

Sys-

Dim ws As New localhost.Service1()


Dim ds As New DataSet()
Dim log, pass As String
Dim categoryid As String
log=Session.Item("login")
pass=Session.Item("ss")
categoryid=DropDownList1.SelectedItem.Value
ds.Merge(ws.GetProducts(log, pass, categoryid))
DataGrid1.DataSource=ds
DataGrid1.DataBind()

End Sub
//C#
private void DropDownList1_SelectedIndexChanged(object sender,
tem.EventArgs e)

{
localhost.Service1
DataSet ds;

ws=new localhost.Service1();

Sys-

511

ds=new DataSet("Categories");
string log;
string pass;
string categoryid;
log=(string) Session["login"];
pass=(string)Session["pass"];
categoryid=DropDownList1.SelectedItem.Value;
short category=System.Convert.ToInt16(categoryid);
ds.Merge(ws.GetProducts (log, pass, category));
DataGrid1.DataSource=ds;
DataGrid1.DataBind();

}
: ws
Web-, ds ,
log pass , categoryid .
log pass
session,

.
categoryid , DropDownList.
ds
Merge. GetProducts
Web-,
, . , ds
Products Northwind.
DataGrid.
DataSource ds .
Web-.
, . ,
, DropDownList .
DataGrid (. 10.46). ,
.

10

512

. 10.46. main.aspx


.

( 10.16).
10.16
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles Button1.Click
Response.Redirect("zakaz.aspx")
End Sub

//C#
private void Button1_Click(object sender, System.EventArgs e)
{

Sys-

513

Response.Redirect("zakaz.aspx") ;

zakaz.aspx, .
, , ,
DataGrid, . ,
.

10,4.3. Web-

Web-
zakaz.aspx, .
1.

Project Add Web Form,


Add New Item.

2.

Template Web Form,


Name "zakaz.aspx"
Open (. 47).

. 10.47. Web-

514

10

. Web-
:
Q DropDownList :
Q DataGrid ( );
Q Button : , , ,
;
Q ,
;
Q , Label .
Web-: GetOrders DropDownList,
GetDetails , , DataGrid. AddOrders DelOrders . D e l t D e t a i l s
. , Web-
, ).
Web- .
1. Web.
2. Properties.
DOCUMENT Property Pages.
Background image , (. 10.48).

. 10.48. DOCUMENT Property Pages

, ,

images
Web-,
,
C:\Inetpub\wwwroot\images\.

515


.
1.

HTML Toolbox
Label.

2.


.
Build Style. Style Builder
.

3.

Label, ,
.

4.

Web Forms Toolbox


DropDownList, DataGrid.

5. DataGrid,
Auto Format.
(, Professional1).
6. DataGrid
.
DataGrid, Property Property
Builder, DataGrid Properties.
7. Columns Available columns
Button Column.
8. Delete
> Selected Columns.
9.

Text , Button
PushButton (. 10.49).

10. Web Forms Toolbox Button. Text : Button1 , Button2 ,


Button3 , Button4 <- .
11. . 10.50.
. ,
DropDownList. , ,
, , DataGrid.

516

10

. 10.49. DataGrid Properties

. 10.50. zakaz.aspx

,
. Page_Load ( 10.17).

517

10,17
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles MyBase.Load

If Not IsPostBack Then


Dim ws As New localhost.Service1()
Dim dsOrders As New DataSet()
Dim dsDetais As New DataSet()
Dim log, pass As String

Dim orderid As String


log=Session.Item("login")
pass=Session.Item("pass")
dsOrders.Merge(ws.GetOrders(log, pass))
DropDownList1.DataSource=dsOrders

DropDownList1.DataMember="Orders"
DropDownList1.AutoPostBack=True
DropDownList1.DataTextField="OrderID"
DropDownList1.DataBind()
orderid=DropDownList1.SelectedItem.Value
dsDetais.Merge(ws.GetDetails(log, pass, orderid))
DataGrid1.DataSource=dsDetais
DataGrid1.DataBind()
Session.Item("OrderID")=orderid

End If
End Sub
//C#
private void Page_Load(object sender, System.EventArgs e)

{
if (! Page.IsPostBack)

{
localhost.Service1 ws=new localhost.Service1();
DataSet dsOrders;
dsOrders=new

DataSet("Orders");

DataSet dsDetais;
dsDetais=new DataSet("[Orders Detais]");
string log;
string pass;

Sys-

518

10
string orderid;
log=(string)

Session["login"];

pass=(string) Session["pass"];
dsOrders.Merge(ws.GetOrders(log, pass));
DropDownList1.DataSource=dsOrders;
DropDownList1.DataMember="Orders";
DropDownList1.AutoPostBack=true;
DropDownList1.DataTextField="OrderID";
DropDownList1.DataBind();
orderid=DropDownList1.SelectedItem.Value;
short order=System.Convert.ToInt16(orderid);
dsDetais.Merge(ws.GetDetails(log, pass, order));
DataGrid1.DataSource=dsDetais;
DataGrid1.DataBind();
Session["OrderID"]=orderid;

}
}

DropDownList if Not IsPostBack, ,


.
: ws Web, dsOrders (), dsDetais ( ), log pass
, orderid .
log pass
session ( ).
dsOrders, Merge.
GetOrders Web, . ,
dsOrders Orders Northwind.
DropDownList dsOrders.
DataSource dsOrders.
DataMember Orders. AutoPostBack true,
. OrderID
DropDownList.

519

, orderid
dsDetais.
GetDetails Web-, , . ,
DataGrid, dsDetais. Session
, .

DropDownList,

DropDownList1_SelectedIndexChanged

.
( 10.18).
10.18
' Visual Basic
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As
tem.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

Sys-

Dim ws As New localhost.Service1()


Dim dsDetais As New DataSet()
Dim log, pass As String
Dim orderid As String
log=Session.Item("login")
pass=Session.Item("pass")
orderid=DropDownList1.SelectedItem.Value
dsDetais.Merge(ws.GetDetails(log, pass, orderid))
DataGrid1.DataSource=dsDetais
DataGrid1.DataBind()
Session.Item("OrderID")=orderid

End Sub

//#
private void DropDownList1_SelectedIndexChanged(object sender,
tem. EventArgs e)

{
localhost.Service1 ws=new localhost.Service1();
DataSet dsDetais;
dsDetais=new DataSet("[Orders Detais]");
string log;

Sys-

520

10
string pass;
string orderid;
log=(string) Session["login"];
pass=(string)Session["pass"] ;
orderid=DropDownList1.SelectedItem.Value;
short order=System.Convert.ToInt16(orderid) ;
dsDetais.Merge(ws.GetDetails(log, pass, o r d e r ) ) ;
DataGrid1.DataSource=dsDetais ;
DataGrid1.DataBind();
Session["OrderID"]=orderid;

: ws Web-, dsDetais
( ), log pass
, orderid .
log pass
session ( ).
orderid
dsDetais.
GetDetails Web-, , . ,
DataGrid, dsDetais. session
.
, . . . ,
. .

( 10.19).
10.19
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'

521

Dim ws s New localhost.Service1( )


Dim dsOrders As New DataSet()
Dim log, pass As String
Dim uspex As Boolean
log=Session.Item("login")
pass=Session.Item("pass")
uspex=ws.AddOrders( log, pass)
If uspex Then
dsOrders. Merge (ws.GetOrders(log, pass) )
DropDownList1.DataSource=dsOrders
DropDownList1.DataBind()
End If
End Sub

//C#
private void Button1_Click(object sender, System.EventArgs e)

{
//

localhost.Service1 ws=new localhost.Service1();


DataSet dsOrders;

dsOrders=new DataSet("Orders");
string log;

string pass;
bool uspex;
log=(string) Session["login"];
pass=(string)Session["pass"];

uspex=ws.AddOrders(log, pass);
if (uspex)
{
dsOrders.Merge(ws.GetOrders(log, pass));
DropDownList1.DataSource=dsOrders;
DropDownList1.DataBind();
}
}

: ws
Web-,
dsOrders (), log pass
, uspex .

522

10

log pass
session,
.
AddOrders Web- ,
. Web- uspex. IF . , dsOrders DropDownList. , .

( 10.20).
10.20
' Visual Basic
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
tem.EventArgs) Handles Button2.Click
'
Dim ws As New localhost.Service1()
Dim dsOrders As New DataSet()
Dim log, pass As String
Dim orderid As String
Dim uspex As Boolean
log=Session.Item("login")
pass=Session.Item("pass")
orderid=DropDownList1.SelectedItem.Value
uspex=ws.DelOrders(log, pass, orderid)
If uspex Then
dsOrders.Merge(ws.GetOrders(log, pass))
DropDownList1.DataSource=dsOrders
DropDownList1.DataBind()
End If
End Sub
//C#

private void Button2_Click(object sender, System.EventArgs e)


{

//'

localhost.Service1 ws=new l o c a l h o s t . S e r v i c e 1 ( ) ;
DataSet dsOrders;

Sys-

523

dsOrders=new DataSet("Orders");
string log;
string pass;
string orderid;
bool uspex;
log=(string) Session["login"];
pass=(string)Session["pass"];
orderid=DropDownList1.SelectedItem.Value;
short order=System.Convert.ToInt16(orderid);
uspex=ws.DelOrders(log, pass, order);
if

(uspex)

dsOrders.Merge(ws.GetOrders(log, pass));
DropDownList1.DataSource=dsOrders;
DropDownList1.DataBind();
}
}

: ws Web-, dsOrders
, log pass , orderid ,
uspex .
log pass
session, .
orderid ( ) DelOrders Web- , , . Web- uspex. IF
. , dsOrders DropDownList. , .
<- ( 10.21),
10.21
' Visual Basic

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


tem. EventArgs) Handles Button4.Click

Sys-

524

10
Response.Redirect("main.aspx")

End Sub
//C#
private void Button4_Click(object sender, System.EventArgs e)

{
Response.Redirect("main.aspx");

<-
main.aspx .
, , ( 10.22).
10.22
' Visual Basic
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
tem. EventArgs) Handles Button3.Click
Response,Redirect("AddProducts.aspx")
End Sub

Sys-

//C#
private void Button3_Click(object sender, System.EventArgs e)
{
Response.Redirect("AddProducts.aspx");
}


AddProducts.aspx, .
.

10.4.4. Web-

Web-
AddProducts.aspx. .
1.

Project Add Web Form,


Add New Item.

2,

525

Template Web Form,


Name AddProducts.aspx
Open.

. Web- :
DropDownList ;
Q

DataGrid ;

Q Button : ;
Q , (
);
Q Label .
Web-:
GetCategories DropDownList, GetProducts
DataGrid, AddDetails
. , Web-
.
Web- .
1. Web.
2. Properties.
DOCUMENT Property Pages.
Background image , .
.
1. HTML Toolbox
Label.

2. . Build Style.
Style Builder
.
3. 2 , ,
.

526

4.

10

Web Forms Toolbox


DropDownList DataGrid.

5. DataGrid,
Auto Format. , (,
Professional1).
6. DataGrid
.
DataGrid, Property Property Builder,
DataGrid Properties.
7.

Columns Available columns


Button Column.

8.

Select > Selected


Columns.

9.

Text , Button
PushButton (. 10.51).

. 10.51. DataGrid Properties

10. Web Forms Toolbox Button. Text : Button1 , Button2 <-


.

527

. . 10.52.

. 10.52. AddProducts.aspx

. ,
DropDownList. , ,
DataGrid.
,
. Page_Load ( 10.23).
10,23
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
tem.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim ws As New localhost.Service1()
Dim ds As New DataSet()
Dim dsProducts As New DataSet()
Dim log, pass As String
Dim categoryid As String
log=Session.Item("login")

Sys-

528

10
pass=Session.Item("pass")
ds.Merge(ws.GetCategories(log, pass))
DropDownList1.DataSource=ds
DropDownList1.DataMember="Categories"
DropDownList1.AutoPostBack=True
DropDownList1.DataTextField="CategoryName"
DropDownList1.DataValueField="CategoryID"
DropDownList1.DataBind()
categoryid=DropDownList1.SelectedItem.Value
dsProducts.Merge(ws.GetProducts(log, pass, categoryid))
DataGrid1.DataSource=dsProducts
DataGrid1.DataBind()

End If
End Sub
//C#
private void Page_Load(object sender, System,EventArgs e)

{
if (! Page.IsPostBack)

{
localhost.Service1 ws=new localhost.Service1();
DataSet ds;
ds=new

DataSet("Categories");

DataSet dsProducts;
dsProducts=new DataSet("Products");
string log;
string pass;
string categoryid;
log=(string)

Session["login"];

pass=(string)Session["pass"];
ds.Merge(ws.GetCategories(log, pass));
DropDownList1.DataSource=ds;
DropDownList1.DataMember="Categories";
DropDownList1.AutoPostBack=true;
DropDownList1.DataTextField="CategoryName";
DropDownList1.DataValueField="CategoryID";
DropDownList1.DataBind();
categoryid=DropDownList1.SelectedItem.Value;

529

short category=Systern.Convert.ToInt16(categoryid);
dsProducts.Merge(ws.GetProducts(log, pass, category));
DataGrid1.DataSource=ds.Products;
DataGrid1.DataBind();
}
}

DropDownList if Not IsPostBack, ,


.
: ws Web-, ds ( ), dsProducts
(), log pass
, categoryid
.
log pass
session
.
ds Merge. GetCategories Web-,
.
, ds Categories Northwind.
DropDownList ds.
DataSource ds.
DataMember

Categories.

AutoPostBack

true . CategoryName
DropDownList.

CategoryID

DropDownList1.DataValueField.

, categoryid
, dsProducts. GetProducts Web-, , . , DataGrid dsProducts.

DropDownList, DropDownList1_SelectedIndexChanged

530

10

. ( 10.24).
10.24
' Visual Basic
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As
tem.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

Sys-

Dim ws As New localhost.Service1()


Dim dsProducts As New DataSet()
Dim log, pass As String
Dim categoryid As String
log=Session.Item("login")
pass=Session.Item("pass")
categoryid=DropDownList1.SelectedItem.Value
dsProducts.Merge(ws.GetProducts(log, pass, categoryid))
DataGrid1.DataSource=dsProducts
DataGrid1.DataBind()
End Sub
//C#

private void DropDownList_SelectedIndexChanged(object sender,


tem.EventArgs e)
{

localhost.Service1

ws=new localhost.Service1();

DataSet dsProducts;
dsProducts=new

DataSet("Products");

string log;
string pass;
string categoryid;
log=(string) Session["login"];
pass=(string)Session["pass"];
categoryid=DropDownList1.SelectedItem.Value;
short

category=System.Convert.ToInt16(categoryid);

dsProducts.Merge(ws.GetProducts(log, pass, category));


DataGrid1.DataSource=dsProducts;
DataGrid1.DataBind();
}

Sys-

531

: ws Web-, dsProducts
, log pass
, categoryid
.
log pass
session ( ).
categoryid , DropDownList.
dsProducts
Merge. GetProducts Web-,
, .
dsProducts products Northwind.
, DataGrid.
DataSource dsProducts
.
, . . . ,
. ,

DataGrid.
DataGrid ( DataGrid) ( 10.25).
| 10.25
' Visual Basic
Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged
TextBox1.Text=1
End Sub
//C#

private void DataGrid1_SelectedIndexChanged(object sender,

10

532
System.EventArgs e)
{

TextBox1.Text="1";
}

TextBox ( , ) 1,
.
( 10.26).
10.26
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
tem.EventArgs) Handles Button1.Click
'
Dim OrderID As Int32
Dim ProductID As Int32
Dim UnitPrice As Double
Dim Quantity As Int16
Dim str As String
Dim ws As New localhost.Service1()
Dim log, pass As String
Dim uspex As Boolean
log=Session.Item("login")
pass=Session,Item("pass")
str=Session.Item("OrderID")
OrderID=CType(str, Int32)
ProductID=CType(DataGrid1.SelectedItem.Cells(1).Text, Int32)
UnitPrice=CType(DataGrid1.SelectedItem.Cells(4).Text,

Double)

Quantity=CType(TextBox1.Text, Int16)
uspex=ws.AddDetails
(log, pass, OrderID, ProductID, UnitPrice, Quantity)
End Sub
//C#

private void Button1_Click(object sender, System.EventArgs e)


{

int OrderID;

Sys-

533

int ProductID;
double UnitPrice;

short Quantity;
string str;

localhost.Service1 ws=new localhost.Service1();


string log;

string pass;
bool uspex;

log=(string) Session["login"];
pass=(string)Session["pass"];

str= (string) Session["OrderID"];


OrderID=System.Convert.ToInt16 (str);
ProductID=System.Convert.ToInt16(DataGrid1.SelectedItem.

Cells[1].Text);
UnitPrice=System.Convert.ToInt16(DataGrid1.SelectedItem.
Cells[4].Text);
Quantity=Systein.Convert.ToInt16(TextBox1.Text);
uspex=ws.AddDetails(log, pass, OrderID, ProductID,
UnitPrice, Quantity);

:
(OrderID
, ProductID
, UnitPrice ,
Quantity , str ). ,
: ws - - Web, log pass
, uspex
.
log, pass str ,
session.
,
( )
AddDetails Web-, . ,
.
Web- uspex.
<- ( 10.27).

534

10

10.27
' Visual Basic
Private Sub Button2_Click(ByVal sender System.Object, ByVal e As
tem. EventArgs) Handles Button2.Click

Sys-

Response.Redirect("Zakaz.aspx")
End Sub
//C#

private void Button2_Click(object sender, System.EventArgs e)


{

Response.Redirect("Zakaz.aspx");
}


Zakaz.aspx .
Zakaz.aspx
. DeleteCommand DataGrid.
:
Q Visual Basic DataGrid,
DeleteCommand;

Q # DataGrid
Properties, Events
,
DeleteCommand DataGrid1_DeleteCommand.

DataGrid1_DeleteCommand,
( 10.28).
10.28
' Visual Basic
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal
e As System.Web.UI.WebControls.DataGridCommandEventArgs)
DataGrid1.DeleteCommand
'
Dim ws As New localhost.Service1()
Dim dsDetais As New DataSet 0

Handles

535

Dim log, pass As String


Dim OrderID As Int32
Dim ProductID As Int32
Dim uspex As Boolean
Dim index As Integer
index=e.Item.ItemIndex
log=Session.Item("login")
pass=Session.Item("pass")
OrderID=CType(DataGrid1.Items(index).Cells(1).Text,

Int32)

ProductID=CType(DataGrid1.Items(index).Cells(2).Text,
uspex=ws.DelDetails(log, pass, OrderID, ProductID)
If uspex Then
OrderID=DropDownList1.SelectedItem.Value
dsDetais.Merge(ws.GetDetails(log, pass, OrderID))
DataGrid1.DataSource=dsDetais
DataGrid1.DataBind()

End If
End Sub
//C#
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{
//
localhost.Service1 ws=new localhost.Service1();
DataSet dsDetais;
dsDetais=new DataSet("[Orders

Detais]");

string log;
string pass;
int OrderID;
int ProductID;
bool uspex;
int index;
index=e.Item.ItemIndex;
log=(string) Session["login"];
pass=(string)Session["pass"];
OrderID=System.Convert.ToInt16(DataGrid1.Items
Cells[1].Text);

[index].

Int32)

536

10
ProductID=System.Convert.ToInt16(DataGrid1.Items[index].
Cells[2].Text);
uspex=ws.DelDetails(log, pass, OrderID, ProductID);
if (uspex)
{

OrderID =
System.Convert.ToInt16(DropDownList1,SelectedItem.Value);
dsDetais.Merge(ws.GetDetails (log, pass, OrderID));
DataGrid1.DataSource=dsDetais;
DataGrid1.DataBind();
}
}

: ws
Web-, dsDetais , log pass
.
: (OrderID , ProductID
), uspex
, index
DataGrid, ,
log, pass session, OrderID ProductID
DataGrid, .
DelDetails Web-
. ,
. Web-
uspex. IF , ,
, Web- GetDetails DataGrid,
.

10.4.5,
, , ,
. , -

537

. ,
, . , (. 10.53).

. 10.53. zakaz.aspx

, ,
, ,
(. 10.54).

. 10.54. Add Products.aspx

538

10

, Web-, .
(
) ( , ).
,
. . . ,
Web-.
, , Web. ,
Web-,
Web-.
, , ,
DataGrid,
, . .
,

.
Web-,
.NET .
, http://www.santayrauto.ru. , .
MS SQL Server 2000,
Visual Studio .NET Visual Basic.