Title: Data Access with ADO.NET
1(No Transcript)
2?????? ?? ?????? ADO.NET
???????????? ?? .NET Framework
http//www.devbg.org/dotnetcourse
?????? ????????
InterConsult Bulgaria, Ltd.
3???????? ????????
- ???????? ?????
- C MVP, MCSD.NET, MCDBA, MCT
- ?????????? ????????, BSH Ltd.
- ??????? ?????
- ???? (academy.devbg.org)
- ?????? ????????
- InterConsult Bulgaria
- ????? ??????
- ???????? ?? ?????????
- ?????????, ???
4?????????? ??????
- ?????? ???????? ?? .NET Framework
- ?????? ???????? ?? ????? C
- ???????? ?? ?????????? ???? ?? ?????
- ???????? ?? ????? SQL
5??????????
- ?????????? ???? ?? ?????
- ????????? ? SQL Server
- ?????? ?? ????? ? ADO.NET
- ??????? ?????
- ????????? ?????
- ??????????? ??? ???????????? ? ???? ?? ?????
6?????????? ???? ?? ????? ?????????? (1)
- ?????? ?? ?????? ?? ?????
- ???????????? ???? ?? ?????
- RDBMS ???????
- ???????, ??????, ????????????? ?? ????????, E/R
???????? - ????????????
- ??????????? (constraints)
- ???????
7?????????? ???? ?? ????? ?????????? (2)
- ?????? SQL
- ????????? ????????? ? ?????? (stored procedures)
- ??????? (views)
- ??????? (triggers)
- ?????????? ? ????????
8?????????? ???? ?? ?????
- ?????? ?? ?????? ?? ?????
- ?????????? (??????????)
- ???????
- ?????????? (????????)
- ???????-??????????
- ???????????? ???? ?? ?????
- ????????????? ??????????? ?? ??????? ? ??????
????? ??? (???????) - ??????? ?????? ????????????? ?????? ????????????
???????
9??????? ?? ?????????? ?? ??
- ??????? ?? ?????????? ?? ?????????? ???? ?? ?????
(????) Relational Database Management System
(RDBMS) - ?????????? ????????? ???????? ??
- ????????? / ??????? / ????????? ?? ??????? ?
?????? ????? ??? - ????????, ???????, ?????????, ??????? ? ?????????
?? ????? ?? ????????? - ????????? ?? ????? SQL
- ?????????? ?? ?????????? (??????????????)
10??????? ?? ?????????? ?? ??
- RDBMS ????????? ?? ??????? ???
- ??????? ?? ?????????? ?? ???? ?? ?????
- ??? ?????? "Database ???????"
- ??-???????? RDBMS ???????
- Microsoft SQL Server
- Oracle Database
- IBM DB2
- PostgreSQL
- MySQL
- Borland Interbase/Firebird
11???????
- ????????? ????????????? ?????????? ?? ?????????,
????????? ? ?????? ? ??????. ?????? (???????
PERSONS) - ???????? ???? ??????? ?????????
- ???????? ???? ??? ? ??? (?????, ???????? ???,
???? ??? ??.)
id name family employer
1 ??????? ????? ????
2 ???????? ????? BSH
3 ?????? ????? CodeAttest
12????? ?? ???????
- ????? ?? ??????? ? ???????? ???????????????? ??
???????? ?? ?????? (??? ? ???) - ???????? ????????? PERSONS ??? ???????? ?????
PERSONS ( id ?????, name ???????? ???,
family ???????? ???, employer ???????? ??? )
13???????? ????
- ?????????? ???? (primary key) ? ?????? ??
?????????, ????? ???????? ???????????? ????? ????
??? - ??? ?????? (????) ?? ???????? ?????? ?? ????????
?????????? ?? ??????? - ?????????? ???? ???? ?? ? ?????????? ?? ???????
??????
id name family employer
1 ??????? ????? ????
2 ???????? ????? BSH
3 ?????? ????? CodeAttest
Primary key
14?????? (???????)
- ???????? ????? ????????? ?? ??????? ??
??????????????? 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 ?????
15?????? (???????)
- ???????? ???? (foreign key) ? ????? ?? ?????
(primary key) ? ????? ??????? - ???????? ????????? ??????????? ?? ??????????
- ? ??????? ????? ?? ????????? ?? ?? ??????? ??
????? ???? - ???????? ???? ????????????? (multiplicity)
- 1 x 1 ???????? ??????? / ???????
- 1 x ????? ??????? / ???????
- ????? x ????? ??????? / ?????? ????
16????????????? ?? ????????
- ?????? 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
17????????????? ?? ????????
- ?????? 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 ?????
18????????????? ?? ????????
- ?????? ????? 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
19?????????? ?????
- ?????????? ????? ?? ?? ???????? ???????????? ??
- ??????? ?? ?????? ???????
- ???????? ????? ?????????
- ???????????? ????? ?????? ??????????? ?? ??
- ?? ??????? ?????, ? ???? ?????????
- ???????????? ????? ?? ??????????? ???????? ????
Entity/Relationship ???????? (E/R Diagrams)
20E/R ???????? ??????
?????????? ? ????????? ? Microsoft SQL Server
Enterprise Manager
21E/R ???????? ??????
?????????? ? ????????? ? PLATINUM ERwin
22E/R ???????? ??????
?????????? ? ????????? ? fabFORCE DB Designer
23??????????? ?? 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)
24????????????
- ?????????????? ?? ???????????? ????? ????????
??????????? ?? ????? - ????????????????? ????? ???????? ?????
??????????. ????????
??????? ???????-????? ???? ????????? ??????? ????
?????? ????? ?????? ??? 0.67 ?????????? ????? ??????????? "?????" ?????
???? "????????" ????? "??????" 0.55 ?????????? ????? ??????????? "?????" ?????
???? "???????" ??????? ?? 0.58 ????????. ??????? ???????? "24 ????" ?????
???? "Tuborg" ???????? ???? ?? 0.67 ????????. ??????? ???????? "24 ????" ?????
25????????????
- 1-?? ???????? ?????
- ??????? ???? ???????? ???
- ???????? ? ???????? ?? ???????? (????????)
????????? - ???? ?????????? ?? ????? ? ??????? ?? ???? ???
- ????????? ? ???????? ???? ?? ????? ???????
????? ISBN (PK) ????? ?????_email
.NET Framework 3847028437 ??? ???? bai-kiro_at_abv.bg
Beginning SQL 7234534450 ???? ???? dedo_at_mraz.org
26????????????
- 2-?? ???????? ?????
- ??????? ???????????? ?? 1-?? ???????? ?????
- ? ????????? ???? ??????, ???????? ?? ???? ??
????????? ???? (??? ? ???????? ?? ??????? ??????)
E-mail-?? ?????? ?? ??????
?????? ?????? ?? ???????
????? (PK) ????? (PK) ???? ?????_email
.NET Framework ??? ???? 37.25 bai-kiro_at_abv.bg
Beginning SQL ???? ???? 19.95 dedo_at_mraz.org
27????????????
- 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
28????????????
- 4-?? ???????? ?????
- ??????? ???????????? ?? 3-?? ???????? ?????
- ? ????????? ??? ???-????? ???? ??????, ?????????
??????? ? ??????? ???????? ?????????
(multi-valued attribute) ?? ???? ????
???? ????? ??? ????? ?????
???? ????? ??? ????? ??????
?????_id ????? ??????
2 .NET Programming Regular Expressions in .NET
4 Mastering J2EE Best Practices in J2EE
29????????????
- ?????? ?? ????????????? ????? (? 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 ?????
30??????????? (Constraints)
- ????????????? (constraints) ??????? ???????, ??
???????, ????? ?? ????? ?? ????? ?????????? - ??????????? ?? ???????? ???? (primary key
constraint) - ?????????? ???? ? ???????? ?? ????? ?????
- ??????????? ?? ???????? ???? (unique key
constraint) - ??????????? ? ?????? ?????? (??? ????? ??????) ??
????????
31??????????? (Constraints)
- ??????????? ?? ?????? ???? (foreign key
constraint) - ?????????? ? ?????? ?????? ? ???? ?? ?????
??????? - ??????????? ?? ???????? (check constraint)
- ??????????? ? ?????? ?????? ?????????? ??????
??????? - ????????
- (hourgt0) AND (hourlt24)
- name upper(name)
32???????
- ????????? ????????? ????????? ?? ??????? ??
???????? ? ?????? ?????? ??? ????? ?? ?????? - ??????? ?? ??? ?????? ???????
- ?????????? ?? ???-????? ? B-??????? ???
???-??????? - ?????????? ? ??????????? ?? ??????????? ??????? ?
??-?????
33?????? SQL
- SQL (Structured Query Language)
- ?????????????? ???????????? ???? (????????) ??
??????????? ?? ?????????? ???? ?? ????? - SQL-92 ????????? ?? ?????? RDBMS
- SQL-99 ??????? ??? ??????
- SQL ????????
- ?????????, ???????, ????????? ?? ??????? ? ?????
?????? ? ?? - ???????, ?????????, ????????, ??????? ? ?????????
?? ?????
34?????? 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
35Stored procedures
- ????????? ?? ???? ???? (???????? ?????????,
stored procedures) - ????????? ???, ????? ?? ????????? ? ????? ??????
?? ???? ????? - ??????? ????? ??-????? ?? ?????? ???
- ??????? ?? ??????? ????????
- ????? ?? ??????? ?????????
- ????? ?? ?????? ????????
- ???????? ????????
- ?????????? ?? ?????? (record set)
36Stored procedures
- ????????? ?? ???? ???? ?? ????? ?? ????,
?????????? ?? SQL - T-SQL ? Microsoft SQL Server
- PL/SQL ? Oracle
- ?????? ?? ????????? ?? T-SQL
CREATE PROCEDURE spGetInventory _at_location
varchar(10)AS SELECT Product, Quantity FROM
Inventory WHERE Warehouse _at_location
37??????? (views)
- ????????? ????????????? ????????? SQL SELECT
??????, ????? ?? ????????? ???? ??????? - ????????? ???????? ?? ?????? SQL ??????
- ???????? ?? ?? ???? ????????? ?? ???????????
- ?? ????? ?????????? ?? ?? ????? ????? ??? ?????
??????? - ????? ?? ?? ????? ???? ??? ????? ???????
(???????????? ?? ???????)
38??????? (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 ???????? ??
39??????? (triggers)
- ????????? (triggers) ?? ????????? ?? ???? ????,
????? ?? ????????? ??? ??????? ???????, ???????? - ??? ???????? ?? ?????
- ??? ??????? ?? ?????
- ??? ????????? ?? ?????
- ????????? ????? ?? ????????? ????????????
????????? ?? ??????? - ??????? ?? ??????? ??? ????????
- ????????? ?? ?????? ? ???????
40??????? ??????
- ????? ??????? ? ????? ?? ?????
- ??????, ????? ??? ???????? ?? ???? ????? ??????
"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)
41??????????
- ???????????? ?? ????????????????? ?? ????????
(?????? ??? ?????? ?????), ????? ?? ??????????
???????? - ??? ?? ?????????? ???????? ???????? ?????? (????
???? ????) - ??? ????? ?? ?????????? ?? ?? ????????? ??????
- ??????
- ??????? ??????????? ?? ???? ?? ???? ?????? ?
????? (??????? ???????) - ??? ????????? ??? ????????? ?? ?????? ????????,
??????? ?????? ????????
42???????????? ?? ????????????
- ???????????? ? ???????? RDBMS ??????? ???? 4
????????????, ?????? ????? ?? ??????? ACID
?????????? - Atomicity ??????????
- ????????? ?? ?????? ??? ????
- Consistency ?????? ?? ???????
- ?????? ?????? ?????? ????????????
- Isolation ???????? ?? ???????
- ????????? ?????????? ?? ????????? ???? ?? ?????
?? ?? ?????? - Durability ?????????? ?? ???????
- ??? ???? ?????????? ???? ??????????, ?? ?? ????
?? ???? ????????
43?????????? ??????
- ????? ??????? ? ??????? ??????
- ?????????? ?????????? ??? ???????? ?? ???? ??
?????? ?? ??????
CREATE TABLE ACCOUNT( id int NOT NULL,
balance decimal NOT NULL)
CREATE PROCEDURE spTransfer_Money( _at_from_acc
int, _at_to_acc int, _at_ammount decimal )
AS (???????? ??????????)
44?????????? ??????
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
45?????????? ? ????????
- ???????????? ????? ?? ????????? ???? ?? ????????
(isolation levels) - ??-??????? ???????? ????????? ??-?????
??????????????, ?? ?????? ??-????? ? ????????
??????? ?? ??-?????
???? ?? ???????? ?????? ?? ????????-???? ????? ???????-??????? ??? ?????? ???????? ??????
Read uncommitted ?? ?? ??
Read committed ?? ?? ??
Repeatable read ?? ?? ??
Serializable ?? ?? ??
46???????? ?? ??????????
- ???? ?? ??????? ???????????
- ??????, ?????? ?? ???? ?????? ???????? ??
??????????? ?????? ?? ?????? ?? ???? ??????? - ??????
- ???????? ?? ??? ?????????? ? ????????? ?
????????????? - ???????? ?? ????? ?? ????? ?????????? ? ?????????
? ??????? - ??? ?????????? ?? ????? ?? ?????, ?????? ?? ??
?????? ? ?????????? ?????????? (?? ?? ???????
?????? ??????????)
47????????? ? MS SQL Server ??????????
- ??????? ?? SQL Server
- ?????????? ?? SQL Server 2000
- ????????? ????? ? ???????????
- ????????? ? T-SQL
- Data Definition Language (DDL) ???????
- Data Manipulation Language (DML) ???????
- Database Console Commands (DBCC) ???????
- ????????? ?????????
- ?????????? ? SQL Server
48??????? ?? SQL Server
- ??????? ?? ???????????? ?????
- ?????? ?? SQL Server
- ?????? ?? SQL Server ?????????? ???
System.Data.SqlClient - SQL Server 7.0
- SQL Server 2000
49???????? ?????????? ?? SQL Server 2000 ??????
- MSSQLSERVER ?????? ??????
- ????????? ?????? ?? SQL Server ?????? ????? ??
??????????? ?? ???????????? - SQLSERVERAGENT SQL Server Agent
- ????????? SQL ???????, ???????? ??????????
???????? ? ???????? ?? ???????? - MSSQLServerADHelper
- ????? ?? ?????????? ? Active Directory
- MSSQLServerOLAPService
- ????? ?? OLAP ?????? ?? ?????
50???????? ?????????? ?? SQL Server 2000
???????????
- Enterprise Manager
- ???????? ????????????? ?? SQL ??????
- ?????????? ?? ???? ????? (?????????, backup,
??????????????, ?????????????) - ?????????? ?? ?????? ? ?????? (???????, ???????,
???????, ?????????, ) - Query Analyzer
- ?????????? ? ?????? ?? SQL ??????
- DTS (Data Transformation Services)
- ?????????, ????????????? ? ??????????? ?? ?????
?? ? ??? ?????? ?????????
51???????? ?????????? ?? SQL Server 2000
???????????
- SQL Profiler
- ???????????? ?? ??????? ? ??????????? ??
?????????????????? ?? SQL Server - SQL XML
- ??????????? ?? ????? ? Web ????? (? Internet
Information Services IIS) - ????????? SQL ?????? ? ????? XML
- Analysis Manager
- ????????? ?? Data Warehousing (OLAP)
- ??????? ?????????? ?? ?????????? ?????????? ??????
52???????????? ?? SQL Server
- ????????? ????? ? ???????????
- Visual Studio .NET 2002/2003
- SQL Servers ? Server Explorer-?
- ?????? ? ???????, ???????, ???????, ?????????,
???????? - ????????? / ????????? / ??????? ? ????????? ??
stored ????????? - DB ???????
- Query Analyzer
- ?????? ??? ????????? ?????????
- ?????????? ?? SQL ??????
- ??????????? ?? SQL ??????
53???????????? 1
- ?????? ? ??????????? Query Analyzer ?? MS SQL
Server
54???????????? 2
- Visual Studio .NET ? ??????????? ?? ?? SQL Server
- ?????? ??? Server Explorer
- ?????? ? ???????
- ????????? ?? DB Project
- ???????????? ?? T-SQL (????????? ????????? ???
VS.NET 2003)
55????????? ? T-SQL
- ?????? ???????
- Data Definition Language (DDL)
- ????????? ? ?????????? ?? ???????? ? ?????? ??
????? (???????, ???????, ???????, ...) - Data Manipulation Language (DML)
- ?????? ? ??????? ? ????????, ????????? ? ??????
?? ????? (?????????, ???????, ...) - DataBase Console Commands (DBCC)
- ???????? ????????? ?????????
56Data Definition Language (1/2)
- MS SQL Server 2000 ???????? SQL-92 DDL, ??
??????? ? ?????????? ?????????? - ??????? ?? ?????????? ? ?????? ?
- ???? ?? ?????
- ???????, ???????, ???????, constraints
- ???????
- ????????? ?????????
- ?????
57Data Definition Language (2/2)
- ?????? ???????
- ?????????? / ??????????? ?? ??????
- CREATE
- ALTER
- DROP
- ??????? ?? ??????
- GRANT
- DENY
- REVOKE
58?????? DDL ???????
- ????????? ?? ??????
- CREATE
59?????? DDL ???????
- ??????? / ?????????? ?? ??????
- ALTER
60?????? DDL ???????
- ????????? ?? ??????? ? ??????
61?????? DDL ???????
62?????? DDL ???????
- ???????? ?? ????? ?? ??????
- GRANT ????????? ??????
- DENY ????????? ??????
- REVOKE ?????? ?????????? ?? ???????? GRANT ?
DENY ???????
63???????????? 3
- DDL ??????? ? SQL Server 2000
64Data 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
65DML ???????
- 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
66DML ???????
- 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')
67DML ???????
- UPDATE ???????? ??????
- ?????????
- ??????
UPDATE table_name SET colname1value1,
colname2value2 WHERE condition
UPDATE Users SET Email'branimir_at_dir.nospam.bg' WH
ERE UserID 118
68DML ???????
- DELETE ??????? ?????? ?? ???????
- ?????????
- ??????
DELETE FROM TableName WHERE Condition
DELETE FROM Users WHERE UPPER(LastName)
'GIUROV' AND Mobile LIKE '359'
69???????????? 4
- D?L ??????? ? SQL Server 2000
70?????????? ???????
- COUNT() ????? ???? ??????
- SUM() ???? ?? ??????
- AVG() ????????????????? ???????? ?? ??????
- MAX() ?????????? ???????? ?? ??????
- MIN() ????????? ???????? ?? ??????
SELECT COUNT(LastName) FROM Users WHERE LastName
Ivanov'
71?????????? (JOIN) ?? ???????
- ????? ? ???????????
- ??????????? ?? ?????? ?? ??? ??? ?????? ??????? ?
????????? ?? ??? ????????? ???????, ???
?????????? ?? ????????? SELECT - ?????? ??????????
- ???????? ?????????? (INNER JOIN)
- ?????? ?????????? (OUTER JOIN)
- ?????????? ?????????? (CROSS JOIN)
72???????? ?????????? (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
73?????? ?????????? (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
74?????? ?????????? (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
75?????? ?????????? (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
76?????????? ?????????? (CROSS JOIN)
- ????? ?????????? ?? ???????? ?? ?????????,
????????? ? ????????????
SELECT fn.Name FirstName, ln.Name LastName FROM
FirstName fn CROSS JOIN LastName ln
77???????????? 5
- ?????????? ??????? ? ?????????? ?? ???????
78???????????? 6
- ?????????????? ?? MS SQL Server SQL Server
Books Online
79????????? ?????????
- ????????? ????????? (Stored Procedures)
- ????????? ? ????? ?? ??????
- ??????
- ???????? (sp_)
- ?????????????
- ????????? ????????? ????????? (Extended Stored
Procedures) - ????????? ? ????? ?? ??????
80????????? ?????????
CREATE PROC procedure_name _at_parameter
data_type VARYING default OUTPUT
    ,...n AS sql_statement ...n
CREATE PROCEDURE spTopExpensiveProducts
_at_count int AS SET ROWCOUNT _at_count SELECT
ProductName, UnitPrice FROM Products ORDER BY
UnitPrice DESC
81????????? ????????? ??????
CREATE PROC spInsertAuthor ( _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
82???????????? 7
- ?????????, ?????????? ? ????????? ?? ?????????
????????? ? VS.NET
83??????????
- ?????????? ??????? ??????, ????? ?????? ?? ????
????????? ???????? - ??? ????????? ??????, ???????????? ?? ???????
(rollback) ? ????????? ?? ?? ??????? - ?????? ??????????
- ??????? ???????? ?? SQL Server
- ???????????? ??????????? ?? DTC (Distributed
Transaction Coordinator) - ???? ?? ???????? ?? ????????????
- ???????? ?????????? ?? ??????? ????? ?????????
??????????
84?????????? ? SQL Server
- ??????? ?? ?????? ? ??????????
- BEGIN TRANSACTION (??? BEGIN TRAN)
- ??????? ???? ??????????
- COMMIT TRANSACTION
- ??????????? ???????? ??????????
- ROLBACK TRANSACTION
- ??????? ???????? ??????????
- SET TRANSACTION ISOLATION LEVEL
- ?????? ?????? ?? ???????? (READ COMMITTED, READ
UNCOMMITTED, REPEATABLE READ, SERIALIZABLE)
85?????????? ? SQL Server ??????
CREATE PROCEDURE spInsertUser ( _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
86???????????? 8
87???????????? 9
- ???? ?? ???????? ?? ????????????
88DBCC ??????? ? SQL Server
- DBCC Database Console Commands
- ???????????? ?? ?????????? ?? ??????????? ??
?????????? - ?????????
- ?????????
- ?? ?????? ????? ???? ?? ?????
- ??????? ? ??????? ?????
- ???????? ?? ???????
- ????????? ?? ???????? ?? ???????
- ?? ?????? ???? ?????
- ?? ???????, ?????? ? ??.
89??-????? DBCC ???????
- ?? ?????????
- DBCC DBREINDEX
- DBCC INDEXDEFRAG
- ...
- ?? ???????? ?? ???????
- DBCC OPENTRAN
- DBCC INPUTBUFFER
- DBCC OUTPUTBUFFER
- ...
90??-????? DBCC ???????
- ?? ?????????
- DBCC CHECKDB
- DBCC CHECKTABLE
- DBCC CHECKALLOC
- DBCC CHECKIDENT
- ...
- ?????
- DBCC DllName(FREE)
- DBCC HELP
- DBCC PINTABLE
- DBCC UNPINABLE
- ...
91???????????? 10
- DBCC ??????? ? SQL Server 2000
92????????? ?? ???? ?????
- ?????? ?? ??? ?????????? ?? ?????? ?????????? ???
???????, ????? ?? ?? ???????? - ?????? ?? ????????? ?? SQL Server ???? ?? ?????
- ???? ?????????? ? ??????????????
- ???? ???????? ? ????????
93????????? ???? ?????????? ? ?????????????? (1/5)
- 1. ?? Enterprise Manager ???????? ?????? ????? ?
?? ???????????? ???? ???????? Backup Database
94????????? ???? ?????????? ? ?????????????? (2/5)
- 2. ?????? ??? ?? ?????? ? ???????? ????, ??????
?? ?? ??????? ????????? ?????
95????????? ???? ?????????? ? ?????????????? (3/5)
- 3. ?? ??????? ???????? ???????? Restore Database
?? Enterprise Manager
96????????? ???? ?????????? ? ?????????????? (4/5)
- 4. ???????? ?????, ??? ????? ?? ?? ??????????
?????? ?? ?????
97????????? ???? ?????????? ? ?????????????? (5/5)
- 5. ???????? ??????, ?? ????? ?? ?? ??????????
?????? ?? ?????
98????????? ???? ???????? ? ???????? (1/3)
- 1. ? Enterprise Manager ???????? ?????? ?? ?????
? ?? ???????????? ???? ???????? Detach Database
99????????? ???? ???????? ? ???????? (2/3)
- 2. ???????? ????????? ?? ?????? ?? ????? ??
???????-???????? ?? ???????-????????. - ??????? ?? ?????? ?? ?????
- lt???_??_????gt.mdf
- lt???_??_????_loggt.ldf
100????????? ???? ???????? ? ???????? (3/3)
- 3. ???????? ?????? ?? ????? ?? ???????-????????
101???????????? 11
- ?????????? ? ?????????????? ?? ???? ?? ?????
102ADO.NET ?????????? (1)
- ?????? ?? ?????? ? ???????
- ??????? ? ?????????
- ???????? ?? ????????????
- ??????????? ?? ADO.NET
- Data Providers
- ?????? ? MS SQL Server
- SqlConnection
- ?????????? ?? ??????? ?????
- SqlCommand, SqlDataReader
- ???????????? ??????
103ADO.NET ?????????? (2)
- ?????? ? ????? ???? ?? ????? ???? OLE DB
- ?????????? ?? ????????? ?????
- ?????? DataSet, ?????-?????????? DataSet-?
- ????????? DataTable ? DataRelation
- ?????? ????? ADO.NET ? XML
- ?????? DataView
- ?????????? ?? DataAdapter
- ??????? ???????? ?? ?????? ? ????? ? ??????????
?????
104?????? ?? ?????? ? ?????
- ??????? ????? (connected model)
- ????????? ?????? ? ??????? (online)
- ?????? ?? ??????????
- ???????? ???? ????????????
- ????????? ????? (disconnected model)
- ???????? ? ??????? ?? ??????????? offline ??
????????? ?? ????? ? ???????? ?? ??????? ??
??????? - ?????? ?? ??????????
- ???????
- ?????? ?? ????? ???? Web ??????
- ?????????? ? XML
105??????? ?????
- ?????????? ?? ?????? ?? ????? ? ?????, ? ?????
?????? ??? ?????? ?? ????????? ?? ???????
106??????? ????? ?? ? ??????
- ??????????
- ??????? ? ??-????? ?? ????????????? (??-?????
?????? ?? ????????????) - ????????? ????? ????????????? ?????? ?? ?????????
??-????? - ??-????? ?????????? ?? ?????? ? ???????? ??????
?? ??????? - ???????????
- ????? ?? ????????? ??????? ??????
- ???????? ??? ????? ?? ????????????
107????????? ?????
- ???????????? ?? ??????? ?? ??????????? ??????? ??
?????????? ?? ??????? ?? ?????? ??????? ???
???????
108????????? ????? ?? ? ??????
- ??????????
- ???????? ?? ???????, ?????? ??? ?????, ? ?
?????????? ????? ?????? ??? ?????? ? ???????????
???? ?? ????? - ????? ??????????? ????? ?? ?? ????????
????????????? - ?????????????? ? ????? ?????
- ???????????
- ??????? ?? ?????? ?? ??????
- ???????????? ?????? ?? ???????? ?? ???????????
????? ?????????? ??????
109???????? ?? ????????????
- ?????????? ??????????
- ???-????? ?????? ???? ???? ??????????
- ??????????
- ?????? ?????????? ?? ?? ???? ?????
- ???????????
- ??????? ?? ???????????????? ??????? ?????????????
- ??????
- ?????????? ???????? ?? MS Access
110???????? ?? ????????????
- ????????? ?????????? (??????-??????)
- ??????????????? ????????? ? ?????? ????????? ??
????????? ?? ???? ????? - ??????? ?? ?????????? ??? ?????? ????
- ??????????
- ??? ????????? ?? ????????????????
- ???????????
- ???? ???????????? ???????? ? ??????????? ??
????? ???? ??????? - ???????
- MS SQL Server ? MS Query Analyzer
- MS Exchange ? MS Outlook
111???????? ?? ????????????
- ????????? ??????????
- ?????????? ?????? ?????????????? ?? ? ????????
?????? - ??????????
- ???????? ?? ???????????????? ????? ?????????????
?????????, ?????? ??????? ? ?????????? / ??????
?? ??????? - ???????????
- ??-?????? ?????????
- ?????? ?????? ?? ??????????? ?? ???????????
- ??????
- ASP.NET Web-?????????? ? ASP.NET Web ?????? ? MS
SQL Server
112???????? ?? ????????????
- ??????????? ??????????
- ??????? ? ?????? ?? 3 ????????? ????
- ?????????? ?? ???????? ?? ??? ?????? ??
??????????? ?? ???????????????? - ??????????
- ?????????? ?? ???????? ?????????? ?? ?????????
????? ?? ???????????????? ???? ???????? ?????????
- ???????????
- ????? ?????? ?????? ?? ?????????? ? ?????????? ??
??????? ?? ??????? ?????? - ???????? ?????? ????????? ? ??-?????? ??????? ??
??????????
113? ??????? ?? ???????
????????????, ????????????
114????? ? ADO.NET?
- ????? ?? ??????? ?? ?????? ? ?????
- ????? ?? ???????, ??????????, ????????? ? ?????
?????? ?? ?????? ?? ????? ???? ?????? .NET
???????? ?????????? - ????????? ????? ?? ?????? ? ?????
- ????????? ?????????? ?? ?????? ? ?????????? ?????
- ????????? ?????? ? XML
- ????????? ?? ADO (Windows ?????????? ?? ?????? ??
???? ?? ?????)
115Namespace-? ?? 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
116???????? ?? ADO ??? ADO.NET
ADO.NET
XxxConnection
Connection
XxxTransaction
XxxCommand
Command
Recordset
DataSet
XxxDataReader
XxxDataAdapter
117?????????? ?? 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
118Data Provider-? ? ADO.NET
- Data Provider-??? ?? ??????????? ?? ???????,
????? ?????????? ?????? ? ???????? ???? ?? ????? - ?? ?????????? RDBMS ??????? ?? ????????? ????????
Data Provider-? - ?????????? ????????????? ????????? ????????
????????? ?? ?????? ??? ????????? ?? ????? - ????????? ?? ?? 4 ??????? ??????
- Connection ?? ?????? ? ??????
- Command ?? ?????????? ?? SQL
- DataReader ?? ????????? ?? ?????
- DataAdapter ?? ?????? ? DataSet
119Data 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
120ADO.NET ? ???????? ?????
SqlDataReader
- ??????? ?? ?? ??????? ?? ????????? ?? ????????
- ???????? ?? ?????? (SqlConnection)
- ?????????? ?? ??????? / ??????? (SqlCommand)
- ????????? ?? ???????? ???????? ???? ???????? ??
???????? ???? ????? (SqlDataReader) - ????????? ?? ??????
- ????????? ?? ????????
SqlCommand
SqlConnection
121ADO.NET ? ?????????? ?????
- ??????? ?? ??????? ? DataSet ????? ? ???????? ??
????????????? - ???????? ?? ?????? (SqlConnection)
- ??????? ?? DataSet (???? SqlDataAdapter)
- ????????? ?? ????????
- ?????? ??? DataSet-a
- ???????? ?? ??????
- ???????? ?? ??????? ?? ??????? ?? ???????
- ????????? ?? ????????
DataSet
SqlDataAdapter
SqlConnection
122ADO.NET, XML ? Web ??????
- ADO.NET ? ????? ??????????? ? XML
- ????? ?? ???????? ? ????????? ????????
??????????? Web ?????? - Web-???????? ????????? ?????? ???? ?? ???????????
?????????? - ???????? ?????? ?????????? ??? ???????
123SqlClient Data Provider
- SqlConnection ??????????? ???????? ? MS SQL
Server - SqlCommand ????????? ??????? ????? SQL Server-?
???? ???? ?????????? ?????? - SqlDataReader ????? ?? ????????? ?? ????? ??
SQL Server-? - ??????? ?? ???????? ?? ????????? ???????
- SqlDataAdapter ?????? ????? ????? DataSet
?????? ? SQL Server - ????????? ????????? ?? DataSet ? ????? ?
?????????? ?? ????????? ????? - ???? ?? ?? ????? ??? ?? ??????????? ?? ???????? ?
?????? ?????
124??????? ?? ????????? ? SQL Server 2000
- ?????? ????????????
- Windows (???? ?????????? ? ??)
- ??????? ????????? ? ??????????
- ????????? ?? ??????
- ???????? ?? ??????
- ?????????? ?? ???????
- ??????? (Windows ? SQL Server)
- ??????? ?? ???????????? ? ???????? ??????
125???????? ??? ?? ?????? ? ???? ?? ?????
(Connection String)
- ???????? ???????????, ?????????? ?? ?????????????
?? ?????? ? ?????? ????? - ??????? ?????????
- Provider ??? ?? ???????? ?? ??????
- Data Source ????????????? ?? ??????
- User ID/Password
- Integrated Security
- Persist Security Info (False)
- ??????
Serverlocalhost DatabasePubs Integrated
Securitytrue Persist Security Infofalse
126SqlConnection ??????
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 // ...
127Connection Pooling
- "Database Connection Pooling" ? ?????????
???????, ????? - ????????? ??-????????? ?????????? ?? ???????? ???
?????? ?? ????? - ????????? ??????????????????
- ???????? ?????? ????
- ??? ???????? ?? ?????? ?? ?? ????? ?????? ?? ?.
???. "???" (connection pool) - ??? ??? ???? ???????? ?? ?????? ????
- ??? ????????? ?? ?????? ?? ?? ????? ? ???? ??? ??
?? ???????? ????????? - ?? ???????????? SqlClient ?????? ???
128??? ?? ???????? ??? SqlConnection
- ??????????? ???????? ? ????????? ?? ????????
(?????? ? ????) - Open() ? Close() ????????
- ?????????? ???????? ? ????????? ?? ????????
(?????? ? ????) - ???????? ? ???? ??? ?????? ? DataAdapter-?
- ?????????? ?? Dispose() ??????
- ??????? ?? ??? ???????? using (con)
- ????? ???????? ? connection pool-?
129????????? ?? SqlConnection
- ??????? ?????????? ?? ????????
- StateChange ? InfoMessage
- StateChangeEventArgs ??????
- ???? ?????????? ?? ???? ????? ?? ? ??????? ?
???????? ??? ?????? ????? - CurrentState ? OriginalState
- SqlInfoMessageEventArgs
- Errors SqlErrorCollection ???????? ?? ?????????
?? ?????? ? ?????????????? - Message ??????????? ?? ??????
- Source ???? ? ?????????? ????????/??????????????
??
130StateChange ??????
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())
131InfoMessage ??????
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)
132???????????? 12
- ???????????? ?? ????????? StateChange ?
InfoMessage
133?????????? ?? ??????? ?????
SqlDataReader
- ??????? ?? ?? ??????? ?? ????????? ?? ????????
- ???????? ?? ?????? (SqlConnection)
- ?????????? ?? ??????? / ??????? (SqlCommand)
- ????????? ?? ???????? ???????? ???? ???????? ??
???????? ???? ????? (SqlDataReader) - ????????? ?? ??????
- ????????? ?? ????????
SqlCommand
SqlConnection
134????????? ? ???????? ?????
135?????? SqlCommand
- ????????? ????????? ??? SQL ?????? ??? ?????????
????????? - ??-????? ????????
- Connection ????? / ?????? SqlConnection-? ??
????????? - CommandType ??? ???????
- CommandType.StoredProcedure
- CommandType.TableDirect
- CommandType.Text
- CommandText SQL ?????? ??? ??? ?? ?????????
????????? - Parameters ?????????
136?????? SqlCommand
- ??-????? ??????
- ExecuteScalar()
- ????? ???????? ???????? (??????? ?????? ?? ??????
??? ?? ?????????) - ????????? ???????? ? System.Object
- ExecuteReader()
- ????? ?????? (SqlDataReader)
- CommandBehavior ?????? ?????????
- ExecuteNonQuery()
- ????? ???? ?? ??????????? ?????? (int)
- ExecuteXmlReader()
- ????? XmlReader ?? ?????????
- ???????? ?? ???? ? SqlClient
137?????? SqlDataReader
- ??????? ???????????????? ?? ?????? (??????)
???????? ?? ????????? ??????? - ???????? ? ???? ?? ?????? (read-only)
- ???????? ? ??????????? (forward-only)
- ??-????? ?????? ? ????????
- Read() ????????? ??????? ?????? ? ????? false
??? ???? ??????? ????? - Item (??????????) ??????? ?????????? ?? ??????
?? ??? ??? ?????? - Close() ??????? ??????? ?????? ????????????
?? ?? ????!
138??????? ????? ??????
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 (???????? ??????????)
139??????? ????? ??????
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()
140???????????? 13
- ?????????? ?? ??????? ????? SqlCommand ?
SqlDataReader
141????????? ?? SqlCommand
- ?????? ?? ????????? SqlCommand ????? ?? ???????
?????? - ?????????
- ?? Server Explorer ??? VS.NET
- ?? Toolbox ??? VS.NET
SqlCommand cmd new SqlCommand( "SELECT
FROM Products, con)
142???????????? 14
- ????????? ?? SqlCommand ??? VS.NET ???? Server
Explorer ? ???? Data ???????????? ?? Toolbox-?
143?????? SqlParameter
- ????? ????????????? SqlParamer-????
- SQL ???????? ? ????????? ????????? ????? ?? ????
??????? ? ???????? ????????? - ?? ?? ????????? ???? Parameters ?????????? ??
SqlCommand ????? - ??-????? ????????
- ParameterName ??? ?? ??????????
- DbType ??? (NVarChar, Timestamp, )
- Size ?????? ?? ???? (??? ???)
- Direction ??????, ???????, ...
144???????????? ?????? ??????
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()
145???????? ???? ?????????
- ??????????? ?? ???????????-????????? ????????
???? ? ?????????? ?? ????? 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()
146???????????? 15
- ?????? ? ???????????? SQL ??????
- ????????? ?? ????????? ?? ?????
- ???????? ?? ????????? ? ????????? ?? ???????????
?? ???? ???????? ????
147?????????? ?? ??????????
- ?????? ? ?????????? ? 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
148?????????? ?? ??????????
- ?????? ? ?????????? ? ADO.NET
- ????????? ?? ??????????
- ????????? ?? ??????? ? ?????? ??????????
- ????????????? / ????????? ?? ??????????
SqlTransaction trans dbConnection.BeginTrans
action()
command.Transaction trans
trans.Commit() trans.Rollback()
149?????????? ?? ??????????
- ?????? ?? ???????? ?? ???????? ? ????????????
IsolationLevel - ???? ?? ???????? ?? SqlTransaction
- ReadUncommited
- ReadCommited
- RepeatableRead
- Serializable
- ??????
SqlTransaction trans dbConnection.
BeginTransaction(IsolationLevel.Serializable)
150???????????? 16
151?????? ? ????? ???? ?? ?????
- ADO.NET ???????? ?????? ? ???????? ???? ?? ?????
(????? SQL Server) ???? ????????? Data Providers - OLE DB ???????? ?? ??????????
- Oracle ???????? ?? ??????????
- MySQL ????????? ?? ????????????
- PostgreSQL ????????? ?? ????????????
- ??????????? ?? ????????????
- IDbConnection
- IDbCommand, IDataParameter
- IDataReader
- IDbDataAdapter
152OLE 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")
153?????? ? 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
154?????? ? 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)
155???????????? 17
- ?????? ? MS Access ???? OLE DB
156???????? ?????? ? ????
- ?????????
- ??????????? ?????????? ?????? ?? ?????? ?????, ?
????? ????????, ? ?? ?????? - ????? ???? ?? ????? ????????? ?????? ?? ???? ???
?? ???? - ? MS SQL Server 2000 ??? datetime (8 ?????) ?
smalldatetime (4 ?????) - ??? ?????? ? ????? ?? ??? ????, ???????????
?????? ???? ?? ???????????? ??? ???????????
157???????? ?????? ? ????
- ??????????? System.DateTime ??????????? ?? ??????
? ???? ? .NET - ??????????? ???????????? ?????? ?? ????????? ??
???? ??? ?????? ????? - ??? ????? ?? ???????????? ??????????
IFormatProvider ?? ?????????? ?? ????????? ??
???????????? - ??? ???????????? ??????????? ????????? ????????
????????? CultureInfo.InvariantCulture
158?????? ? ???? ??????
- 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)
- (???????? ??????????)
159?????? ? ???? ??????
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()
160???????????? 18
- ?????? ? ???? ? MS SQL Server
161?????? ? ???????? ? ??
- ?? ??????????? ?? ???????? ??????????? ? ???? ??
????? ?? ????????? ??????? ?????? - ??? "image" ? MS SQL Server
- ??? "blob" ? Oracle
- ??? "OLE Object" ? MS Access
- ??? ?????? ??????? ?????? ?? ?? ???????? ?????? ?
??????
162???????????? 19
- ?????????? ?? ???????? ??????????? ? ???? ?? ?????
163ADO.NET ? ?????????? ?????
DataSet
- ??????? ?? ??????? ? DataSet ????? ? ???????? ??
????????????? - ???????? ?? ?????? (SqlConnection)
- ??????? ?? DataSet (???? SqlDataAdapter)
- ????????? ?? ????????
- ?????? ??? DataSet-a
- ???????? ?? ??????
- ???????? ?? ??????? ?? ??????? ?? ???????
- ????????? ?? ????????
SqlDataAdapter
SqlConnection
164?????? ? ?????????? ?????
- ??????? ????????, ? ????? ?? ???????? ???????? ?
?????????? ????? - ??????? ??????????? ??????? ?? ???????
- DataSet
- ????????? ???? ??????? ???????, ???????,
constraints ? ????? ?????? - DataTable ??????? ???????
- XxxDataAdapter ??????????? ??????? ?? ???????
???? XxxCommand, XxxConnection ??????? - DataRelation ?????? ????? ???????
165DataSet ??????? ?????
- ???????? ? DataSet
- Tables ??????? ?????????
- Relations ??????? ????????? ????? ?????????
- ????????? ?? ??????????? ????????? (data binding)
- ????? ?? DataSet
- ?????? ??????????? ?? DataSet-?