Title: Data Access with ADO.NET
1(No Transcript)
2?????? ?? ?????? ADO.NET
???????????? ?? .NET Framework
http//www.nakov.com/dotnet/
???????? ?????
????? ??????
C MVP, MCSD.NET, MCDBA, MCT?????????? ????????,
BSH Ltd.
???????? ?? ????????? ?????????, ???
??????? ?????
???? (academy.devbg.org)
?????? ????????
InterConsult Bulgaria
3?????????? ??????
- ?????? ???????? ?? .NET Framework
- ?????? ???????? ?? ????? C
- ?????? ???????? ?? XML ??????????
- ???????? ?? ?????????? ???? ?? ?????
- ???????? ?? ????? SQL
4??????????
- ?????????? ???? ?? ?????
- ????????? ? SQL Server
- ?????? ?? ????? ? ADO.NET
- ??????? ?????
- ????????? ?????
- ??????????? ??? ???????????? ? ???? ?? ?????
5?????????? ???? ?? ????? ?????????? (1)
- ?????? ?? ?????? ?? ?????
- ???????????? ???? ?? ?????
- RDBMS ???????
- ???????, ??????, ????????????? ?? ????????, E/R
???????? - ????????????
- ??????????? (constraints)
- ???????
6?????????? ???? ?? ????? ?????????? (2)
- ?????? SQL
- ????????? ????????? ? ?????? (stored procedures)
- ??????? (views)
- ??????? (triggers)
- ?????????? ? ????????
7?????????? ???? ?? ?????
- ?????? ?? ?????? ?? ?????
- ?????????? (??????????)
- ???????
- ?????????? (????????)
- ???????-??????????
- ???????????? ???? ?? ?????
- ????????????? ??????????? ?? ??????? ? ??????
????? ??? (???????) - ??????? ?????? ????????????? ?????? ????????????
???????
8??????? ?? ?????????? ?? ??
- ??????? ?? ?????????? ?? ?????????? ???? ?? ?????
(????) Relational Database Management System
(RDBMS) - ?????????? ????????? ???????? ??
- ????????? / ??????? / ????????? ?? ??????? ?
?????? ????? ??? - ????????, ???????, ?????????, ??????? ? ?????????
?? ????? ?? ????????? - ????????? ?? ????? SQL
- ?????????? ?? ?????????? (??????????????)
9??????? ?? ?????????? ?? ??
- RDBMS ????????? ?? ??????? ???
- ??????? ?? ?????????? ?? ???? ?? ?????
- ??? ?????? "Database ???????"
- ??-???????? RDBMS ???????
- Microsoft SQL Server
- Oracle Database
- IBM DB2
- PostgreSQL
- MySQL
- Borland Interbase
10???????
- ????????? ????????????? ?????????? ?? ?????????,
????????? ? ?????? ? ??????. ?????? (???????
PERSONS) - ???????? ???? ??????? ?????????
- ???????? ???? ??? ? ??? (?????, ???????? ???,
???? ??? ??.)
id name family employer
1 ??????? ????? ????
2 ???????? ????? BSH
3 ?????? ????? CodeAttest
11????? ?? ???????
- ????? ?? ??????? ? ???????? ???????????????? ??
???????? ?? ?????? (??? ? ???) - ???????? ????????? PERSONS ??? ???????? ?????
PERSONS ( id ?????, name ???????? ???,
family ???????? ???, employer ???????? ??? )
12???????? ????
- ?????????? ???? (primary key) ? ?????? ??
?????????, ????? ???????? ???????????? ????? ????
??? - ??? ?????? (????) ?? ???????? ?????? ?? ????????
?????????? ?? ??????? - ?????????? ???? ???? ?? ? ?????????? ?? ???????
??????
id name family employer
1 ??????? ????? ????
2 ???????? ????? BSH
3 ?????? ????? CodeAttest
Primary key
13?????? (???????)
- ???????? ????? ????????? ?? ??????? ??
??????????????? primary key / foreign key
Foreign key
Primary key
Primary key
TOWN
id name country_id
1 ????? 1
2 ??????? 1
3 ?????? 2
4 ?????? 2
5 ?????? 3
COUNTRY
id name
1 ????????
2 ????????
3 ?????
14?????? (???????)
- ???????? ???? (foreign key) ? ????? ?? ?????
(primary key) ? ????? ??????? - ???????? ????????? ??????????? ?? ??????????
- ? ??????? ????? ?? ????????? ?? ?? ??????? ??
????? ???? - ???????? ???? ????????????? (multiplicity)
- 1 x 1 ???????? ????? / ??? ?? ?????
- 1 x ????? ??????? / ???????
- ????? x ????? ??????? / ?????? ????
15????????????? ?? ????????
- ?????? 1 x 1
- 1 ????? ?? ?????? ??????? ??????????? ?? ????? 1
????? ?? ??????? ??????? - ????? ?? ???????? ???? ??????
TOWN
POPULATION
id name popul_id
1 ????? 1
2 ??????? 2
3 ?????? 3
4 ?????? 4
5 ?????? 5
id population
1 1 177 000
2 720 000
3 1 260 000
4 3 400 000
5 11 800 000
16????????????? ?? ????????
- ?????? 1 x ????? (??? ????? x 1)
- 1 ????? ?? ??????? ??????? ??????????? ?? ?????
?????? ?? ??????? ??????? - ???????? ?? ????? ?????
TOWN
id name country_id
1 ????? 1
2 ??????? 1
3 ?????? 2
4 ?????? 2
5 ?????? 3
COUNTRY
id name
1 ????????
2 ????????
3 ?????
17????????????? ?? ????????
- ?????? ????? x ?????
- 1 ????? ?? ??????? ??????? ??????????? ?? ?????
?????? ?? ??????? ??????? ? ????????? - ????????? ?? ???? ???????? ???????
STUDENT_COURSE
STUDENT
COURSE
student_id course_id
1 1
1 2
3 2
3 3
4 2
id name
1 ????
2 ?????
3 ????
4 ?????
id name
1 .NET
2 Java
3 PHP
18?????????? ?????
- ?????????? ????? ?? ?? ???????? ???????????? ??
- ??????? ?? ?????? ???????
- ???????? ????? ?????????
- ???????????? ????? ?????? ??????????? ?? ??
- ?? ??????? ?????, ? ???? ?????????
- ???????????? ????? ?? ??????????? ???????? ????
Entity/Relationship ???????? (E/R Diagrams)
19E/R ???????? ??????
?????????? ? ????????? ? Microsoft SQL Server
Enterprise Manager
20E/R ???????? ??????
?????????? ? ????????? ? PLATINUM ERwin
21E/R ???????? ??????
?????????? ? ????????? ? fabFORCE DB Designer
22??????????? ?? E/R ??????
- E/R ?????????? ?? ???????? ? ??????????? ??
?????????? ?? ????? (Data Modeling Tools) - Microsoft Visio
- Oracle Designer
- Computer Associates ERwin
- SQL Server Enterprise Manager
- IBM Rational Rose
- theKompany Data Architect
- fabForce DBDesigner (GNU GPL ?????? ? ??????? ???
?? Windows ? Linux)
23????????????
- ?????????????? ?? ???????????? ????? ????????
??????????? ?? ????? - ????????????????? ????? ???????? ?????
??????????. ????????
??????? ???????-????? ???? ????????? ??????? ????
?????? ????? ?????? ??? 0.67 ?????????? ????? ??????????? "?????" ?????
???? "????????" ????? "??????" 0.55 ?????????? ????? ??????????? "?????" ?????
???? "???????" ??????? ?? 0.58 ????????. ??????? ???????? "24 ????" ?????
???? "Tuborg" ???????? ???? ?? 0.67 ????????. ??????? ???????? "24 ????" ?????
24????????????
- 1-?? ???????? ?????
- ??????? ???? ???????? ???
- ???????? ? ???????? ?? ???????? (????????)
????????? - ???? ?????????? ?? ????? ? ??????? ?? ???? ???
- ????????? ? ???????? ???? ?? ????? ???????
????? ISBN (PK) ????? ?????_email
.NET Framework 3847028437 ??? ???? bai-kiro_at_abv.bg
Beginning SQL 7234534450 ???? ???? dedo_at_mraz.org
25????????????
- 2-?? ???????? ?????
- ??????? ???????????? ?? 1-?? ???????? ?????
- ? ????????? ???? ??????, ???????? ?? ???? ??
????????? ???? (??? ? ???????? ?? ??????? ??????)
E-mail-?? ?????? ?? ??????
?????? ?????? ?? ???????
????? (PK) ????? (PK) ???? ?????_email
.NET Framework ??? ???? 37.25 bai-kiro_at_abv.bg
Beginning SQL ???? ???? 19.95 dedo_at_mraz.org
26????????????
- 3-?? ???????? ?????
- ??????? ???????????? ?? 2-?? ???????? ?????
- ???????????? ??????????? ????? ???????? ??
"?????? ?????? ?? ????????? ????"
id ??????? ????????????_id ???? ?????????_id ???????_id ????_id
1 ?????? ????? 2 0.67 2 4 1
2 ???? "????????" 3 0.55 2 4 1
3 ????? "????????" 6 4.38 5 2 1
4 ???? "Tuborg" 4 0.67 4 1 3
27????????????
- 4-?? ???????? ?????
- ??????? ???????????? ?? 3-?? ???????? ?????
- ? ????????? ??? ???-????? ???? ??????, ?????????
??????? ? ??????? ???????? ?????????
(multi-valued attribute) ?? ???? ????
???? ????? ??? ????? ?????
???? ????? ??? ????? ??????
?????_id ????? ??????
2 .NET Programming Regular Expressions in .NET
4 Mastering J2EE Best Practices in J2EE
28????????????
- ?????? ?? ????????????? ????? (? 4-?? ????????
?????)
PRODUCT
id ??????? ????????????_id ???? ?????????_id ???????_id ????_id
1 ?????? ????? 2 0.67 2 4 1
2 ???? "????????" 3 0.55 2 4 1
3 ????? "????????" 6 4.38 5 2 1
4 ???? "Tuborg" 4 0.67 4 1 3
VENDOR
CATEGORY
STORE
TOWN
id ???
2 "?????" ???
4 "???????" ??
id ???
4 ????
2 ??????????
id ???
1 Billa
4 METRO
id ???
1 ?????
3 ?????
29??????????? (Constraints)
- ????????????? (constraints) ??????? ???????, ??
???????, ????? ?? ????? ?? ????? ?????????? - ??????????? ?? ???????? ???? (primary key
constraint) - ?????????? ???? ? ???????? ?? ????? ?????
- ??????????? ?? ???????? ???? (unique key
constraint) - ??????????? ? ?????? ?????? (??? ????? ??????) ??
????????
30??????????? (Constraints)
- ??????????? ?? ?????? ???? (foreign key
constraint) - ?????????? ? ?????? ?????? ? ???? ?? ?????
??????? - ??????????? ?? ???????? (check constraint)
- ??????????? ? ?????? ?????? ?????????? ??????
??????? - ????????
- (hourgt0) AND (hourlt24)
- name upper(name)
31???????
- ????????? ????????? ????????? ?? ??????? ??
???????? ? ?????? ?????? ??? ????? ?? ?????? - ??????? ?? ??? ?????? ???????
- ?????????? ?? ???-????? ? B-??????? ???
???-??????? - ????? ?? ????? ?????? (????? ?????????) ???
???????? - ?????????? ? ??????????? ?? ??????????? ??????? ?
??-?????
32?????? SQL
- SQL (Structured Query Language)
- ?????????????? ???????????? ???? (????????) ??
??????????? ?? ?????????? ???? ?? ????? - SQL-92 ????????? ?? ?????? RDBMS
- SQL-99 ??????? ??? ??????
- SQL ????????
- ?????????, ???????, ????????? ?? ??????? ? ?????
?????? ? ?? - ???????, ?????????, ????????, ??????? ? ?????????
?? ?????
33?????? SQL
- SQL ?? ?????? ??
- DDL Data Definition Language
- ??????? CREATE, ALTER, DROP
- DML Data Manipulation Language
- ??????? SELECT, INSERT, UPDATE, DELETE
- ?????? ?? SQL SELECT ??????
SELECT Town.name, Country.name FROM Town,
Country WHERE Town.country_id Country.id
34Stored procedures
- ????????? ?? ???? ???? (???????? ?????????,
stored procedures) - ????????? ???, ????? ?? ????????? ? ????? ??????
?? ???? ????? - ??????? ????? ??-????? ?? ?????? ???
- ??????? ?? ??????? ????????
- ????? ?? ??????? ?????????
- ????? ?? ?????? ????????
- ???????? ????????
- ?????????? ?? ?????? (record set)
35Stored procedures
- ????????? ?? ???? ???? ?? ????? ?? ????,
?????????? ?? SQL - T-SQL ? Microsoft SQL Server
- PL/SQL ? Oracle
- ?????? ?? ????????? ?? T-SQL
CREATE PROCEDURE sp_GetInventory _at_location
varchar(10)AS SELECT Product, Quantity FROM
Inventory WHERE Warehouse _at_location
36??????? (views)
- ????????? ????????????? ????????? SQL SELECT
??????, ????? ?? ????????? ???? ??????? - ????????? ???????? ?? ?????? SQL ??????
- ???????? ?? ?? ???? ????????? ?? ???????????
- ?? ????? ?????????? ?? ?? ????? ????? ??? ?????
??????? - ????? ?? ?? ????? ???? ??? ????? ???????
(???????????? ?? ???????)
37??????? (views) ??????
T_COMPANY
T_TOWN
T_COUNTRY
id company town_id
1 ????? ??? 1
2 BulkSoft Inc. 2
3 ???????? ?? 4
4 ??????? ?? 3
id town country_id
1 ????? 1
2 New York 3
3 ?????? 2
4 ??????? 1
id country
1 ????????
2 ?????
3 ???
- CREATE VIEW V_BG_COMPANY AS
- SELECT
- T_COMPANY.id AS id,
- T_COMPANY.company AS company
- FROM T_COMPANY INNER JOIN
- (T_TOWN INNER JOIN T_COUNTRY ON
- T_TOWN.country_idT_COUNTRY.id)
- ON T_COMPANY.town_idT_TOWN.id
- WHERE
- T_COUNTRY.country"????????"
V_BG_COMPANY
id company
1 ????? ???
3 ???????? ??
38??????? (triggers)
- ????????? (triggers) ?? ????????? ?? ???? ????,
????? ?? ????????? ??? ??????? ???????, ???????? - ??? ???????? ?? ?????
- ??? ??????? ?? ?????
- ??? ????????? ?? ?????
- ????????? ????? ?? ????????? ????????????
????????? ?? ??????? - ??????? ?? ??????? ??? ????????
- ????????? ?? ?????? ? ???????
39??????? ??????
- ????? ??????? ? ????? ?? ?????
- ??????, ????? ??? ???????? ?? ???? ????? ??????
"Ltd." ??? ????? ?
CREATE TABLE COMPANY( id int NOT NULL, name
varchar(50) NOT NULL)
CREATE TRIGGER trg_COMPANY_INSERT ON COMPANY
FOR INSERT AS UPDATE COMPANY SET name name
' Ltd.' WHERE id (SELECT id FROM inserted)
40??????????
- ???????????? ?? ????????????????? ?? ????????
(?????? ??? ?????? ?????), ????? ?? ??????????
???????? - ??? ?? ?????????? ???????? ???????? ?????? (????
???? ????) - ??? ????? ?? ?????????? ?? ?? ????????? ??????
- ??????
- ??????? ??????????? ?? ???? ?? ???? ?????? ?
????? (??????? ???????) - ??? ????????? ??? ????????? ?? ?????? ????????,
??????? ?????? ????????
41???????????? ?? ????????????
- ???????????? ? ???????? RDBMS ??????? ???? 4
????????????, ?????? ????? ?? ??????? ACID
?????????? - Atomicity ??????????
- ????????? ?? ?????? ??? ????
- Consistency ?????? ?? ???????
- ?????? ?????? ?????? ????????????
- Isolation ???????? ?? ???????
- ????????? ?????????? ?? ????????? ???? ?? ?????
?? ?? ?????? - Durability ?????????? ?? ???????
- ??? ???? ?????????? ???? ??????????, ?? ?? ????
?? ???? ????????
42?????????? ??????
- ????? ??????? ? ??????? ??????
- ?????????? ?????????? ??? ???????? ?? ???? ??
?????? ?? ??????
CREATE TABLE ACCOUNT( id int NOT NULL,
balance decimal NOT NULL)
CREATE PROCEDURE sp_Transfer_Money( _at_from_acc
int, _at_to_acc int, _at_ammount decimal )
AS (???????? ??????????)
43?????????? ??????
BEGIN TRANSACTION UPDATE ACCOUNT set balance
balance - _at_ammount WHERE id _at_from_acc IF
_at__at_rowcount ltgt 1 BEGIN ROLLBACK TRANSACTION
RAISERROR ('Invalid source account!', 16, 1)
RETURN END UPDATE ACCOUNT set balance balance
_at_ammount WHERE id _at_to_acc IF _at__at_rowcount ltgt 1
BEGIN ROLLBACK TRANSACTION RAISERROR
('Invalid destination account!', 16, 1)
RETURN END COMMIT TRANSACTION
44?????????? ? ????????
- ???????????? ????? ?? ????????? ???? ?? ????????
(isolation levels) - ??-??????? ???????? ????????? ??-?????
??????????????, ?? ?????? ??-????? ? ????????
??????? ?? ??-?????
???? ?? ???????? ?????? ?? ????????-???? ????? ???????-??????? ??? ?????? ???????? ??????
Read uncommitted ?? ?? ??
Read committed ?? ?? ??
Repeatable read ?? ?? ??
Serializable ?? ?? ??
45???????? ?? ??????????
- ???? ?? ??????? ???????????
- ??????, ?????? ?? ???? ?????? ???????? ??
??????????? ?????? ?? ?????? ?? ???? ??????? - ??????
- ???????? ?? ??? ?????????? ? ????????? ?
????????????? - ???????? ?? ????? ?? ????? ?????????? ? ?????????
? ??????? - ??? ?????????? ?? ????? ?? ?????, ?????? ?? ??
?????? ? ?????????? ?????????? (?? ?? ???????
?????? ??????????)
46????????? ? MS SQL Server ??????????
- ??????? ?? SQL Server
- ?????????? ?? SQL Server 2000
- ????????? ????? ? ???????????
- ????????? ? T-SQL
- Data Definition Language (DDL) ???????
- Data Manipulation Language (DML) ???????
- Database Console Commands (DBCC) ???????
- ????????? ?????????
- ?????????? ? SQL Server
47??????? ?? SQL Server
- ??????? ?? ???????????? ?????
- ?????? ?? SQL Server
- ?????? ?? SQL Server ?????????? ???
System.Data.SqlClient - SQL Server 7.0
- SQL Server 2000
48???????? ?????????? ?? SQL Server 2000 ??????
- MSSQLSERVER ?????? ??????
- ????????? ?????? ?? SQL Server ?????? ????? ??
??????????? ?? ???????????? - SQLSERVERAGENT SQL Server Agent
- ????????? SQL ???????, ???????? ??????????
???????? ? ???????? ?? ???????? - MSSQLServerADHelper
- ????? ?? ?????????? ? Active Directory
- MSSQLServerOLAPService
- ????? ?? OLAP ?????? ?? ?????
49???????? ?????????? ?? SQL Server 2000
???????????
- Enterprise Manager
- ???????? ????????????? ?? SQL ??????
- ?????????? ?? ???? ????? (?????????, backup,
??????????????, ?????????????) - ?????????? ?? ?????? ? ?????? (???????, ???????,
???????, ?????????, ) - Query Analyzer
- ?????????? ? ?????? ?? SQL ??????
- DTS (Data Transformation Services)
- ?????????, ????????????? ? ??????????? ?? ?????
?? ? ??? ?????? ?????????
50???????? ?????????? ?? SQL Server 2000
???????????
- SQL Profiler
- ???????????? ?? ??????? ? ??????????? ??
?????????????????? ?? SQL Server - SQL XML
- ??????????? ?? ????? ? Web ????? (? Internet
Information Services IIS) - ????????? SQL ?????? ? ????? XML
- Analysis Manager
- ????????? ?? Data Warehousing (OLAP)
- ??????? ?????????? ?? ?????????? ?????????? ??????
51???????????? ?? SQL Server
- ????????? ????? ? ???????????
- Visual Studio .NET 2002/2003
- SQL Servers ? Server Explorer-?
- ?????? ? ???????, ???????, ???????, ?????????,
???????? - ????????? / ????????? / ??????? ? ????????? ??
stored ????????? - DB ???????
- Query Analyzer
- ?????? ??? ????????? ?????????
- ?????????? ?? SQL ??????
- ??????????? ?? SQL ??????
52???????????? 1
- ?????? ? ??????????? Query Analyzer ?? MS SQL
Server
53???????????? 2
- Visual Studio .NET ? ??????????? ?? ?? SQL Server
- ?????? ??? Server Explorer
- ?????? ? ???????
- ????????? ?? DB Project
- ???????????? ?? T-SQL (????????? ????????? ???
VS.NET 2003)
54????????? ? T-SQL
- ?????? ???????
- Data Definition Language (DDL)
- ????????? ? ?????????? ?? ???????? ? ?????? ??
????? (???????, ???????, ???????, ...) - Data Manipulation Language (DML)
- ?????? ? ??????? ? ????????, ????????? ? ??????
?? ????? (?????????, ???????, ...) - DataBase Console Commands (DBCC)
- ???????? ????????? ?????????
55Data Definition Language (1/2)
- MS SQL Server 2000 ???????? SQL-92 DDL, ??
??????? ? ?????????? ?????????? - ??????? ?? ?????????? ? ?????? ?
- ???? ?? ?????
- ???????, ???????, ???????, constraints
- ???????
- ????????? ?????????
- ?????
56Data Definition Language (2/2)
- ?????? ???????
- ?????????? / ??????????? ?? ??????
- CREATE
- ALTER
- DROP
- ??????? ?? ??????
- GRANT
- DENY
- REVOKE
57?????? DDL ???????
- ????????? ?? ??????
- CREATE
58?????? DDL ???????
- ??????? / ?????????? ?? ??????
- ALTER
59?????? DDL ???????
- ????????? ?? ??????? ? ??????
60?????? DDL ???????
61?????? DDL ???????
- ???????? ?? ????? ?? ??????
- GRANT ????????? ??????
- DENY ????????? ??????
- REVOKE ?????? ?????????? ?? ???????? GRANT ?
DENY ???????
62???????????? 3
- DDL ??????? ? SQL Server 2000
63Data Manipulation Language
- SELECT
- INSERT
- UPDATE
- DELETE
SELECT FROM Users
INSERT INTO Users (FirstName, LastName, Email)
VALUES ('Branimir', 'Giurov', 'branimir_at_nospam.bg
')
UPDATE Users SET Email'branimir_at_nospam.bg' WHERE
UserID 1
DELETE Users WHERE UserID1
64DML ???????
- SELECT ??????? ?????
- ?????????
- ??????
SELECT select_listINTO new_table_nameFROM
table_listWHERE search_conditionsGROUP BY
group_by_listHAVING search_conditionsORDER
BY order_list ASC DESC
SELECT FirstName, LastName, Mobile as GSM FROM
Users ORDER BY LastName
65?????????? ???????
- COUNT() ????? ???? ??????
- SUM() ???? ?? ??????
- AVG() ????????????????? ???????? ?? ??????
- MAX() ?????????? ???????? ?? ??????
- MIN() ????????? ???????? ?? ??????
SELECT COUNT(LastName) FROM Users WHERE LastName
Ivanov'
66?????????? (JOIN) ?? ???????
- ????? ? ???????????
- ??????????? ?? ?????? ?? ??? ??? ?????? ??????? ?
????????? ?? ??? ????????? ???????, ???
?????????? ?? ????????? SELECT - ?????? ??????????
- ???????? ?????????? (INNER JOIN)
- ?????? ?????????? (OUTER JOIN)
- ?????????? ?????????? (CROSS JOIN)
67???????? ?????????? (INNER JOIN)
- ????? ???????? ?? ??? ???????, ???? ??? ?? ????
???????????? - ??????? ?????? ? ???????????? ??
- ??????????? ?????????? ? INNER JOIN
SELECT a.Name, si.Quantity, si.SalePrice FROM
SaleItem si INNER JOIN Article a ON
si.ArticleID a.ArticleID
SELECT a.Name, si.Quantity, si.SalePrice FROM
SaleItem si, Article a WHERE si.ArticleID
a.ArticleID
68?????? ?????????? (OUTER JOIN) (1/3)
- ???? ?????? ?????????? (LEFT OUTER JOIN)
- ????? ?????? ?????? ?? ??????? ??????? ????
??????????????? ?? ?????? ?? ??????? ??????? - ??? ??? ??????? ??????? ???? ?????????????
??????, ??????? ?????? ?? ?????? ??? ????????
NULL
SELECT a.Name, si.Quantity, si.SalePrice FROM
Article a LEFT OUTER JOIN SaleItem si ON
a.ArticleID si.ArticleID
69?????? ?????????? (OUTER JOIN) (2/3)
- ????? ?????? ?????????? (RIGHT OUTER JOIN)
- ????? ?????? ?????? ?? ??????? ??????? ????
??????????????? ?? ?????? ?? ??????? ??????? - ??? ? ??????? ??????? ???? ????????????? ??????,
??????? ?????? ?? ?????? ??? ???????? NULL
SELECT a.Name, si.Quantity, si.SalePrice FROM
SaleItem si RIGHT OUTER JOIN Article a ON
si.ArticleID a.ArticleID
70?????? ?????????? (OUTER JOIN) (3/3)
- ????? ?????? ?????????? (FULL OUTER JOIN)
- ????? ?????? ?????? ?? ????? ???????, ?? ?????
??? ???????????? - ??? ??? ?? ???????? ???????? ?? ????? ???????, ??
????? ???? ???????????? - ?????????? ????? ?? ???????? ? NULL
SELECT a.Name, m.Text FROM Author a FULL OUTER
JOIN Message m ON a.AuthorID m.AuthorID
71?????????? ?????????? (CROSS JOIN)
- ????? ?????????? ?? ???????? ?? ?????????,
????????? ? ????????????
SELECT fn.Name FirstName, ln.Name LastName FROM
FirstName fn CROSS JOIN LastName ln
72DML ???????
- INSERT ?????? ?????? ? ???????
- ?????????
- ??????
INSERT INTO (ColumnList) VALUES (ValuesList)
INSERT Users (FirstName, LastName, Phone,
Mobile, Email) VALUES ('Branimir',
'Giurov', '797461', '359 88 792 5209',
'branimir_at_abv.nospam.bg')
73DML ???????
- UPDATE ???????? ??????
- ?????????
- ??????
UPDATE table_name SET colname1value1,
colname2value2 WHERE condition
UPDATE Users SET Email'branimir_at_dir.nospam.bg' WH
ERE UserID 118
74DML ???????
- DELETE ??????? ?????? ?? ???????
- ?????????
- ??????
DELETE FROM TableName WHERE Condition
DELETE FROM Users WHERE UPPER(LastName)
'GIUROV' AND Mobile LIKE '359'
75???????????? 4
- D?L ??????? ? SQL Server 2000
76DBCC ??????? ? SQL Server
- DBCC Database Console Commands
- ???????????? ?? ?????????? ?? ??????????? ??
?????????? - ?????????
- ?????????
- ?? ?????? ????? ???? ?? ?????
- ??????? ? ??????? ?????
- ???????? ?? ???????
- ????????? ?? ???????? ?? ???????
- ?? ?????? ???? ?????
- ?? ???????, ?????? ? ??.
77??-????? DBCC ???????
- ?? ?????????
- DBCC DBREINDEX
- DBCC INDEXDEFRAG
- DBCC SHRINKDATABASE
- DBCC SHRINKFILE
- DBCC UPDATEUSAGE (sysindexes)
- ?? ???????? ?? ???????
- DBCC OPENTRAN
- DBCC INPUTBUFFER
- DBCC OUTPUTBUFFER
- DBCC PROCCACHE
- DBCC SQLPERF ( LOGSPACE )
- DBCC USEROPTIONS
78??-????? DBCC ???????
- ?? ?????????
- DBCC CHECKALLOC
- DBCC CHECKCATALOG
- DBCC CHECKCONSTRAINTS
- DBCC CHECKDB
- DBCC CHECKFILEGROUP
- DBCC CHECKIDENT
- DBCC CHECKTABLE
- ?????
- DBCC DllName(FREE)
- DBCC HELP
- DBCC PINTABLE
- DBCC TRACEON / TRACEOFF
- DBCC UNPINABLE
79???????????? 5
- DBCC ??????? ? SQL Server 2000
80???????????? 6
- ?????????????? ?? MS SQL Server SQL Server
Books Online
81????????? ?????????
- ????????? ????????? (Stored Procedures)
- ????????? ? ????? ?? ??????
- ???????? ? ????????????? ?????????
- ????????? ?? ????????? sp_ ? usp_
- ????????? ????????? ????????? (Extended Stored
Procedures) - ????????? ? ????? ?? ??????
- ????????? ?? ????????? xp_
82????????? ?????????
CREATE PROC procedure_name _at_parameter
data_type VARYING default OUTPUT
    ,...n AS sql_statement ...n
CREATE PROCEDURE sp_TopExpensiveProducts
_at_count int AS SET ROWCOUNT _at_count SELECT
ProductName, UnitPrice FROM Products ORDER BY
UnitPrice DESC
83????????? ????????? ??????
CREATE PROC sp_InsertAuthor ( _at_au_fname
varchar(50), _at_au_lname varchar(50) ) AS INSER
T Authors (au_fname, au_lname, active) VALUES
(_at_au_fname, _at_au_lname, 1) DECLARE
_at_inserted_au_id bigint SET _at_inserted_au_id
(SELECT _at__at_identity) SELECT _at_inserted_au_id GO
84???????????? 7
- ?????????, ?????????? ? ????????? ?? ?????????
????????? ? VS.NET
85??????????
- ?????????? ??????? ??????, ????? ?????? ?? ????
????????? ???????? - ??? ????????? ??????, ???????????? ?? ???????
(rollback) ? ????????? ?? ?? ??????? - ?????? ??????????
- ??????? ???????? ?? SQL Server
- ???????????? ??????????? ?? DTC (Distributed
Transaction Coordinator) - ???? ?? ???????? ?? ????????????
- ???????? ?????????? ?? ??????? ????? ?????????
??????????
86?????????? ? SQL Server
- ??????? ?? ?????? ? ??????????
- BEGIN TRANSACTION (??? BEGIN TRAN)
- ??????? ???? ??????????
- COMMIT TRANSACTION
- ??????????? ???????? ??????????
- ROLBACK TRANSACTION
- ??????? ???????? ??????????
- SET TRANSACTION ISOLATION LEVEL
- ?????? ?????? ?? ???????? (READ COMMITTED, READ
UNCOMMITTED, REPEATABLE READ, SERIALIZABLE)
87?????????? ? SQL Server ??????
CREATE PROCEDURE sp_InsertUser ( _at_userName
varchar(50), _at_userPassHash varchar(50),
_at_groupId bigint ) AS BEGIN TRAN INSERT
INTO Users (UserName, UserPassHash) VALUES
(_at_userName, _at_userPassHash) IF (_at__at_error 0)
INSERT UsersGroups (UserId, GroupId)
VALUES (_at__at_identity, _at_groupId) IF (_at__at_error
0) COMMIT TRAN ELSE
ROLLBACK TRAN
88???????????? 8
89???????????? 9
- ???? ?? ???????? ?? ????????????
90????????? ?? ???? ?????
- ?????? ?? ??? ?????????? ?? ?????? ?????????? ???
???????, ????? ?? ?? ???????? - ?????? ?? ????????? ?? SQL Server ???? ?? ?????
- ???? ?????????? ? ??????????????
- ???? ???????? ? ????????
91????????? ???? ?????????? ? ?????????????? (1/5)
- 1. ?? Enterprise Manager ???????? ?????? ????? ?
?? ???????????? ???? ???????? Backup Database
92????????? ???? ?????????? ? ?????????????? (2/5)
- 2. ?????? ??? ?? ?????? ? ???????? ????, ??????
?? ?? ??????? ????????? ?????
93????????? ???? ?????????? ? ?????????????? (3/5)
- 3. ?? ??????? ???????? ???????? Restore Database
?? Enterprise Manager
94????????? ???? ?????????? ? ?????????????? (4/5)
- 4. ???????? ?????, ??? ????? ?? ?? ??????????
?????? ?? ?????
95????????? ???? ?????????? ? ?????????????? (5/5)
- 5. ???????? ??????, ?? ????? ?? ?? ??????????
?????? ?? ?????
96????????? ???? ???????? ? ???????? (1/3)
- 1. ? Enterprise Manager ???????? ?????? ?? ?????
? ?? ???????????? ???? ???????? Detach Database
97????????? ???? ???????? ? ???????? (2/3)
- 2. ???????? ????????? ?? ?????? ?? ????? ??
???????-???????? ?? ???????-????????. - ??????? ?? ?????? ?? ?????
- lt???_??_????gt.mdf
- lt???_??_????_loggt.ldf
98????????? ???? ???????? ? ???????? (3/3)
- 3. ???????? ?????? ?? ????? ?? ???????-????????
99ADO.NET ?????????? (1)
- ?????? ?? ?????? ? ???????
- ??????? ? ?????????
- ???????? ?? ????????????
- ??????????? ?? ADO.NET
- Data Providers
- ?????? ? MS SQL Server
- SqlConnection
- ?????????? ?? ??????? ?????
- SqlCommand, SqlDataReader
- ???????????? ??????
100ADO.NET ?????????? (2)
- ?????? ? ????? ???? ?? ????? ???? OLE DB
- ?????????? ?? ????????? ?????
- ?????? DataSet, ?????-?????????? DataSet-?
- ????????? DataTable ? DataRelation
- ?????? ????? ADO.NET ? XML
- ?????? DataView
- ?????????? ?? DataAdapter
- ??????? ???????? ?? ?????? ? ????? ? ??????????
?????
101?????? ?? ?????? ? ?????
- ??????? ????? (connected model)
- ????????? ?????? ? ??????? (online)
- ?????? ?? ??????????
- ???????? ???? ????????????
- ????????? ????? (disconnected model)
- ???????? ? ??????? ?? ??????????? offline ??
????????? ?? ????? ? ???????? ?? ??????? ??
??????? - ?????? ?? ??????????
- ???????
- ?????? ?? ????? ???? Web ??????
- ?????????? ? XML
102??????? ?????
- ?????????? ?? ?????? ?? ????? ? ?????, ? ?????
?????? ??? ?????? ?? ????????? ?? ???????
103??????? ????? ?? ? ??????
- ??????????
- ??????? ? ??-????? ?? ????????????? (??-?????
?????? ?? ????????????) - ????????? ????? ????????????? ?????? ?? ?????????
??-????? - ??-????? ?????????? ?? ?????? ? ???????? ??????
?? ??????? - ???????????
- ????? ?? ????????? ??????? ??????
- ???????? ??? ????? ?? ????????????
104????????? ?????
- ???????????? ?? ??????? ?? ??????????? ??????? ??
?????????? ?? ??????? ?? ?????? ??????? ???
???????
105????????? ????? ?? ? ??????
- ??????????
- ???????? ?? ???????, ?????? ??? ?????, ? ?
?????????? ????? ?????? ??? ?????? ? ???????????
???? ?? ????? - ????? ??????????? ????? ?? ?? ????????
????????????? - ?????????????? ? ????? ?????
- ???????????
- ??????? ?? ?????? ?? ??????
- ???????????? ?????? ?? ???????? ?? ???????????
????? ?????????? ??????
106???????? ?? ????????????
- ?????????? ??????????
- ???-????? ?????? ???? ???? ??????????
- ??????????
- ?????? ?????????? ?? ?? ???? ?????
- ???????????
- ??????? ?? ???????????????? ??????? ?????????????
- ??????
- ?????????? ???????? ?? MS Access
107???????? ?? ????????????
- ????????? ?????????? (??????-??????)
- ??????????????? ????????? ? ?????? ????????? ??
????????? ?? ???? ????? - ??????? ?? ?????????? ??? ?????? ????
- ??????????
- ??? ????????? ?? ????????????????
- ???????????
- ???? ???????????? ???????? ? ??????????? ??
????? ???? ??????? - ???????
- MS SQL Server ? MS Query Analyzer
- MS Exchange ? MS Outlook
108???????? ?? ????????????
- ????????? ??????????
- ?????????? ?????? ?????????????? ?? ? ????????
?????? - ??????????
- ???????? ?? ???????????????? ????? ?????????????
?????????, ?????? ??????? ? ?????????? / ??????
?? ??????? - ???????????
- ??-?????? ?????????
- ?????? ?????? ?? ??????????? ?? ???????????
- ??????
- ASP.NET Web-?????????? ? ASP.NET Web ?????? ? MS
SQL Server
109???????? ?? ????????????
- ??????????? ??????????
- ??????? ? ?????? ?? 3 ????????? ????
- ?????????? ?? ???????? ?? ??? ?????? ??
??????????? ?? ???????????????? - ??????????
- ?????????? ?? ???????? ?????????? ?? ?????????
????? ?? ???????????????? ???? ???????? ?????????
- ???????????
- ????? ?????? ?????? ?? ?????????? ? ?????????? ??
??????? ?? ??????? ?????? - ???????? ?????? ????????? ? ??-?????? ??????? ??
??????????
110? ??????? ?? ???????
????????????, ????????????
111????? ? ADO.NET?
- ????? ?? ??????? ?? ?????? ? ?????
- ????? ?? ???????, ??????????, ????????? ? ?????
?????? ?? ?????? ?? ????? ???? ?????? .NET
???????? ?????????? - ????????? ????? ?? ?????? ? ?????
- ????????? ?????????? ?? ?????? ? ?????????? ?????
- ????????? ?????? ? XML
- ????????? ?? ADO (Windows ?????????? ?? ?????? ??
???? ?? ?????)
112Namespace-? ?? ADO.NET
- ???????????? ?? ????? ?? ADO.NET
- System.Data ??????? ???????????? ??????? ??
ADO.NET - System.Data.Common ???? ??????? ?? ?????? data
Provider-? - System.Data.SqlClient ? System.Data.SqlTypes
Data Provider ??????? ?? ?????? ?? SQL Server - System.Data.OleDb ?????? ? OleDB
- System.Data.Odbc ?????? ? ODBC
- System.Xml ?????? ? XML
113???????? ?? ADO ??? ADO.NET
ADO.NET
XxxConnection
Connection
XxxTransaction
XxxCommand
Command
Recordset
DataSet
XxxDataReader
XxxDataAdapter
114?????????? ?? ADO.NET
Disconnected model
Connected model
DataSet
DataReader
Command
DataAdapter
ODBC .NET Data Provider
SQL Server .NET Data Provider
OleDb .NET Data Provider
Oracle .NET Data Provider
115Data Provider-? ? ADO.NET
- Data Provider-??? ?? ??????????? ?? ???????,
????? ?????????? ?????? ? ???????? ???? ?? ????? - ?? ?????????? RDBMS ??????? ?? ????????? ????????
Data Provider-? - ?????????? ????????????? ????????? ????????
????????? ?? ?????? ??? ????????? ?? ????? - ????????? ?? ?? 4 ??????? ??????
- Connection ?? ?????? ? ??????
- Command ?? ?????????? ?? SQL
- DataReader ?? ????????? ?? ?????
- DataAdapter ?? ?????? ? DataSet
116Data Provider-? ? ADO.NET
- ? ADO.NET ??? ??????? ?????????? Data Provider-a
- SqlClient ?? ?????? ??? SQL Server
- OleDB ?? ?????? ??? ????????? OleDB
- Odbc ?? ?????? ??? ????????? ODBC
- Oracle ?? ?????? ? Oracle
- ????? ?????????? ????????? Data Provider-? ??
?????? ? ????? RDBMS - IBM DB2
- MySQL
- PostgreSQL
- Borland Interbase / Firebird
117ADO.NET ? ???????? ?????
SqlDataReader
- ??????? ?? ?? ??????? ?? ????????? ?? ????????
- ???????? ?? ?????? (SqlConnection)
- ?????????? ?? ??????? / ??????? (SqlCommand)
- ????????? ?? ???????? ???????? ???? ???????? ??
???????? ???? ????? (SqlDataReader) - ????????? ?? ??????
- ????????? ?? ????????
SqlCommand
SqlConnection
118ADO.NET ? ?????????? ?????
- ??????? ?? ??????? ? DataSet ????? ? ???????? ??
????????????? - ???????? ?? ?????? (SqlConnection)
- ??????? ?? DataSet (???? SqlDataAdapter)
- ????????? ?? ????????
- ?????? ??? DataSet-a
- ???????? ?? ??????
- ???????? ?? ??????? ?? ??????? ?? ???????
- ????????? ?? ????????
DataSet
SqlDataAdapter
SqlConnection
119ADO.NET, XML ? Web ??????
- ADO.NET ? ????? ??????????? ? XML
- ????? ?? ???????? ? ????????? ????????
??????????? Web ?????? - Web-???????? ????????? ?????? ???? ?? ???????????
?????????? - ???????? ?????? ?????????? ??? ???????
120SqlClient Data Provider
- SqlConnection ??????????? ???????? ? MS SQL
Server - SqlCommand ????????? ??????? ????? SQL Server-?
???? ???? ?????????? ?????? - SqlDataReader ????? ?? ????????? ?? ????? ??
SQL Server-? - ??????? ?? ???????? ?? ????????? ???????
- SqlDataAdapter ?????? ????? ????? DataSet
?????? ? SQL Server - ????????? ????????? ?? DataSet ? ????? ?
?????????? ?? ????????? ????? - ???? ?? ?? ????? ??? ?? ??????????? ?? ???????? ?
?????? ?????
121??????? ?? ????????? ? SQL Server 2000
- ?????? ????????????
- Windows (???? ?????????? ? ??)
- ??????? ????????? ? ??????????
- ????????? ?? ??????
- ???????? ?? ??????
- ?????????? ?? ???????
- ??????? (Windows ? SQL Server)
- ??????? ?? ???????????? ? ???????? ??????
- ????? ??????? ? ?????????
122???????? ??? ?? ?????? ? ???? ?? ?????
(Connection String)
- ???????? ???????????, ?????????? ?? ?????????????
?? ?????? ? ?????? ????? - ??????? ?????????
- Provider ??? ?? ???????? ?? ??????
- Data Source ????????????? ?? ??????
- User ID/Password
- Integrated Security
- Persist Security Info (False)
- ??????
Serverlocalhost DatabasePubs Integrated
Securitytrue Persist Security Infofalse
123SqlConnection ??????
const string CONNECTION_STRING
"Serverlocalhost DatabaseNorthwind "
"Integrated Securitytrue " "Persist
Security Infofalse" // Create the
connection SqlConnection con new
SqlConnection(CONNECTION_STRING) using (con)
// Open connection con.Open() // Use
the connection here // ...
124Connection Pooling
- "Database Connection Pooling" ? ?????????
???????, ????? - ????????? ??-????????? ?????????? ?? ???????? ???
?????? ?? ????? - ????????? ??????????????????
- ???????? ?????? ????
- ??? ???????? ?? ?????? ?? ?? ????? ?????? ?? ?.
???. "???" (connection pool) - ??? ??? ???? ???????? ?? ?????? ????
- ??? ????????? ?? ?????? ?? ?? ????? ? ???? ??? ??
?? ???????? ????????? - ?? ???????????? SqlClient ?????? ???
125??? ?? ???????? ??? SqlConnection
- ??????????? ???????? ? ????????? ?? ????????
(?????? ? ????) - Open() ? Close() ????????
- ?????????? ???????? ? ????????? ?? ????????
(?????? ? ????) - ???????? ? ???? ??? ?????? ? DataAdapter-?
- ?????????? ?? Dispose() ??????
- ??????? ?? ??? ???????? using (con)
- ????? ???????? ? connection pool-?
126????????? ?? SqlConnection
- ??????? ?????????? ?? ????????
- StateChange ? InfoMessage
- StateChangeEventArgs ??????
- ???? ?????????? ?? ???? ????? ?? ? ??????? ?
???????? ??? ?????? ????? - CurrentState ? OriginalState
- SqlInfoMessageEventArgs
- Errors SqlErrorCollection ???????? ?? ?????????
?? ?????? ? ?????????????? - Message ??????????? ?? ??????
- Source ???? ? ?????????? ????????/??????????????
??
127StateChange ??????
private const string CONNECTION_STRING
"Server." " DatabasePubs Integrated
Securitytrue" private SqlConnection
mConn private void InitConnection() mConn
new SqlConnection(CONNECTION_STRING)
mConn.StateChange new
StateChangeEventHandler(ConnStateChange)
mConn.Open() private void ConnStateChange(objec
t sender, StateChangeEventArgs e)
Debug.WriteLine("SQL Server connection "
e.OriginalState.ToString() " --gt "
e.CurrentState.ToString())
128InfoMessage ??????
private const string CONNECTION_STRING
"Server. " "DatabasePubs Integrated
Securitytrue" private SqlConnection
mConn private void InitConnection() mConn
new SqlConnection(CONNECTION_STRING)
mConn.InfoMessage new
SqlInfoMessageEventHandler(ConnInfoMessage)
mConn.Open() private void ConnInfoMessage(objec
t sender, SqlInfoMessageEventArgs e)
Debug.WriteLine("SQL Server message "
e.Message " Source " e.Source)
129???????????? 10
- ???????????? ?? ????????? StateChange ?
InfoMessage
130?????????? ?? ??????? ?????
SqlDataReader
- ??????? ?? ?? ??????? ?? ????????? ?? ????????
- ???????? ?? ?????? (SqlConnection)
- ?????????? ?? ??????? / ??????? (SqlCommand)
- ????????? ?? ???????? ???????? ???? ???????? ??
???????? ???? ????? (SqlDataReader) - ????????? ?? ??????
- ????????? ?? ????????
SqlCommand
SqlConnection
131????????? ? ???????? ?????
132?????? SqlCommand
- ????????? ????????? ??? SQL ?????? ??? ?????????
????????? - ??-????? ????????
- Connection ????? / ?????? SqlConnection-? ??
????????? - CommandType ??? ???????
- CommandType.StoredProcedure
- CommandType.TableDirect
- CommandType.Text
- CommandText SQL ?????? ??? ??? ?? ?????????
????????? - Parameters ?????????
133?????? SqlCommand
- ??-????? ??????
- ExecuteScalar()
- ????? ???????? ???????? (??????? ?????? ?? ??????
??? ?? ?????????) - ????????? ???????? ? System.Object
- ExecuteReader()
- ????? ?????? (SqlDataReader)
- CommandBehavior ?????? ?????????
- ExecuteNonQuery()
- ????? ???? ?? ??????????? ?????? (int)
- ExecuteXmlReader()
- ????? XmlReader ?? ?????????
- ???????? ?? ???? ? SqlClient
134?????? SqlDataReader
- ??????? ???????????????? ?? ?????? (??????)
???????? ?? ????????? ??????? - ???????? ? ???? ?? ?????? (read-only)
- ???????? ? ??????????? (forward-only)
- ??-????? ?????? ? ????????
- Read() ????????? ??????? ?????? ? ????? false
??? ???? ??????? ????? - Item (??????????) ??????? ?????????? ?? ??????
?? ??? ??? ?????? - Close() ??????? ??????? ?????? ????????????
?? ?? ????!
135??????? ????? ??????
using System using System.Data using
System.Data.SqlClient class TestSqlCommand
private const string CONNECTION_STRING
"Server." " Databasepubs Integrated
Securitytrue" private const string
COMMAND_SELECT_AUTHORS "SELECT au_fname,
au_lname, phone FROM authors" static void
Main() SqlConnection con new
SqlConnection(CONNECTION_STRING)
con.Open() try (???????? ??????????)
136??????? ????? ??????
SqlCommand command new
SqlCommand(COMMAND_SELECT_AUTHORS, con)
SqlDataReader reader command.ExecuteReader()
using (reader) while
(reader.Read()) string
firstName (String) reader"au_fname"
string lastName (String) reader"au_lname"
string phone (String) reader"phone"
Console.WriteLine("0 1 - 2",
firstName, lastName, phone)
finally con.Close()
137???????????? 11
- ?????????? ?? ??????? ????? SqlCommand ?
SqlDataReader
138????????? ?? SqlCommand
- ?????? ?? ????????? SqlCommand ????? ?? ???????
?????? - ?????????
- ?? Server Explorer ??? VS.NET
- ?? Toolbox ??? VS.NET
SqlCommand cmd new SqlCommand( "SELECT
FROM Products, con)
139???????????? 12
- ????????? ?? SqlCommand ??? VS.NET ???? Server
Explorer ? ???? Data ???????????? ?? Toolbox-?
140?????? SqlParameter
- ????? ????????????? SqlParamer-????
- SQL ???????? ? ????????? ????????? ????? ?? ????
??????? ? ???????? ????????? - ?? ?? ????????? ???? Parameters ?????????? ??
SqlCommand ????? - ??-????? ????????
- ParameterName ??? ?? ??????????
- DbType ??? (NVarChar, Timestamp, )
- Size ?????? ?? ???? (??? ???)
- Direction ??????, ???????, ...
141???????????? ?????? ??????
private void InsertShipper(string aName, string
aPhone) SqlCommand cmdInsertShipper new
SqlCommand( "INSERT INTO
Shippers(CompanyName, Phone) " "VALUES
(_at_Name, _at_Phone)", dbConnection) SqlParameter
paramName new SqlParameter("_at_Name",
SqlDbType.NVarChar) paramName.Value
aName cmdInsertShipper.Parameters.Add(paramNa
me) SqlParameter paramPhone new
SqlParameter("_at_Phone", SqlDbType.NVarChar)
paramPhone.Value aPhone cmdInsertShipper.Pa
rameters.Add(paramPhone) cmdInsertShipper.Exe
cuteNonQuery()
142???????? ???? ?????????
- ??????????? ?? ???????????-????????? ????????
???? ? ?????????? ?? ????? database ?????? - ??? SQL Server ? MS Access ?? ????????? Identity
? AutoNumber ?????? - ?????????? ?? ?? ?????? ??? ????????
- ?????? ?? ????????? ?? ???????????-???????????
???????? ???? ?? ADO.NET
SELECT _at__at_Identity
SqlCommand cmdSelectIdentity new
SqlCommand("SELECT _at__at_Identity", dbCon) decimal
insertedRecordId (decimal)
cmdSelectIdentity.ExecuteScalar()
143???????????? 13
- ?????? ? ???????????? SQL ??????
- ????????? ?? ????????? ?? ?????
- ???????? ?? ????????? ? ????????? ?? ???????????
?? ???? ???????? ????
144?????????? ?? ??????????
- ?????? ? ?????????? ? SQL Server
BEGIN TRANSACTION DECLARE _at_orderDetailsError
int, _at_productError int DELETE FROM "Order
Details" WHERE ProductID42 SELECT
_at_orderDetailsError _at__at_ERROR DELETE FROM Products
WHERE ProductID42 SELECT _at_productError
_at__at_ERROR IF _at_orderDetailsError 0 AND
_at_productError 0 COMMIT TRANS ELSE
ROLLBACK TRANS
145?????????? ?? ??????????
- ?????? ? ?????????? ? ADO.NET
- ????????? ?? ??????????
- ????????? ?? ??????? ? ?????? ??????????
- ????????????? / ????????? ?? ??????????
SqlTransaction trans dbConnection.BeginTrans
action()
command.Transaction trans
trans.Commit() trans.Rollback()
146?????????? ?? ??????????
- ?????? ?? ???????? ?? ???????? ? ????????????
IsolationLevel - ???? ?? ???????? ?? SqlTransaction
- ReadUncommited
- ReadCommited
- RepeatableRead
- Serializable
- Unspecified
- ??????
SqlTransaction trans dbConnection.
BeginTransaction(IsolationLevel.Serializable)
147???????????? 14
148?????? ? ????? ???? ?? ?????
- ADO.NET ???????? ?????? ? ???????? ???? ?? ?????
(????? SQL Server) ???? ????????? Data Providers - OLE DB ???????? ?? ??????????
- Oracle ???????? ?? ??????????
- MySQL ????????? ?? ????????????
- PostgreSQL ????????? ?? ????????????
- ??????????? ?? ????????????
- IDbConnection
- IDbCommand, IDataParameter
- IDataReader
- IDbDataAdapter
149OLE DB Data Provider
- OleDbConnection ??????????? ?????? ? OLE DB
???????? ?? ????? - OleDbCommand ????????? SQL ??????? ????? OLE DB
?????? ??? ???? ????? - OleDbParameter ????????? ?? ???????
- OleDbDataReader ?? ????????? ?? ????? ??
???????, ????????? ???? OLE DB - OleDbDataAdapter ?????? ????? ????? DataSet
?????? ? OLE DB ???? ?????
OleDbConnection dbConn new OleDbConnection(
_at_"ProviderMicrosoft.Jet.OLEDB.4.0Data
SourceC\MyDB.mdbPersist Security InfoFalse")
150?????? ? OLE DB ??????
- ????? ???? ????? C\Library.mdb, ????????? ? MS
Access - ? ??? ??? ??????? Users
- ?????????? "Microsoft Jet 4.0 Provider" ?? ??????
?? ADO.NET ???? OLE DB - ????????? Connection String ???? OleDbConnection
???????????? ?? Toolbox-a ?? VS.NET
ProviderMicrosoft.Jet.OLEDB.4.0Data Source
C\Library.mdbPersist Security InfoFalse
151?????? ? OLE DB ??????
OleDbConnection dbConn new OleDbConnection(
_at_"ProviderMicrosoft.Jet.OLEDB.4.0Data Source"
_at_"C\Library.mdbPersist Security
InfoFalse") dbConn.Open() OleDbCommand cmd
new OleDbCommand( "INSERT INTO Users
(username, password) " "VALUES (_at_user,
_at_pass)", dbConn) cmd.Parameters.Add("_at_user",
OleDbType.VarChar).Value "new user
name" cmd.Parameters.Add("_at_pass",
OleDbType.VarChar).Value "secret password" int
affected cmd.ExecuteNonQuery() Console.WriteLi
ne("0 records were inserted", affected)
152???????????? 15
- ?????? ? MS Access ???? OLE DB
153???????? ?????? ? ????
- ?????????
- ??????????? ?????????? ?????? ?? ?????? ?????, ?
????? ????????, ? ?? ?????? - ????? ???? ?? ????? ????????? ?????? ?? ???? ???
?? ???? - ? MS SQL Server 2000 ??? datetime (8 ?????) ?
smalldatetime (4 ?????) - ??? ?????? ? ????? ?? ??? ????, ???????????
?????? ???? ?? ???????????? ??? ???????????
154???????? ?????? ? ????
- ??????????? System.DateTime ??????????? ?? ??????
? ???? ? .NET - ??????????? ???????????? ?????? ?? ????????? ??
???? ??? ?????? ????? - ??? ????? ?? ???????????? ??????????
IFormatProvider ?? ?????????? ?? ????????? ??
???????????? - ??? ???????????? ??????????? ????????? ????????
????????? CultureInfo.InvariantCulture
155?????? ? ???? ??????
- CREATE TABLE Messages
- (
- MsgId int identity not null primary key,
- MsgText nvarchar(1000),
- MsgDate datetime - Dont use varchar for
dates! - )
- public void AddMsg(string aText, DateTime aDate)
-
- SqlCommand cmdInsertMsg new SqlCommand(
- "INSERT INTO Messages(MsgText, MsgDate) "
- "VALUES (_at_MsgText, _at_MsgDate)", mDbCon)
- (???????? ??????????)
156?????? ? ???? ??????
SqlParameter paramMsgText new
SqlParameter( "_at_MsgText",
SqlDbType.NVarChar) paramMsgText.Value
aText cmdInsertMsg.Parameters.Add(paramMsgTex
t) SqlParameter paramMsgDate new
SqlParameter( "_at_MsgDate",
SqlDbType.DateTime) paramMsgDate.Value
aDate cmdInsertMsg.Parameters.Add(paramMsgDat
e) cmdInsertMsg.ExecuteNonQuery()
157???????????? 16
- ?????? ? ???? ? MS SQL Server
158?????? ? ???????? ? ??
- ?? ??????????? ?? ???????? ??????????? ? ???? ??
????? ?? ????????? ??????? ?????? - ??? "image" ? MS SQL Server
- ??? "blob" ? Oracle
- ??? "OLE Object" ? MS Access
- ??? ?????? ??????? ?????? ?? ?? ???????? ?????? ?
??????
159???????????? 22
- ?????????? ?? ???????? ??????????? ? ???? ?? ?????
160ADO.NET ? ?????????? ?????
DataSet
- ??????? ?? ??????? ? DataSet ????? ? ???????? ??
????????????? - ???????? ?? ?????? (SqlConnection)
- ??????? ?? DataSet (???? SqlDataAdapter)
- ????????? ?? ????????
- ?????? ??? DataSet-a
- ???????? ?? ??????
- ???????? ?? ??????? ?? ??????? ?? ???????
- ????????? ?? ????????
SqlDataAdapter
SqlConnection
161?????? ? ?????????? ?????
- ??????? ????????, ? ????? ?? ???????? ???????? ?
?????????? ????? - ??????? ??????????? ??????? ?? ???????
- DataSet
- ????????? ???? ??????? ???????, ???????,
constraints ? ????? ?????? - DataTable ??????? ???????
- XxxDataAdapter ??????????? ??????? ?? ???????
???? XxxCommand, XxxConnection ??????? - DataRelation ?????? ????? ???????
162DataSet ??????? ?????
- ???????? ? DataSet
- Tables ??????? ?????????
- Relations ??????? ????????? ????? ?????????
- ????????? ?? ??????????? ?