!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
1
!"# 1. $%&'(!)(*%# Microsoft SQL Server. !"!#$%&'( ')*!+$&+,)' -'". /'00.* !"#$%&'( Microsoft SQL Server )!&*+,#- ) .#/' .0.-#$12# /,"2 3,1124 (master.model, msdb, tempdb), .53#(6,70# .&86#/18* 0195($,:0*, 0 %5&;"5),-#&;.!0# /,"2 3,1124. <,63,' /,", 3,1124 (,"$#7,#-.' ) 5-3#&;124 9,=&,4 – $010$8$ 3)84: 5301 3&' .,$5= /,"2 3,1124 – 9,=& 3,1124 (mdf-9,=&), 0 5301 3&' 68(1,&, -(,1",!:0= (ldf-9,=&). >#()2= 9,=& 3,1124 (mdf-9,=&) ')&'#-.' 5.15)12$ 0 !(5$# .,$04 3,1124 .53#(60.0.-#$18* 0195($,:0*, )-5(5= 0 ).# %5.&*70# 9,=&2 3,1124 ')&'*-.' )-5(0+12$0 (ndf-9,=&,$0) 0 .53#(6,- 1#%5.(#3.-)#115 .,$0 3,112#. ?,.%5&56#10# @-04 9,=&5) $5615 8!,",-; %(0 .5"3,100 /,"2 3,1124. A.15)15= #3010:#= 4(,1#10' 3,1124 ')&'#-.' .-(,10:,. SQL Server )2%5&1'#- +-#10# 0 ",%0.; 3,1124 %5.-(,10+15. B.' /,", 3,1124 &5C0+#.!0 %53(,"3#, 1, .-(,10:2, 18$#(8#$2# 1,+01,' . 0. ?,"$#( .-(,10:2 .5.-,)&'#- 8 ,=- (128 .-(,10: 1, 5301 $#C,/,=-) D&' /5# @99#!-0)15C5 8%(,)
' .-(,10:,$0 510 5/E#301'*-.' ) @!.-#1-2 – %5 8 .-(,10: ) @!.-#1-#. !.-#1-2 $5C8- /2-; 3)84 -0%5): mixed – .-(,10:2, )453'70# ) -,!5= @!.-#1- $5C8- %(01,3,-; (,"12$ 5/E#!-,$; uniform- @!.-#1- .53#(60- .-,10:2, %(01,3,70# 5315$8 5/E#!-8.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
F0.-#$1,' 0195($,:0' 5 .-(,10:# 4(,10-.' ) ",C5&5)!#, %53 !5-5(2= 5-)530-.' %#()2# 96 /,=-. -, 0195($,:0' .53#(60- 15$#( .-(,10:2, -0% .-(,10:2, !5&0+#.-)5 .)5/5315C5 %(5.-(,1.-),, 0 ID 5/E#!-,, )&,3#*7#C5 .-(,10:#=. B !51:# !,635= .-(,10:2 (,.%5&,C,#-.' -,/&0:, .$#7#10' .-(5!.
SQL Server 0.%5&;"8#- ) 9,=&,4 3,1124 .&*70# -0%2 .-(,10:: Data - .-,10:, .53#(60- .-(5!0 ).#43,1124 ", 0.!&*+#10#$ 3,1124 -0%, text, ntext, image, nvarchar(max), varchar(max), varbinary(max) 0 xml-3,1124;
2
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
3
Index - .-,10:, .53#(60- 0195($,:0* 5 013#!.,4;. Text/Image .-(,10:, 3&' 4(,1#10': o .&*704 -0%5) LOB-5/E#!-5): text, ntext, image, nvarchar(max), varchar(max), varbinary(max) 0 xml-3,1124; o .-5&/:5) %#(#$#115= 3&012, +#= (,"$#( .-(5!0 %(#)2G,#- 8 KB: varchar, nvarchar, varbinary 0 sql_variant Global Allocation Map - .-,10:, 3,115C5 -0%, .53#(60- 0195($,:0* 5/ 0.%5&;"8#$5.-0 @!.-#1-5) (1, 5315= .-(,10:# 4(,1'-.' 3,112# 5/ 0.%5&;"8#$5.-0 64000 @!.-#1-5)); Shared Global Allocation Map - .-,10:, 3,115C5 -0%, .53#(600195($,:0* 5/ 0.%5&;"8#$5.-0 @!.-#1-5) -0%, Mixed; Page Free Space - .-(,10:, .53#(60- 0195($,:0* 5 !5&0+#.-)# .)5/5315C5 %(5.-(,1.-), 1, .-(,10:#; Index Allocation Map - .-(,10:, .53#(60- 3,112#, !,!0# @!.-#1-2 0$#*- .-(,10:2, %(01,3,70# 5315$8 5/E#!-8)&,3#&;:8; Bulk Changed Map - .-,10:, .53#(60- 0195($,:0* 5/ @!.-#1-,4, 0"$#1#1124 %5.(#3.-)5$ 1,/5(, 5%#(,:0=, )2%5&1#1124 %5. %5.#= 5%#(,:00 !5%0(5),10' /,"2 3,1124 (BACKUP LOG); Differential Changed Map - .-,10:, .53#(60- 0195($,:0* 5/ @!.-#1-,4, 0"$#1#1124 . $5$#1-, %5.#= 5%#(,:00 !5%0(5),10' /,"2 3,1124 (BACKUP DATABASE). H,/&0:2 0 013#!.2 4(,1'-.' !,! 1,/5(2 .-(,10:. H,/&0:, $56#- /2-; %53(,"3#, 1, 5315 0&0 1#.!5&;!5 (,"/0#10= (partitions), .53#(6,704 .-(5!0.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
4
?,"/0#10# -,/&0:2 5%(#3#&'#- %5&;"5),-#&; %(0 ## .5"3,100. F1,+,&, ) /,"# 3,1124 .5"3,#-.' 981!:0', 5-5/(,6,*7,' .-(5!0 -,/&0:2 0&0 013#!., 1, (,"/0#10#, 5.15),115# 1, "1,+#10'4 8!,",115C5 .-5&/:, (CREATE PARTITION FUNCTION). D,# .5"3,#-.' .4#$,, !5-5(,' 5-5/(,6,#- (,"/0#10' (,"/0),#$24 -,/&0: 0&0 013#!.5) 1, C(8%%2 9,=&5) (CREATE PARTITION SCHEME). I,%(0$#(: CREATE PARTITION FUNCTION myPFunc (int) AS RANGE LEFT FOR VALUES (1, 1000); GO CREATE PARTITION SCHEME myPScheme AS PARTITION myPFunc TO ( to1fg, to1fg, to1fg, to2fg ); -- C(8%%2 9,=&5) J$' C(8%%2 9,=&5) 5%(#3#&'#-.' !5$,135= CREATE DETABASE. >5 8$5&+,10* 9,=&2 3,1124 )453'- ) 5.15)18* C(8%%8 9,=&5). H,/&0:2 SQL Server 2005 0.%5&;"8*- 3&' 5(C,10",:00 04 .-(,10: 3,1124 ) (,"/0#100 5301 0" .&*704 3)84 $#-535):
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ !"#$%&'(% $")!*+( (3&' !5-5(24 .5"3,1 !&,.-#(12= 013#!., -(#/8*70= 90"0+#.!5C5 %#(#.-(5#10' 3,1124 ) .55-)#-.-)00 .5 .-(8!-8(5= 013#!.,); ,-* (Heaps) – -,/&0:2, 1# 0$#*70# !&,.-#(15C5 013#!.,. J13#!.2 ) SQL Server 5(C,10"5),12 ) )03# B-3#(#);#). <,63,' .-(,10:, ) 013#!.15$ B-3#(#)# 1,"2),#-.' 013#!.12$ 8"&5$ (index node). B )#(G01# B-3#(#), (,.%5&56#1 !5(1#)5= 8"#& (root node). B 1061#= +,.-0 3#(#), (,.%5&,C,*-.' 013#!.12# 8"&2, 1,"2),#$2# &0.-;' (leaf nodes). K#638 )#(G015= 3#(#), 0 &0.-;'$0 (,.%5&,C,*-.' %(5$#68-5+12# 8(5)10 (intermediate levels). <,63,' .-(5!, 013#!., .53#(60- !&*+#)5# "1,+#10# 0 8!,",-#&; 0&0 1, .-(,10:8 %(5$#68-5+15C5 8(5)1' ) B-3#(#)#, 0&0 1, 3,112#, (,.%5&56#112# ) 1061#$ 8(5)1# 3#(#), (leaf level). L.&0 -,/&0:2, 3&' !5-5(5= %5.-(5#1 !&,.-#(12= 013#!. 0$##- -(0 (,"/0#10', -5 3&' !,635C5 (,"/0#10' /83#- %5.-(5#15 .)5# B-3#(#)5. F-(,10:2 ) :#%5+!# 3,1124 0 .-(5!0 ) 104 8%5('35+0),*-.' .5C&,.15 "1,+#10* !&*+, !&,.-#(15C5 013#!.,. F&*70= (0.815! 0&&*.-(0(8#- .-(8!-8(8 !&,.-#(15C5 013#!.,.
5
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
D5.-8% ! -,/&0:,$, 1# 0$#*70$ !&,.-#(124 013#!.5), )2%5&1'#-.' %5.(#3.-)5$ %5.#),-#&;15C5 %(5.$5-(, IAM-.-(,10: . :#&;* 1,4563#10' @!.-#1-5), .53#(6,704 .-,10:2, 5-15.'70#.' ! 3,115= !8+#. F&*70= (0.815! 0&&*.-(0(8#- %(5:#.. 0")+#10' .-(5! 3,1124 -,/&0:2 $,G015= /," 3,1124 Database Engine.
6
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
7
I#!&,.-5(12# 013#!.2 0$#*- .-(8!-8(8 B-3#(#), %535/15 !&,.-#(12$ 013#!.,$, ", 0.!&*+#10#$ .&*704 (,"&0+0=: .-(5!0 3,1124 -,/&0:2 ')&'*-.' 1# 8%5('35+#112$0 0 4(,1'-.' ) %5('3!#, 5.15),115$ 1, 04 1#!&,.-#(15$ !&*+#; leaf- 8(5)#1; 1#!&,.-#(15C5 013#!., .5.-50- 0" 013#!.124 .-(,10:, , 1# .-(,10: 3,1124. <,63,' .-(5!, 013#!., ) 1#!&,.-#(15$ 013#!.# .53#(60- 1#!&,.-#(15# !&*+#)5# "1,+#10# 0 &5!,-5( .-(5!0 (row locator). L.&0 -,/&0:, 1# .53#(60!&,.-#(15C5 013#!.,, -5 &5!,-5( .-(5!0 ')&'#-.' 8!,",-#$ .-(5!0, ) %(5-0)15$ .&8+,# – !&*+5$ !&,.-#(15C5 013#!., 3&' 3,115= .-(5!0. M!,",-#&; .-(5!0 (ROWID) .53#(60- ID-9,=&,, 15$#( .-(,10:2, 15$#( .-(5!0 1, .-(,10:#. I, .&*7#$ (0.81!# %(#3.-,), .4#$, 0.%5&;"5),10' 1#!&,.-#(15C5 013#!.,.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
D&' .-5&/:5) -0%, XML SQL Server %5")5&'#- .5"3,),-; XML-013#!.2. L3010:, (,"$#7#10' (allocation unit) ')&'#-.' 1,/5(5$ .-(,10: ) !8+# 0&0 B-3#(#)#, 0.%5&;"8#$5$ 3&' 8%(,)
' 3,112$0 1, 5.15)# -0%, .-(,10:2. SQL Server 3&' 8%(,)
' 3,112$0 -,/&0: 0 013#!.5) %(0$#1'#.&*70# -0%2 #3010: (,"$#7#10': IN_ROW_DATA LOB_DATA
8
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
9
ROW_OVERFLOW_DATA
1*$2' B )#(.00 Microsoft SQL Server 2005 .4#$5= 1,"2),#-.' 1,/5( @$#1-5) (entities) /,"2 3,1124, 95($0(8*70= #3015# %(5.-(,1.-)5 0$#1. B %(#3238704 )#(.0'4 Microsoft SQL Server %51'-0# .4#$2 /2&5 -#.15 .)'",15 . 0$#1#$ %5&;"5),-#&' /,"2 3,1124: 3&' !,635C5 %5&;"5),-#&' %(#31,"1,+,&,.; 53150$#11,' .4#$, /,"2 3,1124. B )#(.00 Microsoft SQL Server 2005 %5&;"5),-#&; $56#- (,"$#7,-; .)50 5/E#!-2 ) (,"&0+124 .4#$,4. A/E#301#10# 5/E#!-5) ) .4#$2, 1# ,..5:00(8#$2# . !51!(#-12$ %5&;"5),-#$, 1# -(#/8#- )1#.#10' 0"$#1#10= ) %(5:#38(2, 0.%5&;"8*70# 5/E#!-2 .4#$2, %(0 ",$#1# %5&;"5),-#&'. I#.!5&;!5 %5&;"5),-#= $5C8- )&,3#-; 5315= .4#$5= +#(#" +.-)5 ) (5&0 0&0 C(8%%# Windows. F5"3,-; 15)8* .4#$8 $5615, 0.%5&;"8' C(,90+#.!0= 01.-(8$#1-,(0= Micrisoft SQL Server Management Studio, 0&0 %(5C(,$$12$ %8-#$, )2%5&10) 5%#(,-5( CREATE SCHEMA, !5-5(2= 0$##- .&*7## 95($,&;15# 5%0.,10#: CREATE SCHEMA schema_def [<schema_element> [ , ...n ] ] <schema_def> ::= { !"_#$%!& | AUTHORIZATION !"_'()*%(+,) | !"_#$%!& AUTHORIZATION !"_'()*%(+,) } <schema_element> ::= { -./%*%(%0 %_1)2( ,& | -./%*%(%0 %_./%*#1)'(%0 " |
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
10
grant_-.%/)1-/ | revoke_-.%/)1-/ | deny_-.%/)1-/ } -5- SQL-5%#(,-5( 5315)(#$#115 . .5"3,10#$ 15)5= .4#$2 $56#35/,)&'-; ) 1## 15)2# -,/&0:, 0 %(#3.-,)
', , -,!6# 8.-,1,)&0),-; %5&15$5+0' 3,112$ 5/E#!-,$ %5.(#3.-)5$ 9(," GRANT, DENY 0 REVOKE. J$' )&,3#&;:, .4#$2 ')&'#-.' 0$#1#$ %5&;"5),-#&' /,"2 3,1124. A%(#3#
# -,/&0:2 ",3,#-.' 5%#(,-5(5$ CREATE TABLE (%(0 @-5$ 1#5/4530$5 1,&0+0# .55-)#-.-)8*704 %5&15$5+0=), , 5%(#3#
# %(#3.-,)
' – 5%#(,-5(5$ CREATE VIEW. I,%(0$#(: USE MyDb; CREATE SCHEMA Schema1 AUTHORIZATION dbo CREATE TABLE Tbl1 (id1 int, f1 int, f2 varchar(20)) GRANT SELECT TO user1 DENY SELECT TO user2; GO D&' %5&8+#10' .%0.!, ).#4 95($ $5615 0.%5&;"5),-; %(#3.-,)
# sys.schemas:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
11
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
3-4$&+. -'". /'00.* N5C0+#.!0 3,112# ) /,"# 3,1124 4(,1'-.' ) )03# 5/E#!-5) /,"2 3,1124. A/E#!-2 3,1124 4(,1'-.' ) .4#$# /,"2 3,1124. SQL Server %(#35.-,)&'#- .&*70# 5/E#!-2 3,1124: -,/&0:2; %(#3.-,)
'; .01010$2; 013#!.2; 4(,10$2# %(5:#38(2; -(0CC#(2; %5&;"5),-#&;.!0# -0%2 3,1124; 981!:00 %5&;"5),-#&'; !&*+0, 5/#.%#+0),*70# ..2&5+18* :#&5.-15.-;; 5C(,10+#10' :#&5.-15.-0; 8$5&+,10' %(,)0&, (0.%5&;"8*-.' 3&' 5/(,-15= .5)$#.-0$5.-0) < 5/E#!-,$ /,"2 3,1124 -,!6# 5-15.'-.' .4#$2, %5&;"5),-#&0 0 (5&0. B SQL Server ))#3#12 15)2# 5/E#!-2, 0.%5&;"8#$2# Service Broker: -0%2 .55/7#10= (.-(8!-8(, .55/7#10', 5-%(,)&'#$5C5 5- 5315C5 .#()0., 3(8C5$8), !51-(,!-2 (.5C&,G#10' $#638 3)8$' .#()0.,$0), 5+#(#30 (.55/7#10', 1,%(,)p# .#()0.8), .#()0.2 (1,/5(2 ",3,+, C3# !,63,' ",3,+, %(#3.-,)&'#-.' !51-(,!-5$), .#()0.12# %(5C(,$$2.
15"/'0!$ -'". /'00.* F5"3,-; /,"8 3,1124 $5615 !,! %(5C(,$$12$ %8-#$, )2%5&10) SQL5%#(,-5( CREATE DATABASE, -,! 0 0.%5&;"8' .(#3.-), Micrisoft SQL Server Management Studio.
12
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
13
>(0 %53!&*+#100 ! SQL-.#()#(8 .&#- )2/(,-; -0% .#()#(,: Database Engine, Analysis Services, Reporting Services, SQL Server Mobile, Integration Services, 0 -0% ,8-#1-090!,:00 %5&;"5),-#&': Windows Authentication 0&0 SQL Server Authentication. D&' .5"3,10' 15)5= /,"2 3,1124 .&#- ) Micrisoft SQL Server Management Studio ) 5!1# Object Explorer )2%5&10-; 3&' .#!:00 Database !5$,138 !51-#!.-15C5 $#1* New Database 0 ) %(#3&56#115$ 3,# 30,&5C# ))#.-0 0$' .5"3,),#$5= %5&;"5),-#&;.!5= /,"2 3,1124. I, .-(,10:# Options 30,&5C, New Database .&#- ",3,-; %,(,$#-(2 /,"2 3,1124. ?#60$ (,/5-2 . !8(.5(5$ 90!.0(8#-.' 3)8$' %,(,$#-(,$0: Close Cursor on Commit Enabled – %,(,$#-( 5%(#3#&'#- /83#- &0 ",!(2- !8(.5( %(0 90!.,:00 -(,1",!:00, ) !5-5(5= 3,112= !8(.5( /2& 5-!(2-. >5 8$5&+,10* "1,+#10# %,(,$#-(, (,)15 false – !8(.5( 5.-,#-.' 5-!(2-2$ %5. 90!.,:00 -(,1",!:00, 0 %(0 5-!,-# -(,1",!:00 ",!(2),*-.' !8(.5(2, !5-5(2# 1# /2&0 5%(#3# !,! STATIC 0&0 INSENSITIVE. A-$#-0$, +-5 %(0 "1,+#100 True 90!.,:0' 0&0 5-!,- -(,1",!:00 5"1,+,#- ",!(2-0# !8(.5(,; Default Cursor – %,(,$#-(, 5%(#3#&'*70= %5 8$5&+,10* %5)#3#10# !8(.5(,, !,! LOCAL 0&0 GLOBAL ("1,+#10# %5 8$5&+,10*). D&' 5C(,10+#10' 35.-8%, ! /,"# 3,1124 "1,+#10# %,(,$#-(, Restrict Access $5615 ",3,-; !,!: Multiple – ! /,"# 3,1124 5315)(#$#115 (,"(#G#1 35.-8% 1#.!5&;!0$ %5&;"5),-#&'$ ("1,+#10# %5 8$5&+,10*); Single – ) !,632= $5$#1- )(#$#10 ! /,"# 3,1124 $56#- /2-; %53!&*+#1 -5&;!5 5301 %5&;"5),-#&;; Restricted - ! /,"# 3,1124 $5C8- /2-; %53!&*+#12 -5&;!5 %5&;"5),-#&0, 0$#*70# %5&15$5+0', 5%(#3#&'#$2# (5&'$0 db_owner, dbcreator 0&0 sysadmin.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
14
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
15
!"# 2. +!#,'-#.'/ 012(*3# ) 4#-#" 0#556& 2%!02(7#"' /-6)# SQL. 6".& SQL O"2! SQL %(#31,"1,+#1 3&' 35.-8%, ! 0195($,:00 0 8%(,)
' (#&':05115= /,"5= 3,1124. O"2! SQL 5%(#3#&'#-: 5%#(,-5(2 '"2!,, 1,"2),#$2# 015C3, !5$,13,$0 '"2!, SQL; -0%2 3,1124; 1,/5( ).-(5#1124 981!:0=. >5 .)5#$8 &5C0+#.!5$8 1,"1,+#10* 5%#(,-5(2 '"2!, SQL +,.-5 (,"/0),*-.' 1, .&*70# C(8%%2: '"2! 5%(#3#
' 3,1124 DDL (Data Definition Language); '"2! $,10%8&0(5),10' 3,112$0 DML (Data Manipulation Language). O"2! 5%(#3#
' 3,1124 )!&*+,#- 5%#(,-5(2, 8%(,)&'*70# 5/E#!-,$0 /,"2 3,1124. < 5/E#!-,$ /,"2 3,1124 5-15.'-.' -,/&0:2, 013#!.2, %(#3.-,)
'. O"2! $,10%8&0(5),10' 3,112$0 )!&*+,#- 5%#(,-5(2, 8%(,)&'*70# .53#(6,10#$ -,/&0: /,"2 3,1124 0 0")!,*70$0 0195($,:0* 0" @-04 -,/&0:. O"2! DML 5%(#3#&'#- .&*70# 5%#(,-5(2: SELECT – 0")+#10# 3,1124 0" 5315= 0&0 1#.!5&;!04 -,/&0:; INSERT – 35/,)
# .-(5! ) -,/&0:8; DELETE – 83,
# .-(5! 0" -,/&0:2; UPDATE – 0"$#1#10# "1,+#10= %5= ) -,/&0:#.
7$)$&89#$0!$ 2$:/, -'"'2! /'00.* A301 %5&;"5),-#&; $56#- (,/5-,-; . 1#.!5&;!0$0 /,",$0 3,1124. D&' %#(#!&*+#10' 1, !51!(#-18* /,"8 3,1124 %(0$#1'#-.' SQL-5%#(,-5( USE, !5-5(2= 0$##- .&*7## 95($,&;15# 5%0.,10#: USE { !"_2)3&_*)00&$ }
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
16
B.# 5%#(,-5(2, )2%5&1'*70#.' %5. 5%#(,-5(, USE /838- 0.%5&;"5),-; 8!,",118* /,"8 3,1124. D&' )2%5&1#10' %#(#453, 1, 3(8C8* /,"8 3,1124 %5&;"5),-#&; 35&6#1 5/&,3,-; .55-)#-.-)8*70$0 %5&15$5+0'$0, 1,%(0$#(, 0$#-; (5&; dbo 0&0 sysadmin.
7$)$&89#$0!$
!"#$ %&'(!%)(* + ,-.&/'!'+0
!"#$% '()!*"$"+, !"!#$%& '(#)#( *+,- +.!$#,& */' .0+"# (12#$)+3 !*'%-4 *+,- +.!$#,&), ',' */' .5 +.# .5*+,)(+6+ (+"1,&. 7'%$#(! 8# +*!%)+%$' %$/+'$%& )! *+%,#"+.!$#,-)+( *+"9,42#)'' 9 %#/.#/1 SQL Server, ! !$#( 9 %!(+3 8! # "!))50. :! +3 8!))50 *+ 1(+,2!)'4 *#/.+)!2!,-)+ 1%$!)!.,'.!#$%& 8! ! "!))50 master. ;/' !1$#)$'<'9!='' )! 1/+.)# %#/.#/! */+.#/$%& 12#$)!& !*'%- (login) ' .5*+,)$%& +$+8/!>#)'# "!))+3 12#$)+3 !*'%' . '(& *+,- +.!$#,& 8! 5 "!))50 (user). ?%# !/#6'%$/'/+.!))5# 12#$)5# !*'%' (login) +$+8/!>!4$%& . %#9='' Security +9)! Object Explorer %/#"5 Micrisoft SQL Server Management Studio, ! *+,- +.!$#,' 9!>"+3 8! 5 "!))50 +$+8/!>!4$%& . %#9='' Security %++$.#$%$.14@#3 8! 5 "!))50. A! %,#"14@#( /'%1)9# */#"%$!.,#)+ %++$.#$%$.'# (#>"1 12#$)+3 !*'%-4 user1 ' '(#)#( *+,- +.!$#,& 8! 5 "!))50 MyDB name_user1.
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
17
B+>)+ 6+.+/'$-, 2$+ */' *+"9,42#)'# 9 8! # "!))50 */+'%0+"'$ *#/.'2)!& ()! 1/+.)# %#/.#/!) ' .$+/'2)!& ()! 1/+.)# 8! 5 "!))50) '"#)$'<'9!='& *+,- +.!$#,&.
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
18
SQL Server 2005 *+ .+,$ &.)+ 19! !$- 9+)$#9%$ .5*+,)#)'& $!9'0 +*/#"#,(50 *+,- +.!$#,#( (+"1,#3 9!9: <1)9='', */+=#"1/5, !*/+%5 ' $/'66#/5. C (#)#)'# 9+)$#9%$! .5*+,)#)'& ')'=''/1#$%& +*#/!$+/+( EXECUTE, 9+$+/53 '(##$ %,#"14@## <+/(!,-)+# +*'%!)'#: !"#$%%,&'("%)*+ ,'-$+.!'* % DML /'%00+'*: {EXEC | EXECUTE} AS { CALLER | SELF | OWNER | ' !"_#$%&'$()*+%"'} DDL /'%00+'* !'-1"2 3(4* .(""*&: { EXEC | EXECUTE } AS { CALLER | SELF | ' !"_#$%&'$()*+%"'} DDL /'%00+'* !'-1"2 5+'1+'(: { EXEC | EXECUTE } AS { CALLER | SELF | ',-+*.)"_')# /&' } 6(,'-5*: { EXEC | EXECUTE } AS { SELF | OWNER | ' !"_#$%&'$()*+%"'} D,42#.+# %,+.+ CALLER +*/#"#,$, 2$+ (+"1,- .5*+,)$%& . 9+)$#9%$# *+,- +.!$#,&, ')'=''/14@#6+ .5*+,)#)'#. E,& F$+6+ 1 "!))+6+ *+,- +.!$#,& "+">)5 85$- %++$.#$%$.14@'# */'.#,#6'& )# $+,-9+ ",& !*1%9! (+"1,&, )+ ' )! "+%$1* 9+ .%#( +8G#9$!( 8! 5 "!))50, '%*+,- 1#(5( . F$+( (+"1,#. H)!2#)'# CALLER '%*+,- 1#$%& 9!9 )!2#)'# *+ 1(+,2!)'4 ",& .%#0 (+"1,#3 9/+(# !*/+%+.. I9! !$- 9+)9/#$)53 9+)$#9%$ .5*+,)#)'& (+>)+ )! %$!"'' %+ "!)'& */+=#"1/5. A!*/'(#/: USE MyDB; GO
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
19
CREATE PROCEDURE dbo.test1 WITH EXECUTE AS 'user1' AS SELECT user_name(); -- !"#$%&%'( )*+ -- (',-&'.! /!01"!#%('0+ GO E!,## */'(#)#)'# 1%$!)+.,#))+6+ 9+)$#9%$! .5*+,)#)'& 19! 5.!#$%& 9,42#.5( %,+.+( CALLER. A!*/'(#/: CREATE PROCEDURE dbo.test1 WITH EXECUTE AS 'name_user1' AS SELECT user_name(); -- 2!3(',4( #5/!03'3)+ -4(%3!#0'3 -- 3% name_user1 EXECUTE AS CALLER; SELECT user_name();(); -- 2!3(',4( #5/!03'3)+ -- -4(%3!#0'3 3% name_user2, -- #5/!03)#6'.! #5"!# *!7-0+ REVERT; SELECT user_name(); -- 2!3(',4( #5/!03'3)+ -4(%3!#0'3 -- 3% name_user1 GO J*#/!$+/ REVERT .5*+,)$ *#/#9,42#)'# 9+)$#9%$! .5*+,)#)'& )! )!2#)'# 19! !))+# . *+%,#")#( .5*+,)#))+( +*#/!$+/# EXECUTE AS. E,& 19! !)'& 9+)$#9%$! .5*+,)#)'& %#!)%! '%*+,- 1#$%& +*#/!$+/ EXECUTE AS, 9+$+/53 '(##$ %,#"14@## <+/(!,-)+# +*'%!)'#: { EXEC | EXECUTE ] AS <4/'8)9),%8)+_,!3(',4(%> [;] .7' < 4/'8)9),%8)+_,!3(',4(% >::=
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
20
{ LOGIN | USER } = 'name' [ WITH { NO REVERT | COOKIE INTO @varbinary_variable } ] | CALLER
1&2#*'+! (*3/+4E,& %+ "!)'& $!8,'=5 '%*+,- 1#$%& +*#/!$+/ CREATE TABLE, '(#4@'3 %,#"14@## <+/(!,-)+# +*'%!)'#: CREATE TABLE [ !"_0)'1_2)..13 . [/3+!)] . |/3+!) .] !"_*)0% 41 ( { | } [ ] [ ,...n ] ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ { TEXTIMAGE_ON { filegroup | "default" } ] [ ; ] <-,'+.+7+"%+_5/-73$(> ::= !"_/*$%04) <()/_7%335<> [ COLLATE 2+.* 5 6)*$7_,##$7"2$- (). " ] [ NULL | NOT NULL ] [ [ CONSTRAINT 2+.* 5 6)*$7_$87). -+. "_4+%$/*.$/* DEFAULT (17)9+. +_$87). -+. "_4+%$/*.$/* ] | [ IDENTITY [ ( .)-_'.)-+. + , .67+!+.* ) ] [ NOT FOR REPLICATION ]
]
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
21
] [ ROWGUIDCOL ] [ [ ...n ] ] %,_.(""*&> ::= [ type_schema_name . ] type_name [ ( precision [ , scale ] | max | [ { CONTENT | DOCUMENT } ] xml_schema_collection ) ] <-0'("%8+"%+_$+7-5/"-5/%_5/-73$(> ::= [ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor | WITH ( < index_option > [ , ...n ] ) ] [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] | [ FOREIGN KEY ] REFERENCES [ /3+!) . ] referenced_table_name [ ( ref_column ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( %$8_(17)9+. + ) }
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
22
<-,'+.+7+"%+_1*8%57%)%0-_5/-73$(> ::= !"_/*$%04) AS (1- /%"+!$+_(17)9+. + [ PERSISTED [ NOT NULL ] ] [ [ CONSTRAINT constraint_name ] { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor | WITH (
[ , ...n ] ) ] | [ FOREIGN KEY ] REFERENCES referenced_table_name [ ( ref_column ) ] [ ON DELETE { NO ACTION | CASCADE } ] [ ON UPDATE { NO ACTION } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] ] < -0'("%8+"%+_$+7-5/"-5/% /(37%$* > ::= [ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] (column [ ASC | DESC ] [ ,...n ] ) [ WITH FILLFACTOR = fillfactor |WITH ( [ , ...n ] ) ] [ ON { partition_scheme_name (partition_column_name)
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
23
| filegroup | "default" } ] | FOREIGN KEY ( column [ ,...n ] ) REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] (%$8_(17)9+. + ) } <-,$%%_%".+#5(> ::= { PAD_INDEX = { ON | OFF } | FILLFACTOR = fillfactor | IGNORE_DUP_KEY = { ON | OFF } | STATISTICS_NORECOMPUTE = { ON | OFF } | ALLOW_ROW_LOCKS = { ON | OFF} | ALLOW_PAGE_LOCKS ={ ON | OFF} } ;/' %+ "!)'' $!8,'=5 (+>#$ 85$- 19! !)! 8! ! "!))50 ' %0#(!, . 9+$+/14 81"1$ "+8!.,#)! $!8,'=!. J*'%!)'# %$+,8=+. 19! 5.!#$%& . 9/16,50 %9+89!0 2#/# !*&$14. 7$+,8=5 (+61$ 85$- ".10 .'"+.: %$+,8=5 !"!))+6+ $'*! ' .52'%,(5# %$+,8=5. ;/' %+ "!)'' $!8,'=5 (+>)+ +*/#"#,'$- ## /! 8'#)'#. J6/!)'2#)'& =#,+%$)+%$' ",& %$+,8=! (+61$ 19! 5.!$-%& %,#"14@'(' ! !(':
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
24
NOT NULL – . ,48+3 "+8!.,(+3 ',' ' (#)(+3 %$/+9# %$+,8#= .%#6"! "+,>#) '(#$- )!2#)'# +$,'2)+# +$ NULL; UNIQUE – .%# )!2#)'& %$+,8=! "+,>)5 85$- 1)'9!,-)5; PRIMARY KEY – 1%$!)!.,'.!#$ +"') %$+,8#= 9!9 *#/.'2)53 9,42 ' +")+./#(#))+ *+"/! 1(#.!#$ 2$+ .%# )!2#)'& %$+,8=! 81"1$ 1)'9!,-)5; CLUSTERED – +*/#"#,$ %+ "!)'# 9,!%$#/)+6+ ')"#9%!; NONCLUSTERED– +*/#"#,$ %+ "!)'# )#9,!%$#/)+6+ ')"#9%!; CHECK (condition) – 19! 5.!#(+# . %9+89!0 1%,+.'# '%*+,- 1#$%& ",& %/!.)#)'& )!2#)'# %$+,8=! ' .+ ./!@!#$ TRUE, FALSE ',' UNKNOWN. K%,' */' *+*5$9# .5*+,)#)'& SQL-+*#/!$+/! .+ ./!@!#(+# )!2#)'# /!.)+ FALSE, $+ +*#/!$+/ .5*+,)#) )# 81"#$; REFERENCES table (fields_list) – +6/!)'2#)'# $/#81#$ %+.*!"#)'& )!2#)'3 %$+,8=+. "!))+3 $!8,'=5 % 19! !))5(' %$+,8=!(' /+"'$#,-%9+3 $!8,'=5. J6/!)'2#)'& =#,+%$)+%$' ",& $!8,'=5 (+61$ 19! 5.!$-%& %,#"14@'(' ! !(': UNIQUE (column) – .%# )!2#)'& %$+,8=! "+,>)5 85$- 1)'9!,-)5; PRIMARY KEY(column) – 1%$!)!.,'.!#$ +"') %$+,8#= 9!9 *#/.'2)53 9,42 ' +")+./#(#))+ *+"/! 1(#.!#$ 2$+ .%# )!2#)'& %$+,8=! 81"1$ 1)'9!,-)5; CLUSTERED (column) – +*/#"#,$ %+ "!)'# 9,!%$#/)+6+ ')"#9%!; NONCLUSTERED (column) – +*/#"#,$ %+ "!)'# )#9,!%$#/)+6+ ')"#9%!; CHECK (condition) – 19! 5.!#(+# . %9+89!0 1%,+.'# '%*+,- 1#$%& ",& %/!.)#)'& )!2#)'# %$+,8=! ' .+ ./!@!#$ TRUE, FALSE ',' UNKNOWN. K%,' */' *+*5$9# .5*+,)#)'& SQL-+*#/!$+/! .+ ./!@!#(+# )!2#)'# /!.)+ FALSE, $+ +*#/!$+/ .5*+,)#) )# 81"#$;
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
25
FOREIN KEY (fields_list) – !" "#$%&'()&') *" +&),&)-. /01(. 203 4!"056"+ #%$%&!'$.)!, (!" +4) 7&%()&'3, ./%7%&&8) +" +&),&)/01() 5.2.! 4""!+)!4!+"+%!9 7&%()&'3- $"2'!)094/"#" /01(%, "5)4*)('+%3 4480"(&.1 6)0"4!&"4!9; REFERENCES table (fields_list) – "#$%&'()&') !$)5.)! 4"+*%2)&'3 7&%()&': 4!"056"+ 2%&&": !%50'68 4 ./%7%&&8-' 4!"056%-' $"2'!)094/": !%50'68. ;%50'68 -"#.! 58!9 *"4!"3&&8-' ' +$)-)&&8-'. < 4+"1 "()$)29 +$)-)&&8) !%50'68 2)03!43 &% 0"/%09&8) - 2"4!.*&8) !"09/" + $%-/%= 2%&&"#" 4)%&4%, ' #0"5%09&8) – 2"4!.*&8) +" +4)= 4)%&4%=. >-3 0"/%09&": !%50'68 &%('&%)!43 4 *$)?'/4% #, % '-3 #0"+%09&": – 4 *$)?'/4% ##.
!"#$"!%&'$( )&*"!+!% >7+0)()&') '&?"$-%6'' '7 5%78 2%&&8= +8*"0&3)!43 *"4$)24!+""*)$%!"$% 7%*$"4% SELECT, /"!"$8: '-))! 40)2.1@)) ?"$-%09&") "*'4%&'): SELECT [WITH [,...n]] < !"#$%&'% (#)"*+#> [ ORDER BY { !"#$%&'%_()* +,-"*(-.' #&'* | &-/%"_01-)23# [ ASC | DESC ] } [ ,...n ] ] [ COMPUTE { { AVG | COUNT | MAX | MIN | SUM } ( !"#$%&'% ) } [ ,...n ] [ BY !"#$%&'% [ ,...n ] ] ] [ FOR { BROWSE | <XML> } ] [ OPTION ( [ ,...n ] ) ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
26
< !"#$%&'%_(#)"*+#> ::= { <+)%,'-'.#,'/_(#)"*+#> | ( < !"#$%&'%_(#)"*+#> )} [ { UNION [ ALL ] | EXCEPT | INTERSECT } <+)%,'-'.#,'/_(#)"*+#> | ( < !"#$%&'%_(#)"*+#> ) [...n ] ] <+)%,'-'.#,'/_(#)"*+#> ::= SELECT [ ALL | DISTINCT ] [TOP !"#$%&'% [PERCENT] [ WITH TIES ] ] < +)'+*._ !0*"# > [ INTO &- #*_1#2)'3# ] [ FROM { <'+1*2/_3#04',#> } [ ,...n ] ] [ WHERE <5+4* '%> ] [ GROUP BY [ ALL ] !"#$%&'%_'0,-)45+%/-%_()*_6"+,,! [ ,...n ] [ WITH { CUBE | ROLLUP } ] ] [ HAVING < 5+4* '% > ] <XML>::= XML {{ RAW [ ( 'ElementName' ) ] | AUTO } [ [, BINARY BASE64 ][, TYPE ][, ROOT [('RootName')]] [, { XMLDATA | XMLSCHEMA [('TargetNameSpaceURI')]}] [, ELEMENTS [ XSINIL | ABSENT ] ] | EXPLICIT [ [, BINARY BASE64 ][, TYPE ][, ROOT [('RootName')]] [ , XMLDATA ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
27
] | PATH [ ( 'ElementName' ) ] [ [, BINARY BASE64 ][, TYPE ][, ROOT [('RootName')]] [ , ELEMENTS [ XSINIL | ABSENT ] ] ] }
A40' "*)$%!"$ SELECT +8*"0&3)!43 '7 *$'0"B)&'3 &% 2$.#"- 378/) *$"#$%--'$"+%&'3, !" ?"$-'$.)!43 $)7.09!'$.1@': &%5"$, $%7-)@%)-8: + *%-3!' *$'0"B)&'3 '0' 4)$+)$% CD, % 7%!)- *$'0"B)&') '7+0)/%)! 2%&&8) '7 $)7.09!'$.1@)#" &%5"$% + 4+"' *)$)-)&&8). E"40) ?$%78 SELECT ./%78+%)!43 4*'4"/ +8$%B)&':, "*$)2)031@': 7&%()&'3 ?"$-'$.)-8) 7%*$"4"-. < 4%-"- *$"4!"- 40.(%) 4*'4"/ +8$%B)&': 3+03)!43 4*'4/"- *"0): !%50'68. A40' !$)5.)!43 '7+0)()&') 7&%()&': +4)= *"0):, !" +-)4!" 4*'4/% *"0): -"B&" ./%7%!9 4'-+"0 *. F%*$'-)$: SELECT * FROM tbl1; . >-3 *"03 -"B)! 58!9 /+%0'?'6'$"+%&" '-)&)- !%50'68, ./%78+%)-8()$)7 !"(/.. F%*$'-)$: SELECT tbl1.f1, tbl2.f1 FROM tbl1, tbl2; . G$%7% FROM "*$)2)03)! "2&. '0' &)4/"09/" !%50'6 '0' *"27%*$"4"+, '4*"097.)-8= 203 '7+0)()&'3 2%&&8=. G$%7% WHERE "*$)2)03)! .40"+'), /"!"$"-. 2"0B&8 .2"+0)!+"$3!9 +4) 4!$"/', '4*"097.)-8) 203 ?"$-'$"+%&'3 $)7.09!'$.1@)#" &%5"$%. E$)2'/%! 4"2)$B'! "2&" '0' &)4/"09/" +8$%B)&':, +8*"0&31@'= 4$%+&)&'3. < +8$%B)&'3= -"#.! .(%4!+"+%!9 '-)&% 4!"056"+, ?.&/6'' %#$)#'$"+%&'3, *)$)-)&&8). H$"-) 4!%&2%$!&8= "*)$%!"$"+ 4$%+&)&'3, !%/'= /%/ =, <>, >, <, >=, <= + *$)2'/%!) -"#.! 58!9 '4*"097"+%&8 "*)$%!"$8 !%/'), /%/:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
28
BETWEEN – +"7+$%@%)! TRUE, )40' 7&%()&') &%="2'!43 + ./%7%&&"- 2'%*%7"&). F%*$'-)$: x BETWEEN y AND z /+'+%0)&!&" +8$%B)&'1 (x<=z) AND (x>=y); IN - 4"+*%2%)! 4 "2&'- '7 *)$)('40)&&8= + 4*'4/). F%*$'-)$: x IN (a,b,c); LIKE - +"7+$%@%)! TRUE 203 7&%()&':, 4"+*%2%1@'= 4 ./%7%&&": *"24!$"/": 4'-+"0"+. F%*$'-)$: x LIKE 'abc'; IS NULL - +"7+$%@%)! TRUE, )40' 7&%()&') $%+&" NULL. I!"! *$)2'/%! +"7+$%@%)! !"09/" 7&%()&') TRUE '0' FALSE F%*$'-)$: x IS NULL; EXISTS – !" *$)2'/%! 4.@)4!+"+%&'3, +"7+$%@%1@': 7&%()&') TRUE, )40' ./%7%&&8: + &)- *"27%*$"4 4"2)$B'! ="!3 58 "2&. 4!$"/.. F%*$'-)$: SELECT * FROM tbl1 t_out WHERE EXISTS (SELECT * FROM tbl1 t_in WHERE t_in.f1= t_out.f1); G$%7% GROUP BY "*)$%!"$% SELECT *$'-)&3)!43 203 "*$)2)0)&'3 #$.**8 4!$"/, &%2 /"!"$8-' +8*"0&31!43 ?.&/6'' %#$)#'$"+%&'3. A40' + "*)$%!"$) SELECT ./%7%&% ?$%7% GROUP BY, !" +4) '-)&% 4!"056"+, ./%78+%)-8) + 4*'4/) 203 "*$)2)0)&'3 4"72%+%)-"#" $)7.09!'$.1@)#" &%5"$%, 2"0B&8 58!9 ./%7%&8 4 ?.&/6'3-' %#$)#'$"+%&'3. ;%/ /%/ 203 /%B2": #$.**8 4!$"/ + $)7.09!'$.1@': &%5"$ 5.2)! +/01()&% !"09/" "2&% 4!$"/%, 4"2)$B%@%3 7&%()&'3 *"0.()&&8) ?.&/6'3-' %#$)#'$"+%&'3 &%2 2%&&": #$.**": 4!$"/. H ?.&/6'3- %#$)#'$"+%&'3 "!&"43!43 40)2.1@') ?.&/6'' 378/% TransactSQL: COUNT ( { [ [ ALL | DISTINCT ] +8$%B)&') ] | * } ) – *"24()! /"0'()4!+% 7&%()&': 4!"056"+ (ALL- +4)= 203 7%2%&&"#" +8$%B)&'3, DIST – .('!8+%3 !"09/" .&'/%09&8) &) $%+&8) NULL, * - *"24()! +4)= 4!$"/);
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
29
AVG – "*$)2)0)&') 4$)2&)#" 7&%()&'3; SUM – *"24()! 4.--8 +4)= 7&%()&'' #$.**8. A40' *$' !"*"0.(%)-") 7&%()&') +8="2'! 7% *$)2)08 4.--'$.)-"#" !'*% 2%&&8=, !" '&'6''$.)!43 ",'5/% +8*"0&)&'3 SQL-"*)$%!"$%; MAX - "*$)2)0)&') -%/4'-%09&"#" 7&%()&'3 '7 #$.**8; MIN - "*$)2)0)&') -'&'-%09&"#" 7&%()&'3 '7 #$.**8. G$%7% HAVING "*)$%!"$% SELECT "*$)2)03)! *$)2'/%! %&%0"#'(&" ?$%7) WHERE, &" *$'-)&3)-8: / 4!$"/%-, *"0.()&&8- + $)7.09!%!) +8*"0&)&'3 ?.&/6': %#$)#'$"+%&'3. J0)2.1@': *$'-)$ '0014!$'$.)! *$'-)&)&') #$.**. J!"05)6 id1 '-))! +4)#" !$' $%70'(&8= 7&%()&'3: 11, 22 ' 33. D03 /%B2": '7 !$)= #$.** &%="2'!43 -'&'-%09&") ' -%/4'-%09&") 7&%()&') 4!"056% f1. SELECT id1, MIN(f1), MAX(f1) FROM tbl1 GROUP BY id1; K)7.09!%!"- +8*"0&)&'3 !"#" SQL-"*)$%!"$% 5.2)! ?"$-'$"+%&') 40)2.1@'= 4!$"/: id1 MIN(f1) MAX(f1) –––––––––– –––––––––– –––––––––– 11 125 600 22 200 2300 33 100 450 E$' +85"$) 4 *$'-)&)&')- #$.** ' 4 2"*"0&'!)09&8- "#$%&'()&')- &% 7&%()&') + 4!"056) MIN(f1). SELECT dno, MIN(f1), MAX(f1) FROM tbl1 GROUP BY id1 HAVING MAX(f1) < 1000; < $)7.09!%!) +8*"0&)&'3 !"#" SQL-"*)$%!"$% 5.2.! +"7+$%@)&8 !"09/" 2+) 4!$"/': *)$+%3 ' *"40)2&33: id1 MIN(f1) MAX(f1)
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
30
–––––––––– –––––––––– –––––––––– 11 125 600 33 100 450 G$%7% FOR XML "*)$%!"$% SELECT"*$)2)03)!, (!" $)7.09!%! 7%*$"4% 5.2)! +"7+$%@)& + +'2) XML-2"/.-)&!%.
,!(-$'('$( .&/0$1 D03 4")2'&)&'3 !%50'6 4 "2&"'-)&&8-' 4!"056%-' '0' 4")2'&)&'' !%50'68 4 4%-": 4"5": '4*"097.1!43 %0'%48, 7%2%+%)-8) +" ?$%7) FROM ()$)7 ?$%7. AS *"40) '-)&' !%50'68. F%*$'-)$: select t1.f1, t1.f2, t2.f1, t2.f2 from tbl1 as t1, tbl1 as t2 where t1.f1= t2.f2; J")2'&)&') !%50'6 "*$)2)03)!43 ?$%7": FROM "*)$%!"$% SELECT, '-)1@): 40)2.1@)) ?"$-%09&") "*'4%&'): [ FROM { <'+1*2/_3#04',#> } [ ,...n ] ] <'+0*1/_2#34',#> ::= {'/*_1#2)'3!_')'_,"%(01# )%&'* [ [ AS ] #)'#0_1#2)'3! ] [ TABLESAMPLE [SYSTEM] ( sample_number [ PERCENT | ROWS ] ) [REPEATABLE ( repeat_seed )] ] [ WITH ( < table_hint > [ [ , ]...n ] ) ] | rowset_function [ [ AS ] #)'#0_1#2)'3! ] [ ( bulk_column_alias [ ,...n ] ) ] | user_defined_function [ [ AS ] #)'#0_1#2)'3!] | OPENXML | derived_table [ AS ] !" #_$ %!"&'
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ [ ( !" #_#$(!%& | < !"#$%&"'("_)*+,$-(> | |
31
[ ,...n ] ) ]
} < !"#$%&"'("_)*+,$-(> ::= { <$ .!#%*&_)*+,$-*> <)$/_ !"#$%"%$&> <$ .!#%*&_)*+,$-*> ON <0 ,!1$"> |<$ .!#%*&_)*+,$-*> CROSS JOIN <$ .!#%*&_)*+,$-*> | !)* +_"#,(-. +_$ %!"& { CROSS | OUTER } APPLY /0 * +_"#,(-. +_$ %!"& | [ ( ] < !"#$%&"'("_)*+,$-(> [ ) ] } <)$._ !"#$%"%$&> ::= [ { INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } } [ <join_hint> ] ] JOIN ::= "#,(-. +_$ %!"& PIVOT table_alias ::= ( 12.3&"+_ 40)4"0(* ."+ ( value_column ) FOR pivot_column IN ( ) ) ::=
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ "#,(-. +_$ %!"&
UNPIVOT
32 !" #_$ %!"&'
::= (value_column FOR pivot_column IN (< /$ !2_ )!,+-!1>)) < .$ !/_ )!,+-!0> ::= "5+_#$(!%& [ , ... ] !"# $%&'& FROM ()%*+*",*- .("** (+/(0 -&."#12 #"# )(+'&)%(!&, -( 3!* 4-# -&."#12 !(*+#/,5-!,. 6*%*7%*!-/(* !(*+#/*/#* (CROSS JOIN), /&'23&*8(* -&79* +*7&%-(328 )%(#'3*+*/#*8 !('+&*- %*':";-#%:5<#0 /&.(% !( 3!*8# 3('8(9/28# 7(8.#/&1#,8# !-%(7. =(*+#/*/#, )('3(",5- 32)("/,-; 3%*8*//(* (.>*+#/*/#* +&//2?, /* )%*+:!8(-%*//(* !?*8(0 (%(+#-*";!7#8# # 3/*@/#8# 7"5A&8#). =(*+#/,*82* -&."#12 )*%*A#!",5-!, A*%*' '&),-:5 3( $%&'* FROM ()*%&-(%& SELECT. B( $%&'* FROM 8(9/( #!)(";'(3&-; !"*+:5<#* -#)2 !(*+#/*/#0: CROSS JOIN - )*%*7%*!-/(* !(*+#/*/#*; INNER JOIN – 3/:-%*//** !(*+#/*/#*. C-( !(*+#/*/#* #!)(";':*-!, )( :8("A&/#5. 6%# 3/:-%*//*8 !(*+#/*/## 3 %*':";-#%:5<#0 /&.(% 37"5A&5-!, -(";7( -* !-%(7#, '/&A*/#, 7(-(%2? )( !(*+#/,*828 ((+/(#8*//28) !-(".1&8 !(3)&+&5-; LEFT [OUTER] JOIN – "*3(* 3/*@/** !(*+#/*/#*. 6%# 3/*@/*8 "*3(8 !(*+#/*/## 3 %*':";-#%:5<#0 /&.(% .:+:- 32.%&/2 3!* !-%(7# #' "*3(0 -&."#12 (:7&'23&*8(0 )*%3(0). 6%# !(3)&+*/## '/&A*/#0 )( !(*+#/,*828 ((+/(#8*//28) !-(".1&8 '/&A*/#, 3-(%(0 -&."#12 '&/(!,-!, 3 %*':";-#%:5<#0 /&.(% 3 !((-3*-!-3:5<#* !-%(7#. 6%# (-!:-!-3## !(3)&+*/#0 3 7&A*!-3* '/&A*/#0 3-(%(0 -&."#12 )%(!-&3",*-!, '/&A*/#* NULL;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
33
RIGHT [OUTER] JOIN – )%&3(* 3/*@/** !(*+#/*/#*. 6%# 3/*@/*8 )%&3(8 !(*+#/*/## 3 %*':";-#%:5<#0 /&.(% .:+:- 32.%&/2 3!* !-%(7# #' )%&3(0 -&."#12 (:7&'23&*8(0 3-(%(0). 6%# !(3)&+*/## '/&A*/#0 )( !(*+#/,*828 ((+/(#8*//28) !-(".1&8 '/&A*/#, )*%3(0 -&."#12 '&/(!,-!, 3 %*':";-#%:5<#0 /&.(% 3 !((-3*-!-3:5<#* !-%(7# (%#!. 3.6.). 6%# (-!:-!-3## !(3)&+*/#0 3 7&A*!-3* '/&A*/#0 )*%3(0 -&."#12 )%(!-&3",*-!, '/&A*/#* NULL; FULL [OUTER] JOIN - )("/(* 3/*@/** !(*+#/*/#*. 6%# )("/(8 3/*@/*8 !(*+#/*/## 3 %*':";-#%:5<#0 /&.(% .:+:- 32.%&/2 3!* !-%(7#, 7&7 #' )%&3(0, -&7 # #' "*3(0 -&."#12. 6%# !(3)&+*/## '/&A*/#0 )( !(*+#/,*828 ((+/(#8*//28) !-(".1&8 !-%(7& !(+*%9#'/&A*/#, # #' "*3(0 # #' )%&3(0 -&."#12. B )%(-#3/(8 !":A&*, 38*!-( (-!:-!-3:5<#? '/&A*/#0 3 !-(".12 -&."#12 ("*3(0 #"# )%&3(0) '&/(!#-!, '/&A*/#* NULL. D%&'& ON )('3(",*- 32)("/#-; *!-*!-3*//(* !(*+#/*/#* )( :7&'23&*8(8: )%*+#7&-:. B %*':";-#%:5<#0 /&.(% 32.#%&5-!, !-%(7#, :+(3"*-3(%,5<#* '&+&//(8: :!"(3#5. C-(- !)(!(. !(*+#/*/#, &/&"(E#A*/ !(*+#/*/#5 )( )%*+#7&-:, :7&'23&*8(8: $%&'(0 WHERE.
!"#$%&!'( =)*1#$#7&1#, '&)%(!&, :7&'23&*8&, 3 ()*%&-(%&? ,'27& Transact-SQL, ()#!23&*- )%+"!)#%(. 6(+'&)%(! ,3",*-!, (A*/; 8(28 !%*+!-3(8 ,'27& SQL, )('3(",, !-%(#-; !"(9/2* #*%&%?## '&)%(!(3, 8/(E(7%&-/( 32)("/,*82* 3 )%(1*!!* )(!-%(*/#, %*':";-#%:5<*E( /&.(%& #"# 32)("/*/#, (+/(E( #' ()*%&-(%(3 #'8*/*/#, +&//2? (DELETE, INSERT, UPDATE). F!"(3/( )(+'&)%(!2 #/(E+& )(+%&'+*",5- /& -%# -#)&, 7&9+20 #' 7(-(%2? ,3",*-!, !:9*/#*8 )%*+2+:<*E(: -&."#A/20 )(+'&)%(!, 3('3%&<&5<#0 /&.(% !-%(7 # !-(".1(3; )(+'&)%(! !-%(7#, 3('3%&<&5<#0 -(";7( (+/: !-%(7:, /( 3('8(9/( /*!7(";7( !-(".1(3;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
34
!7&",%/20 )(+'&)%(!, 3('3%&<&5<#0 '/&A*/#* (+/(E( !-(".1& 3 (+/(0 !-%(7*. 6(+'&)%(! )('3(",*- %*@&-; !"*+:5<#* '&+&A#: ()%*+*",-; /&.(% !-%(7, +(.&3",*820 3 -&."#1: /& (+/( 32)("/*/#* ()*%&-(%& INSERT; ()%*+*",-; +&//2*, 37"5A&*82* 3 )%*+!-&3"*/#*, !('+&3&*8(* ()*%&-(%(8 CREATE VIEW. +", ()%*+*"*/#, '/&A*/#0, 8(+#$#1#%:*82? ()*%&-(%(8 UPDATE; +", :7&'&/#, (+/(E( #"# /*!7(";7#? '/&A*/#0 3( $%&'&? WHERE # HAVING ()*%&-(%& SELECT; +", ()%*+*"*/#, 3( $%&'* FROM -&."#12 7&7 %*':";-&-& 32)("/*/#, )(+'&)%(!&; G&)%#8*%: SELECT * INTO t1 FROM (select * from tbl1 WHERE f2 LIKE 'aa%') +", )%#8*/*/#, 7(%%*"#%(3&//2? )(+'&)%(!(3. 6(+'&)%(! /&'23&*-!, 7(%%*"#%(3&//28, *!"# '&)%(!, !(+*%9&<#0!, 3 )%*+#7&-*, #8**- !!2"7: /& '/&A*/#* #' -&."#12 (3/*@/*0 7 +&//(8: '&)%(!:), 7(-(%&, )%(3*%,*-!, )(!%*+!-3(8 +&//(E( )%*+#7&-&.
)*+,"-.,.-/ H'27 Transact-SQL )%*+(!-&3",*- +3& !)(!(.& (.>*+#/*/#, -&."#1: :7&'23&, !(*+#/,*82* -&."#12 (37"5A&, )(+'&)%(!2) 3( $%&'* FROM ()*%&-(%& SLECT. B 4-(8 !":A&* !/&A&"& 32)("/,*-!, !(*+#/*/#* -&."#1, & :9* )(-(8 7 )(":A*//(8: 8/(9*!-3: )%#8*/,5-!, :!"(3#,, :7&'&//2* $%&'(0 WHERE, &E%*E#%(3&/#*, ()%*+*",*8(* $%&'(0 GROUP BY, :)(%,+(A#3&/#* +&//2? # -.).;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
35
()%*+*",, (.>*+#/*/#* %*':";-#%:5<#? /&.(%(3, )(":A*//2? )%# (.%&.(-7* ()*%&-(%& SELECT. B 4-(8 !":A&* +3& ()*%&-(%& SELECT !(*+#/,5-!, $%&'(0 UNION, INTERSECT #"# EXCEPT. I", (.>*+#/*/#, %*':";-&-(3 /*!7(";7#? '&)%(!(3 3 (+#/ %*':";-#%:5<#0 /&.(% 3 ()*%&-(%* SELECT #!)(";':*-!, $%&'& UNION: { < /"-$3$2*-$&_4*/5! *> | ( <1(5*6"%$"_4*/5! *> ) } UNION [ ALL ] < /"-$3$2*-$&_4*/5! *> | ( <1(5*6"%$"_4*/5! *> ) [UNION [ ALL ] < /"-$3$2*-$&_4*/5! *> | ( <1(5*6"%$"_4*/5! *> ) [ ...n ] ] D%&'& UNION (.>*+#/,*- %*':";-&-2 +3:? '&)%(!(3 )( !"*+:5<#8 )%&3#"&8: 7&9+20 #' (.>*+#/,*82? '&)%(!(3 +("9*/ !(+*%9&-; (+#/&7(3(* A#!"( !-(".1(3; -#) '/&A*/#0 #' )()&%/( (.>*+#/,*82? !-(".1(3 +("9*/ .2-; (+#/&7(328 #"# )%#3(+#828. J&7 /*";', (.>*+#/,-; '/&A*/#, #' !-(".1& -#)& integer # !-(".1& -#)& varchar; #' %*':";-#%:5<*E( /&.(%& &3-(8&-#A*!7# #!7"5A&5-!, !(3)&+&5<#* !-%(7#. D%&'& UNION ALL 32)("/,*- (.>*+#/*/#* +3:? )(+'&)%(!(3 &/&"(E#A/( $%&'* ALL !( !"*+:5<#8# #!7"5A*/#,8#: !(3)&+&5<#* !-%(7# /* :+&",5-!, #' $(%8#%:*8(E( %*':";-#%:5<*E( /&.(%&; (.>*+#/,*82* '&)%(!2 323(+,-!, 3 %*':";-#%:5<*8 /&.(%* )(!"*+(3&-*";/( .*' :)(%,+(A#3&/#,. 6%# (.>*+#/*/## .("** +3:? '&)%(!(3 +", #'8*/*/#, )(%,+7& 32)("/*/#, ()*%&1## (.>*+#/*/#, 8(9/( #!)(";'(3&-; !7(.7#
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
36
D%&'& INTERSECT )('3(",*- 32.%&-; -(";7( -* !-%(7#, 7(-(%2* )%#!:-!-3:5- 3 7&9+(8 (.>*+#/,*8(8 %*':";-#%:5<*8 /&.(%*. D%&'& EXCEPT )('3(",*- 32.%&-; -(";7( -* !-%(7#, 7(-(%2* )%#!:-!-3:53 )*%3(8 (.>*+#/,*8(8 %*':";-#%:5<*8 /&.(%*, /( (-!:-!-3:5- 3( 3-(%(8 %*':";-#%:5<*8 /&.(%*. 6%# 32)("/*/## (.>*+#/*/#, '&)%(!(3, :7&'23&*82? $%&'&8# UNION, EXCEPT # INTERSECT, !:<*!-3:5- !"*+:5<#* (E%&/#A*/#,: $%&': INTO 8(9*- !(+*%9&-; -(";7( )*%320 )(+'&)%(!; $%&'& ORDER BY )%#8*/,*-!, -(";7( 7( 3!*8: ()*%&-(%: (:7&'23&*-!, 3 7(/1*), )%#8*/*/#* 4-(0 $%&'2 7 "5.(8: )(+'&)%(!:, 3?(+,<*8: 3 (.>*+#/*/#* /* +():!7&*-!,; $%&'2& GROUP BY # HAVING 8(E:- )%#8*/,-;!, -(";7( 7 )(+'&)%(!&8, )%#8*/,-; #? 7 )(":A&*8(8: 7(/*A/(8: %*':";-#%:5<*8: /&.(%: /*";',; $%&'& FOR BROWSE /* 8(9*- .2-; :7&'&/& 3 ()*%&-(%&?, #!)(";':5<#? $%&'2 UNION, EXCEPT # INTERSECT. B ()*%&-(%* INSERT 8(9/( #!)(";'(3&-; $%&'2 UNION, EXCEPT # INTERSECT.
)%,&$0!&( 1%&$23,.-/ "$..(4K ()*%&-(%&8 :)%&3"*/#, +&//28# (-/(!,-!, ()*%&-(%2 UPDATE, INSERT # DELETE. L)*%&-(% UPDATE 32)("/,*- #'8*/*/#* +&//2? -&."#12 #"# )%*+!-&3"*/#, # #8**- !"*+:5<** $(%8&";/(* ()#!&/#*: [ WITH [...n] ] UPDATE [ TOP ( *'0 6).") ) [ PERCENT ] ] { | rowset_function_limited [ WITH ( [ ...n ] ) ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
37
} SET { "5+_#$(!%& = { *'0 6).") | DEFAULT | NULL } | {udt_column_name.{{ property_name = expression | field_name = expression } | method_name (argument [,...n ]) } } | !"_#$%&'() {.WRITE (*+,)-./ . ,@Offset ,@Length)} | @variable = expression | @variable = column = expression [ ,...n ] } [ ,...n ] [ < !"#"_OUTPUT> ] [ FROM{ } [ ,...n ] ] [ WHERE { <search_condition> | { [ CURRENT OF { { [ GLOBAL ] cursor_name } | cursor_variable_name } ] } } ] [ OPTION ( [ ,...n ] ) ] [ ; ] < !"#$%> ::= { [ #.,*., . ')0)_1)//+2 . #2.!) . | ')0)_1)//+2 .[ #2.!) ] . | #2.!) .
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ ]
38
!"_$)'& (+_ & _3,.1#$)*&./ "}
!"#$% INSERT !#'(")*"%+* ,-* ,&.$/-")'* +%#&0 / %$.-'12 '-' !#",+%$/-")'" ' '(""% +-",234"" 5($-6)&" &!'+$)'": [ WITH [ ,...n ] ] INSERT [ TOP ( *+,)-./ . ) [ PERCENT ] ] [ INTO] { <$%&'()> | rowset_function_limited [ WITH ( [ ...n ] ) ] } { [ ( column_list ) ] [ < !"#"_OUTPUT> ] { VALUES ( { DEFAULT | NULL | *+,)-./ . } [ ,...n ] ) | derived_table | execute_statement } } | DEFAULT VALUES [; ] !"#$% DELETE /7!&-)*"% 2,$-")'" +%#&0 '8 %$.-'17 '-' !#",+%$/-")'* ' '(""% +-",234"" 5($-6)&" &!'+$)'": [ WITH [ ,...n ] ] DELETE [ TOP ( *+,)-./ . ) [ PERCENT ] ] [ FROM ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
39
{ <$%&'()> | rowset_function_limited [ WITH ( [ ...n ] ) ] } [ < !"#"_OUTPUT> ] [ FROM [ ,...n ] ] [ WHERE { <search_condition> | { [ CURRENT OF { { [ GLOBAL ] !"_45,#%,) } | !"_3.,.!.//%6_45,#%,) } ] } } ] [ OPTION ( [ ,...n ] ) ] [; ] 9#$8$ WITH &!#","-*"% /#"("))7: '(")2"(7: #"82-6%'#234': )$., '+!&-682"(7: ,#2;'(' &!"#$%$(' (&,2-*. <$!#'("#: USE MYDB; WITH NewRes(f1, f3) AS ( SELECT f1, COUNT(*) FROM Tbl1 AS t1 WHERE f1 IS NOT NULL GROUP BY f1 ) SELECT f1, f3 FROM NewRes ORDER BY f1;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ = !&-" f3 8$!#&+$ '8 NewRes &%&.#$>$"%+* 0&-'?"+%/& +%#&0 / ;#2!!", +&8,$/$"(&: !& !&-3 f1.
40
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
41
!"# 3. $%&'%#""(#) *%!+# SQL-*!%,!%#. !"# Transact-SQL @870 Transact-SQL !&8/&-*"% 8$!'+7/$%6 /7#$>")'*, &.A*/-*%6 0&)+%$)%7 ' !"#"("))7", #"$-'8&/7/$%6 !#&1",2#7. .A*/-")'" !"#"("))7B /7!&-)*"%+* &!"#$%&( DECLARE. C(* &.A*/-*"(&: !"#"("))&: )$?')$"%+* + +'(/&-$ @. @870 Transact-SQL !&8/&-*"% '+!&-68&/$%6 .&-6D&: )$. %'!&/ ,$))7B (/0-3?$* %'!7 ,$))7B, !#'+24'" Microsoft SQL Server, +!"1'$-6)7" %'!7 ,$))7B (uniqueidentifier,sql_variant) ' %'!XML).
$%&" '())"* E'!7 ,7))7B '("3% )" %&-60& !"#"("))7", )& ' %$0'" &.A"0%7 0$0: +%&-.17 %$.-'1 ' !#",+%$/-")':; !$#$("%#7 B#$)'(7B !#&1",2#; /&8/#$4$"(7" 8)$?")'* ,-* 52)01':. = *870" Transact-SQL !&,,"#>'/$3%+* +-",234'" %'!7 ,$))7B: !"#$%&"!''(!: int &% -2^31 (-2,147,483,648) ,& 2^31-1 (2,147,483,647) 4 .$:%$ smallint -2^15 (-32,768) to 2^15-1 (32,767) 2 .$:%$ tinyint &% 0 ,& 255 1 .$:% bigint &% -2^63 (-9,223,372,036,854,775,808) to 2^63-1 ,& (9,223,372,036,854,775,807) 8 .$:% bit 1, 0 '-' NULL )!*!&+)!''(! & ,%-&%.#)/''#0 +#$'#&+12: numeric ($0+'($-6)& ,&!2+%'(&" ?'+-& 8)$0&/ 38 decimal ($0+'($-6)& ,&!2+%'(&" ?'+-& 8)$0&/ 38 )!*!&+)!''(! & 3"/)/2*!0 +#$-#0: float - 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
42
float [ ( n ) ] – n ?'+-& !"#$, !%'()*+,#+ ,-."!%%#/ (#" 1 0# 53); $ )-$!%!,#%"! #" 1"#2# ).-3+.!+ )-.!,-+" 4 -/"- (#"1 0# 24, "#3.#%"( 7 ).-4#$) !'! 8 -/"#$ (25-53, "#3.#%"( 15 ).-4#$) real - 3.40E + 38 to -1.18E - 38, 0 and 1.18E - 38 to 3.40E + 38 !"!#"$% money #" -922,337,203,685,477.5808 0# 922,337,203,685,477.5807 8 -/"#$ smallmoney #" - 214,748.3648 0# 214,748.3647 4 -/"&'()*+,"$!: char varchar text* &'()*+,"$!, '&-*+,./01'! Unicode: nchar ntext* nvarchar )*'2"$!: binary varbinary image* 3'-$ 43$/)5!(!"': datetime smalldatetime +6 547*3$ & 8/5&*5*( cursor (,#5.# !%'()#$-"( "#'(4# 0'6 &+7+,+..89 ! &-7-,+"7#$ 97-.!,89 &7#:+0*7) +6 954"!"'6 5!./+,3'5/01!:* "47*54: table *&34+,"$! 3'-$:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
43
sql_variant – 0'6 97-.+.!6 &+7+,+..89 '; #2# "!&- ()- .+4#"#78, !%4';3+.!+,) uniqueidentifier – 0'6 97-.+.!6 16 -/"#$#2# GUID xml – 0'6 0-..89 $ XML-<#7,-"+ timestamp – %!.#.!, "!& rowversion. =!&8 ,+3+..8+ %!,$#'#, * .+ &7+0'-2-+"%6 +75!$-"( $ %'+0*;>!9 $+7%!69 Microsoft SQL Server. Transact-SQL )$#'6+" &7!,+.6"( 0$- "!&- !0+."!
!"#$%' ()'*$ Transact-SQL A)84 Transact-SQL +75!$-+" %'+0*;>!+ 4#.%"7*4:!!: !"#: BEGIN END, $%!"&'() "*+,-.",: IF Boolean_expression { sql_statement | statement_block } [ ELSE { sql_statement | statement_block } ] /0#! WHILE: WHILE Boolean_expression { sql_statement | statement_block } [ BREAK ] { sql_statement | statement_block } [ CONTINUE ] { sql_statement | statement_block } 1$'#/02 CASE: CASE input_expression WHEN when_expression THEN result_expression
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ [ ...n ] [ ELSE else_result_expression ] END 1$'#/02 CASE: CASE WHEN Boolean_expression THEN result_expression [ ...n ] [ ELSE else_result_expression ] END
<*.4:!6 COALESCE. A)84 Transact-SQL &7+0#%"-$'6+" .- #7 $%"7#+..89 <*.4:!/ (,-"+,-"!3+%4!+, %"7#4#$8+, <*.4:!! 0-"8/$7+,+.!, <*.4:!! 4#.-;>!9 4#0 #&+7-"#7#$ )-&7#%-. ?'6 0# -$'+.!6 0-..89 &7+0#%"-$'6+"%6 #&+7-"#7 MERGE. A)84 Transact-SQL )$#'6+" $8'.6"( # 7- #"4* !%4';3+.!/ %7+0%"$#, #&+7-"#7- TRY-CATCH.
+&,-./ ?'6 %#)0-.!6 97-.!,89 &7#:+0*7 !%'()*+"%6 #&+7-"#7 CREATE PROCEDURE, 4#"#78/ !,++" %'+0*;>++ <#7,-'(.#+ #&!%-.!+:
44
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
45
CREATE { PROC | PROCEDURE } [ !"#$.] %#&_'()*"+,(- [ ; number ] [ { @'$($#".( [ !"#$_.%'_+$//-!. ] .%'_+$//-! } [ VARYING ] [ = default ] [ [ OUT [ PUT ] ] [ ,...n ] [ WITH [ ENCRYPTION ] [ RECOMPILE ] [ 1,-3-_EXECUTE_AS ] [ FOR REPLICATION ] AS { {[ BEGIN ] )'"($.)(- [ END ]} [;][ ...n ] | EXTERNAL NAME 0)(1$.12$ .#".)+ } [;] ?'6 %#)0-.!6 <*.4:!/ !%'()*+"%6 #&+7-"#7 CREATE FUNCTION, 4#"#78/ !,++" %'+0*;>++ <#7,-'(.#+ #&!%-.!+: !"#$%&"$ '(&!)*$: CREATE FUNCTION [ !"#$. ] %#&_3,/1*%% ( [ { @'$($#".( [ AS ][ !"#$_.%'$_+$//-!. ] .%'_'$($#".$ [ = default ] } [ ,...n ] ] ) RETURNS .%'_4)54($6$"#)7)_5/$8"/%& [ WITH [ ,...n ] ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ [ AS ] BEGIN ."2) 3,/1*%% RETURN 4)54($6$"#)" 5/$8"/%" END [ ; ] Inline '(&!)*$: CREATE FUNCTION [ !"#$. ] %#&_3,/1*%% ( [ { @'$($#".( [ AS ] [ !"#$_.%'$_+$//-!. ] .%'_'$($#".($ [ = default ] } [ ,...n ] ] ) RETURNS TABLE [ WITH <"*/00_1$'#/00> [ ,...n ] ] [ AS ] RETURN [ ( ] "*+,-.",_select [ ) ] [ ; ] +(&!)*$, ,-.,%"/"0/"$ 1"2#*)(: CREATE FUNCTION [ !"#$. ] %#&_3,/1*%% ( [ { @'$($#".( [ AS ] [ !"#$_.%'$_+$//-!. ] .%'_'$($#".($ [ = default ] } [ ,...n ] ] )
46
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
47
RETURNS @return_variable TABLE <"*,+4+!+'0+_.- !0/(> [ WITH <"*/00_1$'#/00> [ ,...n ] ] [ AS ] BEGIN ."2)_3,/1*%% RETURN END [ ; ] CLR '(&!)*$: CREATE FUNCTION [ schema_name. ] function_name ( { @parameter_name [AS] [ type_schema_name. ] parameter_data_type [ = default ] } [ ,...n ] ) RETURNS { return_data_type | TABLE } [ WITH [ ,...n ] ] [ AS ] EXTERNAL NAME assembly_name.class_name.method_name [ ; ] <-3)**_'(&!)**>::= { [ ENCRYPTION ] | [ SCHEMABINDING ] | [ RETURNS NULL ON NULL INPUT | CALLED ON NULL INPUT] | [ EXECUTE_AS_1,-3- ] }
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
48
<-3)**_clr_'(&!)**>::= } [ RETURNS NULL ON NULL INPUT | CALLED ON NULL INPUT ] | [ EXECUTE_AS_1,-3- ] } <-3%454#4&*4_1"2#*)6>:: = ( { <)'("+"2"/%"_ .)20*$> <)7($/%8"/%&_*"2) ./) .%_ .)20*$> | <"*,+4+!+'0+_&(50%!06"7"_%."! /-> } [ <"7,-'05+'02_/+!"%.'"%.0_.- !0/(> ] [ ,...n ] ) ::= ( { .)20"* .%'_+$//-! } [ ,...n ] ) <-3%454#4&*4_71-#2)">::= { { .)20"* .%'_+$//-! } [ [ DEFAULT constant_expression ] [ COLLATE collation_name ] | [ ROWGUIDCOL ] ] | [ IDENTITY [ (/$8_5/$8"/%" , '(%($6"/%" ) ] ] [ <"7,-'05+'0+_/+!"%.'"%.0_%."! /(> [ ...n ] ] } <-8%"&*94&*4_ !"#$%$%'_$%#"( )>::= { [ NULL | NOT NULL ] { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
49
[ WITH FILLFACTOR = fillfactor | WITH ( < !"##_#$%&'() > [ , ...n ] ) [ ON { filegroup | "default" } ] | [ CHECK ( logical_expression ) ] [ ,...n ] } <#*+!,!"!&'!_-./'$"'0#1#_$%#"( )>::= !"#$%& AS '()*+%,-% <#1+)&'/!&'!_ !"#$%$%'_%)("' .>::= { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] ( !"#$%& [ ASC | DESC ] [ ,...n ] ) [ WITH FILLFACTOR = fillfactor | WITH ( < !"##_#$%&'()> [ , ...n ] ) | [ CHECK ( logical_expression ) ] [ ,...n ] } <#* ''_'&,!2$)>::= { PAD_INDEX = { ON | OFF } | FILLFACTOR = fillfactor | IGNORE_DUP_KEY = { ON | OFF } | STATISTICS_NORECOMPUTE = { ON | OFF } | ALLOW_ROW_LOCKS = { ON | OFF } | ALLOW_PAGE_LOCKS ={ ON | OFF } } !"#$%!&'() *)+,#-$%). ")&$/.#-0) +!1!%.-1)0 *&2 3,("4$$ – 1024. 5%2 +!1!%.-1! 3,("4$$ (!/$(!.-#2 # +1.3$"#! @.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
50
6&.*,78$9 +1$%.1 $&&7#-1$1,.- +1$%.(.($. inline-3,("4$$, 0):01!8!78.9 -!;&$4,: CREATE FUNCTION S1.MyFn (@i1 int) RETURNS TABLE AS RETURN ( SELECT t1.ID, t1.Name, SUM(t2.F3) AS '* +& (,--.' FROM tbl1 AS t1 JOIN tbl2 AS t2 t2.ID = t1.ID JOIN tbl3 AS t3 ON t3.IDzak = t2.IDzak WHERE t3.IDcust = @i1 GROUP BY t1.ID, t1.Name ); GO <(!/.($2 3,("4$9, 0):01!8!78$= -!;&$4,, %)>,- ;?-' $#+)&':)0!(? 0 SQL-)+.1!-)1!= -!"$% @. );1!:)%, "!" $ +)*:!+1)#?. A!+1$%.1, 0?:)0 3,("4$$ %)@.- ;?-' ,"!:!( 0) 31!:. FROM )+.1!-)1! SELECT #&.*,78$% );1!:)%: SELECT * FROM S1.MyFn (55); -- !)/)-&0/ ,')1.2)&0 -- 1$)3&$#& ! +4 IDcust 2 tbl3
!"# 4. $!%#&'(") *+#,!&&-.- +-/0*1#. 2&0!34!5/ ODBC. !"#$%&$'!( ODBC 5(-.13.9# ODBC (Open Database Connectivity) ;?& 1!:1!;)-!( 3$1%)9 Microsoft "!" )-"1?-?9 $(-.13.9# *)#-,+! " ;!:!% *!((?=, +1.*)#-!0&278$9 ,($3$4$1)0!((?. #1.*#-0! 0:!$%)*.9#-0$2 +1$"&!*()9
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
51
+1)>1!%%?, (!:?0!.%)9 "&$.(-)% ($&$ +1$&)@.($.%-"&$.(-)%) # #.10.1)% – ;!:)9 *!((?=. B )#()0, $(-.13.9#! ODBC ;?&$ +)&)@.(? #+.4$3$"!4$2 CLI-$(-.13.9#! (Call-Level Interface), 1!:1!;)-!((!2 X/Open, $ ISO/IEC *&2 API ;!: *!((?=, $ 2:?" SQL (Structured Query Language), "!" #-!(*!1- 2:?"! *)#-,+! " ;!:!% *!((?=. 5(-.13.9# ODBC +1)."-$1)0! *&2 +)**.1@"$ %!"#$%!&'()9 $(-.1)+.1!;.&'()#-$ +1$&)@.($9, );.#+./$0!78.9 ,($3$4$1)0!((?9 *)#-,+ &7;)>) +1$&)@.($2, $#+)&':,78.>) ODBC, " 1!:&$/(?% $#-)/($"!% *!((?=. C&2 0:!$%)*.9#-0$2 # ;!:)9 *!((?= +1$&)@.($.-"&$.(- 0?:?0!.- 3,("4$$ $(-.13.9#! ODBC, ")-)1?. 1.!&$:)0!(? 0 #+.4$!&'(?= %)*,&2=, (!:?0!.%?= ODBC-*1!90.1!%$. D!" +1!0$&), ODBC-*1!90.1? E-) DLL;$;&$)-."$. F1$ E-)% )*(! DLL-;$;&$)-."! %)@.- +)**.1@$0!-' (.#")&'") ODBC-*1!90.1)0. F1$ ,#-!()0". (! ")%+'7-.1 &7;)>) SQL-#.10.1! !0-)%!-$/.#"$ 0?+)&(2.-#2 1.>$#-1!4$2 0 1..#-1. Windows $ #))-0.-#-0,78.>) ODBC-*1!90.1!. G1=$-."-,1! ODBC +1.*#-!0&2.-#2 /.-?1'%2 ")%+)(.(-!%$: 3#0,%1*-0*-',0*-&, 0?+)&(278.. 0?:)0 3,("4$9 ODBC. 4*-*+1*# +#!25*#%5, :!>1,@!78$9 $ )#0);)@*!78$9 ODBC*1!90.1?, -1.;,.%?. +1$&)@.($2%-"&$.(-!%. .(.*@.1 *1!90.1)0 );1!;!-?0!.- 0?:)0? ODBC-3,("4$9 $&$ +.1.*!.- $= *1!90.1,. ODBC-+#!25*#, );1!;!-?0!78$9 0?:)0? SQL-3,("4$9, +.1.*!0!2 SQL-#.10.1, 0?+)&(2.%?9 SQL-)+.1!-)1, ! +1$&)@.($7-"&$.(-, – 1.:,&'-!- 0?+)&(.($2 0?:0!(()9 3,("4$$. 6(&%7-0' +!--./, )+1.*.&2.%?9 "!" ")("1.-(!2 &)"!&'(!2 $&$ ,*!&.((!2 ;!:! *!((?=.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
52
H#()0(). (!:(!/.($. %.(.*@.1! *1!90.1)0 – :!>1,:"! *1!90.1!, #))-0.-#-0,78.>) +)*"&7/!.%)%, $#-)/($", *!((?= $ $("!+#,&24$2 0:!$%)*.9#-0$2 # 1!:&$/(?%$ -$+!%$ $#-)/($")0 *!((?= +)#1.*#-0)% +1$%.(.($2 1!:&$/(?= ODBC-*1!90.1)0.
)*$+,-#& .(--/" C&2 +)*"&7/.($2 " SQL-#.10.1, # $#+)&':)0!($.% 3,("4$9 ODBC API +.10)(!/!&'() #&.*,.- #):*!-' $#-)/($" *!((?= DSN, 0 ")-)1)% )+1.*.&2.-#2 $#+)&':,.%). $%2 $#-)/($"! *!((?= $ %.#-)+)&)@.($. ;!:? *!((?= (0 :!0$#$%)#-$ )- ")("1.-()9 IC E-) %)@.- ;?-' $%2 3!9&! ;!:? *!((?= $&$ $%2 #.10.1! $ $%2 ;!:? *!((?=). 6):*!-' $#-)/($" *!((?= %)@() "!" +1)>1!%%() - 0?:0!0 3,("4$7 ODBC API, -!" $ $(-.1!"-$0() – $#+)&':,2 ,-$&$-, ODBC. DLL-;$;&$)-."! ODBCCP32.DLL +1.*)#-!0&2.- 3,("4$$ ODBC API ConfigDSN $ SQLConfigDataSource, +):0)&278$. 0?+)&(2-' 1.>$#-1!4$7 ()0?= $#-)/($")0 *!((?= $&$ ,*!&2-' $(3)1%!4$7 ); $#-)/($"!= *!((?= $: 1..#-1! Windows. C&2 $#+)&':)0!($2 0 #1.*. Visual Studio 3,("4$9 ConfigDSN $ SQLConfigDataSource #&.*,.- +)*"&7/$-' :!>)&)0)/(?9 3!9& odbcinst.h. J,("4$2 ConfigDSN +):0)&2.- *);!0&2-', $:%.(2-' $&$ ,*!&2-' $#-)/($"$ *!((?= $ $%..- #&.*,78.. 3)1%!&'(). )+$#!($.: BOOL ConfigDSN( HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes); . F!1!%.-1 fRequest ,"!:?0!.- -$+ :!+1)#!, ")-)1?9 :!*!.-#2 )*()9 $: #&.*,78$= ")(#-!(-: ODBC_ADD_DSN – *);!0&.($. ()0)>) $#-)/($"! *!((?=; ODBC_CONFIG_DSN – $:%.(.($. #,8.#-0,78.>) $#-)/($"! *!((?=;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
53
ODBC_REMOVE_DSN – ,*!&.($. #,8.#-0,78.>) $#-)/($"! *!((?=.
F!1!%.-1 lpszDriver #)*.1@$- )+$#!($. *1!90.1!, ! +!1!%.-1 lpszAttributes – #+$#)" !-1$;,-)0 0 3)1%. ""&7/.0). #&)0)=:(!/.($." ((!+1$%.1: DSN=MyDB\0UID=User11\0PWD=P1\0DATABASE=DB1\0\0). 6+$#)" !-1$;,-)0 :!0.1K!.-#2 *0,%2 null-;!9-!%$. F1$ ,#+.K()% :!0.1K.($$ 3,("4$2 0):01!8!.- :(!/.($. TRUE.
0+./ 1+21!($( B#. 3,("4$$ ODBC API 0):01!8!7- :(!/.($2, (!:?0!.%?. ")*!%$ 0):01!-!. D)* 0):01!-! )+1.*.&2.-, ;?&! &$ 3,("4$2 0?+)&(.(! ,#+.K() $&$ =!1!"-.1$:,.- -$+ +1)$:)K.*K.9 )K$;"$. B :!>)&)0)/()% 3!9&. sql.h )+1.*.&.(? #&.*,78$. ")*? 0):01!-!: #define SQL_SUCCESS 0 J,("4$2 0?+)&(.(! ,#+.K(). #define SQL_SUCCESS_WITH_INFO 1 J,("4$2 0?+)&(.(! ,#+.K(), () # ,0.*)%$-.&'(?% #));8.($.%. #if (ODBCVER >= 0x0300) I)&'K. (.- #-1)" *&2 #define SQL_NO_DATA 100 $:0&./.($2 $= $: #endif 1.:,&'-$1,78.>) (!;)1!. B +1.*?*,8.9 0.1#$$ ODBC API E-)- ")* 0):01!-! );):(!/! "!" SQL_NO_DATA_FOUND. B 0.1#$$ 3.x ")* 0):01!-! SQL_NO_DATA_FOUND #)*.1@!-#2 0 :!>)&)0)/()% 3!9&. sqlext.h. #define SQL_ERROR (-1) F1$ 0?+)&(.($$ 3,("4$$ +1)$:)K&! )K$;"!. #define SQL_INVALID_HANDLE (-2) L"!:!( (.0.1(?9
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
54
*.#"1$+-)1. !"#$%&, '()*+"&,-.& ./%"01*""*, )*#. ", 2.',13,".. #define SQL_NEED_DATA 99 4+& !/),3"*5* '()*+","%& 6.""*7 8!"#$%% /+,6!,9 )1,6'.1%9,+:"* *)1,6,+%9: ",*;0*6%-(, 6.""(,. <,1'(, 6'. #*6. '*2'1.9. *)1,6,+&=9, >9* 8!"#$%& ;(+. '()*+","., . */9.+:"(, #*6( '*2'1.9. %"8*1-%1!=9 * 9%), )1*%2*3,63,7 *3%;#,. 4+& *)1,6,+,"%& 9%). #*6. '*2'1.9. ' 2.5*+*'*>"*- 8.7+, sqltypes.h '',6,"* /+,6!=?,, *;@&'+,"%,: typedef signed short RETCODE; #define SQL_STILL_EXECUTING
2
!"#$%% ODBC API A /+,6!=?,7 9.;+%$, )1,6/9.'+," /)%/*# */"*'"(0 8!"#$%7 ODBC API. !"#$%& '(%)*"%+ ,-+.%"+"%+ ) %)/-0"%#-1 .*""23 SQLAllocHandle
<*+!>.,9 %6,"9%8%#.9*1 (6,/#1%)9*1) /1,6(, /*,6%","%& %+% *),1.9*1., %+% 6,/#1%)9*1 )1%+*B,"%&.
SQLConnect
C*,6%","%, / %/9*>"%#*- 6.""(0 )* DSN, %-,"% % ).1*+= )*+:2*'.9,+&.
SQLDriverConnect
C*,6%","%, / %/9*>"%#*- 6.""(0 )* !#.2.""*7 /91*#, /*,6%","%& %+% )1% )*-*?% *9*;1.B.,-*5* 6%.+*5. 6+& %"9,1.#9%'"*5* ''*6. ).1.-,91*' /*,6%","%&.
SQLBrowseConnect
<*/+,6*'.9,+:"* 2.)1.3%'.,9 .91%;!9( /*,6%","%& % !/9.".'+%'.,9 6*)!/9%-(, 2".>,"%&
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
55
.91%;!9.. <*/+, /),$%8%#.$%% 2".>,"%& 6+& #.B6*5* 91,;!,-*5* .91%;!9. /*,6%","%& 8!"#$%& '()*+"&,9 /*,6%","%, / %/9*>"%#*- 6.""(0. 4-5!0+"%+ %"6-71*$%% - .7*89+7*3 % -: %)/-0"%#*3 .*""23 SQLDataSources
A*2'1.?.,9 /)%/*# 6*/9!)"(0 %/9*>"%#*' 6.""(0.
SQLDrivers
A*2'1.?.,9 /)%/*# !/9."*'+,""(0 61.7',1*' % %0 .91%;!9(
SQLGetInfo
A*2'.?',9 %"8*1-.$%= *; !#.2.""(0 61.7',1, % %/9*>"%#, 6.""(0.
SQLGetFunctions
A*2'1'?.,9 8!"#$%%, #*9*1(, )*66,1B%'.=9/& %/)*+:2!,-(- 61.7',1*-.
SQLGetTypeInfo
A*2'1.?.,9 %"8*1-.$%= * )*66,1B%'.,-(0 9%).0 6.""(0.
;<1+"+"%+ */7%:!/-9 .7*89+7-9 % (-5!0+"%+ %"6-71*$%% -: */7%:!/*3 .7*89+7-9 SQLSetConnectAttr
D/9.".'+%'.,9 .91%;!9( /*,6%","%&.
SQLGetConnectAttr
A*2'1.?.,9 2".>,"%, .91%;!9. /*,6%","%&.
SQLSetEnvAttr
D/9.".'+%'.,9 .91%;!9( /1,6(.
SQLGetEnvAttr
A*2'1.?.,9 2".>,"%, .91%;!9. /1,6(.
SQLSetStmtAttr
D/9.".'+%'.,9 .91%;!9( *),1.9*1..
SQLGetStmtAttr
A*2'1.?.,9 2".>,"%, .91%;!9. *),1.9*1..
;<1+"+"%+ (-5+8 .+)#7%(/-7* % (-5!0+"%+ %"6-71*$%% - (-5&3 .+)#7%(/-7* SQLGetDescField
A*2'1.?.,9 2".>,"%, 6,/#1%)9*1. *6"*5* )*+&.
SQLGetDescRec
A*2'1.?.,9 2".>,"%& 6,/#1%)9*1. 6+& ",/#*+:#%0
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
56
)*+,7. SQLSetDescField
D/9.".'+%'.,9 2".>,"%, 6,/#1%)9*1. 6+& *6"*5* )*+&.
SQLSetDescRec
D/9.".'+%'.,9 2".>,"%, 6,/#1%)9*1. 6+& ",/#*+:#%0 )*+,7.
4-.=-/-9#* SQL-<*(7-)-9 SQLPrepare
E*-)%+%1!,9 SQL-*),1.9*1 6+& )*/+,6!=?,5* '()*+","%&.
SQLBindParameter
C'&2('.,9 ;!8,1 / ).1.-,91.-%, %/)*+:2!,-(-% ' SQL-*),1.9*1,.
SQLGetCursorName
A*2'1.?.,9 %-& #!1/*1., #*9*1*, .//*$%%1*'."* / 6,/#1%)9*1*- *),1.9*1..
SQLSetCursorName
F)1,6,+&,9 %-& #!1/*1..
SQLSetScrollOptions
D/9.".'+%'.,9 *)$%%, #*9*1(, !)1.'+&=9 )*',6,"%,- #!1/*1.. A ',1/%&0 ODBC 2.x % 3.x G9. 8!"#$%& 2.-,",". 8!"#$%,7 SQLSetStmtAttr.
>2(-5"+"%+ <*(7-)-9 SQLExecute
A()*+"&,9 *9#*-)%+%1*'.""(7 SQL-*),1.9*1.
SQLExecDirect
A()*+"&,9 SQL-*),1.9*1.
SQLNativeSql
A*2'1.?.,9 9,#/9 SQL-*),1.9*1., )1,*;1.2*'.""*5* #*"#1,9"(- 61.7',1*-, "* ", '()*+"&,9 ,5*.
SQLDescribeParam
A*2'1.?.,9 *)%/."%, ).1.-,91*', %/)*+:2!,-(0 ' *9#*-)%+%1*'.""*- SQL-*),1.9*1,.
SQLNumParams
A*2'1.?.,9 >%/+* ).1.-,91*' ' *9#*-)%+%1*'.""*- SQL-*),1.9*1,.
SQLParamData
H/)*+:2!,9/& /*'-,/9"* / 8!"#$%,7 SQLPutData 6+& ),1,6.>% '* '1,-& '()*+","%& 2".>,"%7
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
57
).1.-,91*'. SQLPutData
<,1,6.,9 >./9: %+% '/, 2".>,"%& ).1.-,91*'.
;<95+0+"%+ 7+
A*2'1.?.,9 >%/+* /91*#, ". #*9*1(, '*26,7/9'*'.+ SQL-*),1.9*1 insert, update %+% delete.
SQLNumResultCols
A*2'1.?.,9 >%/+* /9*+;$*' ' 1,2!+:9%1!=?,".;*1,.
SQLDescribeCol
F)%/('.,9 /9*+;,$ 1,2!+:9%1!=?,5* ".;*1., '*2'1.?.& %-& )*+&, ,5* 9%), 1.2-,1 % 9.).
SQLColAttribute
A*2'1.?.,9 %"8*1-.$%= * /9*+;$, 1,2!+:9%1!=?,5* ".;*1.. A *9+%>%, *9 8!"#$%% SQLColAttribute )*2'*+&,9 )*+!>%9: ;*+,, *;3%1"!= %"8*1-.$%= * /9*+;$,, '#+=>.& %"8*1-.$%=, *)1,6,+&,-!= #*"#1,9"(61.7',1*-.
SQLBindCol
A()*+"&,9 /'&2('."%, ;!8,1. )1%+*B,"%+%,"9. /* /9*+;$.-% 1,2!+:9%1!=?,5* ".;*1..
SQLFetch
H2'+,#.,9 6.""(, *6"*7 /+,6!=?,7 /91*#% %2 1,2!+:9%1!=?,5* ".;*1., '*2'1.?.& 6.""(, 6+& '/,0 /'&2.""(0 /9*+;$*'.
SQLFetchScroll
H2'+,#.,9 6.""(, *6"*7 %+% ",/#*+:#%0 /91*# %2 1,2!+:9%1!=?,5* ".;*1., '*2'1.?.& 6.""(, 6+& '/,0 /'&2.""(0 /9*+;$*'. !"#$%& )*2'*+&,9 &'"* !#.2.9: #.#!= /91*#! /+,6!,9 %2'+,>:. 4."".& 8!"#$%& 2.-,"%+. 8!"#$%= SQLExtendedFetch %2 ODBC 2.x.
SQLGetData
H2'+,#.,9 %2 1,2!+:9%1!=?,5* ".;*1. 2".>,"%, *6"*5* /9*+;$. *6"*7 9,#!?,7 /91*#%. 4+&
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
58
%/)*+:2*'."%& G9*7 8!"#$%% ", 91,;!,9/& )1,6'.1%9,+:"*, /'&2('."%, /9*+;$*'. SQLSetPos
<*2%$%*"%1!,9 #!1/*1 ' %2'+,>,""*- ;+*#, 6.""(0 % )*2'*+&,9 )1%+*B,"%=-#+%,"9!: *;"*'+&9: 6.""(, ' /91*#,, -*6%8%$%1*'.9: %+% !6.+&9: 6.""(, ' 1,2!+:9%1!=?,- ".;*1,.
SQLBulkOperations
A()*+"&,9 ",/#*+:#* '/9.'*# %+% ",/#*+:#* )*-,>,""(0 *),1.$%7, '#+=>.&, %2-,","%,, !6.+,"%, % '(;*1#! )* !/9."*'+,""*-! -.1#,1!.
SQLMoreResults
F)1,6,+&,9, ,/9: +% ,?, /+,6!=?%7 1,2!+:9%1!=?%7 ".;*1, % )1% ,5* ".+%>%% '()*+"&,9 ),1,0*6 ". ",5*.
SQLGetDiagField
A*2'1.?.,9 2".>,"%, )*+& 2.)%/% %2 /91!#9!1( 6%.5"*/9%>,/#*7 %"8*1-.$%%, .//*$%%1*'.""*7 / #*"#1,9"(- 6,/#1%)9*1*- (/1,6(, /*,6%","%&, *),1.9*1.).
SQLGetDiagRec
A*2'1.?.,9 2".>,"%& ",/#*+:#%0 )1,6*)1,6,+,""(0 )*+,7 2.)%/% %2 /91!#9!1( 6%.5"*/9%>,/#*7 %"8*1-.$%%, .//*$%%1*'.""*7 / #*"#1,9"(- 6,/#1%)9*1*- (/1,6(, /*,6%","%&, *),1.9*1.).
!"#$%% #*/*5-=* SQLColumnPrivileges
A*2'1.?.,9 /)%/*# )*+,7 % %-,=?%,/& )1%'%+,5%% 6+& *6"*7 %+% ",/#*+:#%0 9.;+%$.
SQLColumns
A*2'1.?.,9 /)%/*# %-," )*+,7 ' !#.2.""*7 9.;+%$,.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
59
SQLForeignKeys
A*2'1.?.,9 /)%/*# )*+,7, #*9*1(, /*/9.'+&=9 '",3"%, #+=>% 9.;+%$(, ,/+% *"% /*26."(.
SQLPrimaryKeys
A*2'1.?.,9 /)%/*# )*+,7, #*9*1(, /*/9.'+&=9 ),1'%>"(7 #+=> 9.;+%$(.
SQLProcedureColumns
A*2'1.?.,9 ' '%6, 1,2!+:9%1!=?,5* ".;*1. /)%/*# '0*6"(0 % '(0*6"(0 ).1.-,91*' !#.2.""*7 )1*$,6!1(.
SQLProcedures
A*2'1.?.,9 /)%/*# 01."%-(0 )1*$,6!1 6+& )*6#+=>,""*5* %/9*>"%#. 6.""(0.
SQLSpecialColumns
<*+!>.,9 %"8*1-.$%= *; *)9%-.+:"*- ".;*1, )*+,7 !"%#.+:"* %6,"9%8%$%1!=?%0 /91*#! ' !#.2.""*7 9.;+%$, %+% %-& )*+&, #*9*1*, .'9*-.9%>,/#% *;"*'+&,9/& )1% %2-,","%% #.#*5*+%;* )*+& ' /91*#,.
SQLStatistics
A*2'1.?.,9 %"8*1-.$%= * 9.;+%$, % /)%/*# %"6,#/*', .//*$%%1*'.""(0 / ",7.
SQLTablePrivileges
A*2'1.?.,9 ' '%6, 1,2!+:9%1!=?,5* ".;*1. /)%/*# 9.;+%$ % )1%'%+,5%%, ".2".>,""(, 6+& #.B6*7 9.;+%$(.
SQLTables
A*2'1.?.,9 ' '%6, 1,2!+:9%1!=?,5* ".;*1. /)%/*# 9.;+%$, 01."%-(0 ' !#.2.""*- %/9*>"%#, 6.""(0.
')9-:-@.+"%+ -(+7*/-7* SQLFreeStmt
I.',13.,9 *;1.;*9#! *),1.9*1., !6.+&,9 1,2!+:9%1!=?%7 ".;*1 % */'*;*B6.,9 '/, 1,/!1/(, .//*$%%1*'.""(, / 6.""(6,/#1%)9*1*- *),1.9*1..
SQLCloseCursor
I.#1('.,9 #!1/*1, *9#1(9(7 / 6.""(6,/#1%)9*1*- *),1.9*1., % !6.+&,9
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
60
1,2!+:9%1!=?%7 ".;*1. SQLCancel
I.',13.,9 '()*+","%, SQL-*),1.9*1., )1,#1.?.& ./%"01*""*, '()*+","%, 8!"#$%%, '()*+","%, 8!"#$%%, 91,;!=?,7 6.""(, %+% 8!"#$%%, '()*+"&,-*7 ' 61!5*- )*9*#,. A *9+%>%, *9 ',1/%% 2.x 6."".& 8!"#$%& ", -*B,9 '()*+"%9: */'*;*B6,"%, 6,/#1%)9*1. *),1.9*1. % 91,;!,9/& &'"(7 '(2*' 8!"#$%% SQLFreeStmt.
SQLEndTran
A()*+"&,9 2.',13,"%, %+% *9#.9 91."2.#$%%.
')9-:-@.+"%+ )-+.%"+"%& SQLDisconnect
I.#1('.,9 /*,6%","%, / %/9*>"%#*- 6.""(0.
SQLFreeHandle
F/'*;*B6.,9 1,/!1/(, .//*$%%1*'.""(, / !#.2.""(- 6,/#1%)9*1*- (/1,6(, /*,6%","%&, *),1.9*1., )1%+*B,"%&).
&'(#)%*+,)<,1,6 %/)*+:2*'."%,- 8!"#$%7 ODBC API )1%+*B,"%,-#+%,"9 /*26.,9 +*('#0)&%# (0+*-&030'!&%#) %'#41*-05, *)1,6,+&=?%7 5+*;.+:"(7 #*"9,#/9 6+& 6*/9!). # %/9*>"%#.- 6.""(0. 4,/#1%)9*1 *#1!B,"%& )1,6*/9.'+&,9 6*/9!) # 1.2+%>"*7 %"8*1-.$%%, '#+=>.& 9,#!?%, !/9."*'#% '/,0 .91%;!9*' *#1!B,"%&, 6,/#1%)9*1( /*,6%","%7, /*26.""(, 6+& 6.""*5* *#1!B,"%&, 6%.5"*/9%#! !1*'"& *#1!B,"%&. 4,/#1%)9*1 *#1!B,"%& *)1,6,+&,9 ",#*9*1!= /91!#9!1!, /*6,1B.?!= 6.""!= %"8*1-.$%=. J,)*/1,6/9',""* 6,/#1%)9*1 *#1!B,"%& *;(>"* %/)*+:2!,9/& )1% '(2*', 8!"#$%7 SQLDataSources % SQLDrivers % )1% /*26."%% 6,/#1%)9*1*' /*,6%","%&. 4+& )1%+*B,"%&-#+%,"9., 1,.+%2!=?,5* / %/)*+:2*'."%,- 8!"#$%7 ODBC API 6*/9!) # %/9*>"%#! 6.""(0, 6*/9.9*>"* %-,9: *6%" 6,/#1%)9*1 *#1!B,"%&.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
61
C*2."%, 6,/#1%)9*1. *#1!B,"%, '()*+"&,9/& 8!"#$%,7 SQLAllocHandle, . */'*;*B6,"%, – 8!"#$%,7 SQLFreeHandle. !"#$%& SQLAllocHandle '',6,". ' ',1/%% ODBC 3.x '-,/9* /!?,/9'*'.'3%0 ' ',1/%% ODBC 2.0 8!"#$%7 SQLAllocConnect, SQLAllocEnv % SQLAllocStmt. 4+& 9*5* >9*;( )1%+*B,"%,, %/)*+:2!=?,, 8!"#$%= SQLAllocHandle, -*5+* 1.;*9.9: >,1,2 61.7',1( ODBC 2.x, -,",6B,1 61.7',1*' ',1/%% 3.x 2.-,"&,9 '(2*'( '(2*'( 8!"#$%7 91,9:,7 ',1/%% ". %0 .".5*5% '9*1*7 ',1/%% % ),1,6.,9 9.#*7 "*9#*11,#9%1*'.""(7" '(2*' ODBC-61.7',1!. 4+& )*6#+=>,"%& # ;.2, 6.""(0 /+,6!,9 /*26.9: +*('#0)&%# (0+*-&030'!&%#) (%*+0-*-05. 4+& *6"*5* 6,/#1%)9*1. *#1!B,"%& -*B,9 ;(9: /*26."* ",/#*+:#* 6,/#1%)9*1*' /*,6%","%&. 4+& '()*+","%& SQL-*),1.9*1. /*26.,9/& 6,/#1%)9*1 (%6,"9%8%#.9*1) *),1.9*1.. 4+& *6"*5* 6,/#1%)9*1. /*,6%","%& -*B,9 ;(9: /*26."* ",/#*+:#* 6,/#1%)9*1*' *),1.9*1.. <* /),$%8%#.$%% ODBC 6+& #.B6*5* )1%+*B,"%& 61.7',1( -*5!9 )*66,1B%'.9: ",*51."%>,""*, >%/+* 6,/#1%)9*1*' #.B6*5* 9%).. F6".#* #*"#1,9"(7 61.7',1 -*B,9 ".#+.6('.9: ",#*9*1(, *51."%>,"%& ". #*+%>,/9'* 6,/#1%)9*1*'. !"#$%&, %/)*+:2!,-.& 6+& /*26."%& 6,/#1%)9*1. *#1!B,"%&, /*,6%","%&, *),1.9*1. %+% )1%+*B,"%& %-,,9 /+,6!=?,, 8*1-.+:"*, *)%/."%,: SQLRETURN SQLAllocHandle( SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE * OutputHandlePtr);
<.1.-,91 HandleType ([Input]) !#.2('.,9 *6"*7 %2 /+,6!=?%0 #*"/9."9 9%) /*26.'.,-*5* 6,/#1%)9*1.: SQL_HANDLE_ENV SQL_HANDLE_DBC
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
62
SQL_HANDLE_STMT SQL_HANDLE_DESC <.1.-,91 InputHandle ([Input]) *)1,6,+&,9 #*"9,#/9, ' #*9*1(7 6*;.'+&,9/& /*26.'.,-(7 6,/#1%)9*1. K/+% 9%) 6,/#1%)9*1. SQL_HANDLE_ENV, 9* ).1.-,91 InputHandle !#.2('.,9/& #*"/9."9*7 SQL_NULL_HANDLE. <1% /*26."%% 6,/#1%)9*1. /1,6( ).1.-,91 InputHandle 2.6.,9 6,/#1%)9*1 *#1!B,"%&, . 6+& /*26."%& 6,/#1%)9*1. *),1.9*1. (SQL_HANDLE_STMT) % 6,/#1%)9*1. )1%+*B,"%& (SQL_HANDLE_DESC) – 6,/#1%)9*1 /*,6%","%&. H6,"9%8%#.9*1(, *)1,6,+&=?%, 9%) 6,/#1%)9*1. % /.- 6,/#1%)9*1 *)%/."( ' 2.5*+*'*>"(0 8.7+.0 sql.h % sqltypes.h. <.1.-,91 OutputHandlePtr ([Output]) –G9* !#.2.9,+: ". ;!8,1, ' #*9*1(7 )*-,?.,9/& /*26.'.,-.& 6+& 6,/#1%)9*1. /91!#9!1. 6.""(0. <*/+, /*26."%& 6,/#1%)9*1. *#1!B,"%& /+,6!,9 !/9."*'%9: .91%;!9 SQL_ATTR_ODBC_VERSION. A )1*9%'"*- /+!>., )1% )*)(9#, /*26.9: 6,/#1%)9*1 /*,6%","%& )1*%2*76,9 *3%;#..
./'01 *,2#345'"%6 # %(+,5"%#! 21""-/ J. /+,6!=?,7 /0,-, *9*;1.B,". )*/+,6*'.9,+:"*/9: 6,7/9'%7 )1%+*B,"%+%,"9. 6+& 1,.+%2.$%% 6*/9!). # %/9*>"%#! 6.""(0.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
63
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
64
C,'2%"'"%' ( %(+,5"%#,0 21""-/ 4+& ",)*/1,6/9',""*5* )*6#+=>,"%, # ;.2, 6.""(0 ODBC API )1,6*/9.'+&,9 /+,6!=?%, 91% 8!"#$%%: SQLConnect - /*,6%","%, / %/9*>"%#*- 6.""(0 )* DSN, %-,"% % ).1*+= )*+:2*'.9,+&; SQLDriverConnect - /*,6%","%, / %/9*>"%#*- 6.""(0 )* !#.2.""*7 /91*#, /*,6%","%& %+% )1% )*-*?% *9*;1.B.,-*5* 6%.+*5. 6+& %"9,1.#9%'"*5* ''*6. ).1.-,91*' /*,6%","%&; SQLBrowseConnect - /*,6%","%, / %/9*>"%#*- 6.""(0 / )1,6'.1%9,+:"()*/+,6*'.9,+:"(2.)1*/*.91%;!9*' /*,6%","%&.
7!3 (,'2%"'"%8 F15."%2.$%& )!+. /*,6%","%7 )*2'*+&,9 )1%+*B,"%= '(;%1.9: /*,6%","%& %2 )!+. ;,2 ",*;0*6%-*/9% ),1,!/9.".'+%'.9: %0 6+& #.B6*5* %/)*+:2*'."%&. <*/+, 9*5* #.# /*,6%","%, /*26."* % )*-,?,"* ' )!+ )1%+*B,"%, -*B,9 %/)*+:2*'.9: G9* /*,6%","%,, ", )*'9*1&& *)&9: )1*$,// !/9."*'#% /*,6%","%&. H/)*+:2*'."%, /*,6%","%7, )*-,?,""(0 ' )!+, -*B,9 2".>%9,+:"* !',+%>%9: )1*%2'*6%9,+:"*/9: )1%+*B,"%7, -"*5*#1.9"* !/9.".'+%'.=?%0 % 1.21('.=?%0 /*,6%","%&. <1%-,1*- 9.#%0 )1%+*B,"%7 -*5!9 /+!B%9: /,1',1"(, H"9,1",9-)1%+*B,"%& /1,6",5* 2',". ' 91,02',""*7 .10%9,#9!1,, )*/9*&""* )*'9*1"* !/9.".'+%'.=?%, % 1.21('.=?%, /*,6%","%,.
9-:,)#1 21""-/ 4+& %2'+,>,"%& 6.""(0 / %/)*+:2*'."%,- ODBC API /".>.+. /+,6!,9 '(2'.9: 8!"#$%=, '()*+"&=?!= SQL-*),1.9*1, #*9*1(7 *)1,6,+&,9 8*1-%1!,-(7 1,2!+:9%1!=?%7 ".;*1. H 9*+:#* 2.9,- -*B"* )1%/9!).9: # '(;*1#, 6.""(0. ODBC API )1,6*/9.'+&,9 6'. /)*/*;. %2'+,>,"%& 6.""(0 %2 1,2!+:9%1!=?,5* ".;*1.:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
65
/ )1,6'.1%9,+:"(- /'&2('."%,- )*+,7 1,2!+:9%1!=?,5* ".;*1. / ),1,-,""(-% */"*'"*5* &2(#. )1*51.--%1*'."%&; )1&-.& '(;*1#. #.B6*5* )*+& 1,2!+:9%1!=?,5* ".;*1. ' !#.2('.,-!= ),1,-,""!= */"*'"*5* &2(#. )1*51.--%1*'."%&. L,2!+:9%1!=?%7 ".;*1 /*26.,9/& )1% '()*+","%% SQL-*),1.9*1. SELECT. 4+& '()*+","%& 2.)1*/. ODBC API )1,6*/9.'+&,9 /+,6!=?%, 6', 8!"#$%%: SQLExecute - '()*+"&,9 *9#*-)%+%1*'.""(7 SQL-*),1.9*1; SQLExecDirect -'()*+"&,9 SQL-*),1.9*1, !#.2('.,-(7 ).1.-,91*-. !"#$%& SQLExecDirect 1,.+%2!,9 %+-%6!7%8.2 0-&*#3*2(, )1% #*9*1*)1*$,// #*-)%+&$%% SQL-*),1.9*1. % ,5* '()*+","%, */!?,/9'+&,9/& ,6%"*B6( )1% '(2*', 6.""*7 8!"#$%%. !"#$%% SQLPrepare % SQLExecute 1,.+%2!=9 9-%7%6!7%8.2 0-&*#3*2(: /".>.+. '()*+"&,9/& #*-)%+&$%& *),1.9*1. % 8*1-%1!,9/& )+." '()*+","%&, . 2.9,- '*2-*B"* -"*5*#1.9"*, '()*+","%, )*65*9*'+,""*5* *),1.9*1.. !"#$%& SQLExecDirect %-,,9 /+,6!=?,, 8*1-.+:"*, *)%/."%,: SQLRETURN SQLExecDirect( SQLHSTMT StatementHandle, SQLCHAR * StatementText, SQLINTEGER TextLength); <.1.-,91 StatementHandle ([Input]) !#.2('.,9 6,/#1%)9*1 *),1.9*1., ).1.-,91 StatementText ([Input]) *)1,6,+&,9 '()*+"&,-(7 SQL- *),1.9*1, TextLength (Input]) - 6+%". /91*#% *StatementText. <1% '()*+","%% SQL-*),1.9*1., /*6,1B.?,5* ).1.-,91(, %0 2".>,"%& )1,6'.1%9,+:"* 6*+B"( ;(9: *)1,6,+,"( '(2*'*- 8!"#$%7 SQLParamData % SQLPutData %+% SQLBindParameter. A )1*9%'"*- /+!>., )1% '()*+","%% 8!"#$%% SQLExecDirect ;!6,9 '*2'1.?,"* 2".>,"%, SQL_NO_DATA. <1% -"*5*3.5*'*- %"9,18,7/, ),1'*7 6*+B". ;(9: '()*+",". 8!"#$%& SQLPrepare, %"%$%%1!=?.& #*-)%+&$%= SQL-*),1.9*1.. !"#$%& SQLPrepare %-,,9 /+,6!=?,, 8*1-.+:"*, *)%/."%,:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
66
SQLRETURN SQLPrepare( SQLHSTMT StatementHandle, SQLCHAR * StatementText, SQLINTEGER TextLength); <.1.-,91 StatementHandle ([Input]) !#.2('.,9 6,/#1%)9*1 *),1.9*1., ).1.-,91 StatementText ([Input]) *)1,6,+&,9 9,#/9 #*-)%+%1!,-*5* SQL*),1.9*1., TextLength ([Input]) – G9* 6+%". /91*#% *StatementText. !"#$%& SQLExecute '()*+"&,9 *9#*-)%+%1*'.""(7 SQL-*),1.9*1. K/+% '()*+"&,-(- SQL-*),1.9*1*- ;(+ *),1.9*1 SELECT, 9* ' 1,2!+:9.9, '()*+","%& SQLExecute /*26.,9/& 1,2!+:9%1!=?%7 ".;*1. !"#$%& SQLExecute %-,,9 /+,6!=?,, 8*1-.+:"*, *)%/."%,: SQLRETURN SQLExecute( SQLHSTMT StatementHandle); <.1.-,91 StatementHandle ([Input]) !#.2('.,9 6,/#1%)9*1 *),1.9*1. J.)1%-,1: SQLHSTMT hstmtS, hstmtU; SQLExecDirect(hstmtS, "SELECT F2 FROM TBL1 ", SQL_NTS); SQLPrepare(hstmtU, "UPDATE TBL1 SET F2=F2*10 WHERE F1=20", SQL_NTS); SQLExecute(hstmtU); L,2!+:9%1!=?%7 ".;*1 8*1-%1!,9/& ' '(6,+&,-*7 *;+./9% ).-&9%. 4+& 9*5* >9*;( %/)*+:2*'.9: 6.""(,, 2.)%/.""(, ' 1,2!+:9%1!=?%7 ".;*1, %0 /+,6!,9 %2'+,>: %2 9*7 *;+./9% ).-&9% ' ),1,-,""(, %/)*+:2!,-*5* &2(#. )1*51.--%1*'."%&. H2'+,>,"%, 6.""(0 %2 1,2!+:9%1!=?,5* ".;*1. ' ),1,-,""(, -*B,9 ;(9: '()*+","*: '(2*'*- 8!"#$%% SQLFetch %+% SQLFetchScroll; '(2*'*- 8!"#$%% SQLGetData.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
67
!"#$%% SQLFetch %+% SQLFetchScroll '/,56. '()*+"&=9 )1*6'%B,"%, #!1/*1. ". /+,6!=?!= 2.)%/:. <1% G9*- 8!"#$%& SQLFetch, 1,.+%2!,9 -,0."%2- "*6"*".)1.'+,""*5* #!1/*1.", . 8!"#$%& SQLFetchScroll, )*2'*+&,9 ' 2.'%/%-*/9% *9 %/)*+:2!,-*5* %/9*>"%#. 6.""(0 1,.+%2*'('.9: -,0."%2- "6'!".)1.'+,""*5* #!1/*1." % -,0."%2- ")1&-*7 '(;*1#%". !"#$%% SQLFetch %+% SQLFetchScroll '()*+"&=9 *6"*'1,-,""*, %2'+,>,"%, 6.""(0 9*+:#* ' 9*- /+!>.,, ,/+% )*+& 1,2!+:9%1!=?,5* ".;*1. )1,6'.1%9,+:"* ;(+% /'&2."( / ),1,-,""(-% '(2*'*- 8!"#$%% SQLBindCol 6+& #.B6*5* /'&2('.,-*5* )*+&. !"#$%& SQLFetch %-,,9 /+,6!=?,, 8*1-.+:"*, *)%/."%,: SQLRETURN SQLFetch( SQLHSTMT StatementHandle); <.1.-,91 StatementHandle ([Input]) !#.2('.,9 6,/#1%)9*1 *),1.9*1. !"#$%& SQLGetData %-,,9 /+,6!=?,, 8*1-.+:"*, *)%/."%,: SQLRETURN SQLGetData( SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLPOINTER TargetValuePtr, SQLINTEGER BufferLength, SQLINTEGER * StrLen_or_IndPtr); <.1.-,91 StatementHandle ([Input]) !#.2('.,9 6,/#1%)9*1 *),1.9*1. <.1.-,91 ColumnNumber ([Input]) !#.2('.,9 "*-,1 /'&2('.,-*5* /9*+;$. 1,2!+:9%1!=?,5* ".;*1. (".>%".& / 1). <* !-*+>."%= /9*+;,$ "*-,1 0 &'+&,9/& /9*+;$*- -.1#,1. /91*#%, ' 9*- /+!>., ,/+% -.1#,1( 6*/9!)"(. <.1.-,91 TargetType ([Input]) *)1,6,+&,9 C-9%) 6.""(0 6+& ;!8,1.*TargetValuePtr. <.1.-,91 TargetValuePtr ([Output]) *)1,6,+&,9 ;!8,1, ' #*9*1(7 '()*+"&,9/& %2'+,>,"%, 6.""(0, . ).1.-,91 BufferLength ([Input]) *)1,6,+&,9 1.2-,1 G9*5* ;!8,1. ' ;.79.0. 4+& 6.""(0, %-,=?%0 8%#/%1*'.""!= 6+%"!, 9.#%0 #.# $,+*>%/+,""(, 2".>,"%&, 61.7',1 %5"*1%1!,9 2".>,"%, ).1.-,91. BufferLength.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ <.1.-,91 StrLen_or_IndPtr ([Output]) *)1,6,+&,9 ;!8,1, ' #*9*1*'*2'1.?.,9/& 1.2-,1 6.""(0 %+% %"6%#.9*1 C+,6!=?%7 )1%-,1 %++=/91%1!,9 )1%-,","%, -,0."%2-. )*/91*>"*7 '(;*1#% 6.""(0 ;,2 %0 )1,6'.1%9,+:"*5* /'&2('."%&. //OBDC_Connect.cpp #include "stdafx.h" #include "ODBC_connect.h" #include #ifdef _DEBUG #define new DEBUG_NEW #endif CWinApp theApp; // !"#$%&'(& )!"#*+, -.(%)/&'(# using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { _tprintf(_T(" 0(!*, ('(1(,%(2,1( MFC\n")); nRetCode = 1; } else { std::cout<<"Begin"<<std::endl; SQLHENV henv; // 3&4*.(-+). )*.5/&'(# SQLHDBC hdbc; // 3&4*.(-+). 4)&6('&'(# SQLHSTMT hstmt; // 3&4*.(-+). )-&.,+)., SQLRETURN retcode; // 7)6 $)2$.,+, /*8'(1(,%(2,1(# 6&4*.(-+)., )*.5/&'(# */
68
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
69
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { std::cout<<"SQLAllocHandle 4)26,' 54-&0')"<<std::endl; /* -.&6&%&'(& $&.4(( ODBC */ retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { /* 8'(1(,%(2,1(# 6&4*.(-+)., 4)&6('&'(# */ retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { std::cout<<"SQLAllocHandle 4)26,' 54-&0') "<<std::endl; retcode = SQLConnect(hdbc, (SQLCHAR*) "MySQLDB", SQL_NTS, (SQLCHAR*) "", SQL_NTS, (SQLCHAR*) "", SQL_NTS); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /* 8'(1(,%(2,1(# 6&4*.(-+)., )-&.,+)., */ retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); SQLCHAR sqf3[50]; SQLINTEGER sqf1,sqf2,sbf1,sbf2,sbf3; SQLCHAR selecttxt[] ="SELECT f1, f2, f3 FROM tbl1"; /* 9)26,'(& .&25%:+(.5;<&=) ',!)., */ retcode = SQLExecDirect(hstmt, selecttxt, SQL_NTS); if (retcode == SQL_SUCCESS) { while (TRUE) { /* >?!).*, 6,''?@ */ retcode = SQLFetch(hstmt);
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
70
if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) { } if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /* 82$%&A&'(& 6,''?@ +.&@ -)%&B .&25%:+(.5;<&=) ',!)., */ SQLGetData(hstmt, 1, SQL_C_ULONG, &sqf1, 0, &sbf1); SQLGetData(hstmt, 2, SQL_C_ULONG, &sqf2, 0, &sbf2); SQLGetData(hstmt, 3, SQL_C_CHAR, sqf3, 50, &sbf3); /* C,-(4: $ -)+)* $?$)6, 4+.)* .&25%:+(.5;<&=) ',!)., */ std::cout<< "1: "<<sqf1<<" 2: "<<sqf2<<" 3: "<< sqf3<<" "<<std::endl; } else { break; } } } /* 4$)!)/6&'(& 6&4*.(-+).)$ */ if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } SQLDisconnect(hdbc); } SQLFreeHandle(SQL_HANDLE_DBC, hdbc); } } SQLFreeHandle(SQL_HANDLE_ENV, henv); } return nRetCode;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ } } //OBDC_Connect.h #pragma once #include "resource.h" #ifndef _AFX_NOFORCE_LIBS //////////////////////////////////////////////////////////////// // Win32 !(!%()+&*( #pragma comment(lib, "odbc32.lib") #pragma comment(lib, "odbccp32.lib") #endif //!_AFX_NOFORCE_LIBS #ifndef __SQL #include <sql.h> // #6.) #endif #ifndef __SQLEXT #include <sqlext.h> // .,40(.&'(& #endif !"#$%&, '()*+"&,-.& )/01'./%20+3"*0 4'&5('."%0 1.""(6 %7002 4+01!,-00 8*/7.+3"*0 *)%4."%0: SQLRETURN SQLBindCol( SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLPOINTER TargetValuePtr, SQLINTEGER BufferLength, SQLLEN * StrLen_or_Ind);
71
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
72
9".:0"%& )./.702/*' 8!"#$%% SQLBindCol .".+*;%:"( 5".:0"%&7 )./.702/*' 8!"#$%% SQLGetData. <* 8!"#$%& SQLBindCol !#.5('.024& 2*+3#* *1%" /.5 1+& #.=1*;* )*+&, . 5.207 '(>*/#. 1.""(6 '()*+"&024& .'2*7.2%:04#% )/% '(5*'0 8!"#$%% SQLFetch.
!"!#$%"&'$()&$ *!+",(?./.702/*7 ".5('.024& )0/070"".&, %4)*+35!07.& ' SQL-*)0/.2*/0. ?/%70"0"%0 )./.702/*' )*5'*+&02 8*/7%/*'.23 SQL-*)0/.2*/( "0)*4/0142'0""* '* '/07& '()*+"0"%& )/%+*=0"%&. @.#, )/% %4)*+35*'."%% 7"*;*A.;*'*;* %"20/80B4., )/% #*2*/*7 )/*$044 #*7)%+&$%% % '()*+"0"%& SQL-*)0/.2*/. )/*%46*1%2 #.# )*4+01*'.20+3"(0 10B42'%&, 1*42.2*:"* *2#*7)%+%/*'.23 SQL-*)0/.2*/ 4 )./.702/.7% '7042* &'"*;* !#.5."%& 5".:0"%B )*+0B 2.>+%$( >.5( 1.""(6, . 5.207 7"*;*#/.2"* '()*+"&23 1.""(B *)0/.2*/ 4 /.5+%:"(7% 5".:0"%&7% )./.702/*'. <.)/%70/: INSERT INTO TBL1 (F_ID, F2, F3) VALUES (?, ?, ?)
?./.702/( 7*;!2 >(23 03*-%4!--.30 % )%"050%--.30. ?*5%$%*""(0 )./.702/( !#.5('.,24& 4%7'*+*7 '*)/*4%20+3"(B 5".# (?), ".5('.07(7 7./#0/*7 )./.702/*'. ?/% '()*+"0"%% *)0/.2*/. '7042* )./.702/. ' 4**2'0242'!,-!, )*5%$%, SQL-*)0/.2*/. )/%42.'+&024& 5".:0"%0 )./.702/.. C()*+"&07(B SQL-*)0/.2*/ 7*=02 4*10/=.23 "04#*+3#* )./.702/*'. ?0/01 %4)*+35*'."%07 )./.702/ 4+01!02 *)/010+%23 ('()*+"%23 4'&5('."%0 )./.702/.). D)/010+0"%0 )./.702/. 5.#+,:.024& ' !#.5."%% 2%)*' 1+& 5".:0"%& (E-2%)) % 1+& )*+& 2.>+%$( (SQL-2%)), . 2.#=0 !#.5."%0 >!80/., ' #*2*/*7 >!102 4*10/=.234& 5".:0"%0 )./.702/., %+% "*70/. )./.702/. - 1+& 0;* )*4+01!,-0;* 5.)/*4. '* '/07& '()*+"0"%&. D)/010+0"%0 )./.702/. /0.+%5!024& 8!"#$%0B SQLBindParameter. !"#$%& SQLBindParameter %7002 4+01!,-00 8*/7.+3"*0 *)%4."%0: SQLRETURN SQLBindParameter( SQLHSTMT StatementHandle,
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
73
SQLUSMALLINT ParameterNumber, SQLSMALLINT InputOutputType, SQLSMALLINT ValueType, SQLSMALLINT ParameterType, SQLUINTEGER ColumnSize, SQLSMALLINT DecimalDigits, SQLPOINTER ParameterValuePtr, SQLINTEGER BufferLength, SQLINTEGER * StrLen_or_IndPtr); ?./.702/ StatementHandle ([Input]) !#.5('.02 104#/%)2*/ *)0/.2*/., )./.702/ ParameterNumber ([Input]) 5.1.02 "*70/ )./.702/. ()* 70/0 %6 '6*=10"%& ' SQL-*)0/.2*/), ".:%".& 4 1. ?./.702/ InputOutputType ([Input]) *)/010+&02 2%) )./.702/.. ?./.702/ ValueType ([Input]) *)/010+&02 2%) 5".:0"%& )0/070""*B (E-2%)), %5 #*2*/*B >!102 %5'+0#.234& 5".:0"%0, )0/01.'.07*0 ' >.5! 1.""(6, . )./.702/ ParameterType ([Input]) !#.5('.02 2%) )./.702/. (SQL-2%) )*+& 2.>+%$( >.5( 1.""(6). ?./.702/ ColumnSize ([Input]) *)/010+&02 /.570/ 42*+>$. %+% !#.5('.02 '(/.=0"%0 4**2'0242'!,-00 7./#0/! )./.702/., . )./.702/ DecimalDigits ([Input]) *)/010+&02 #*+%:042'* 104&2%:"(6 5".#*' ' 42*+>$0 %+% !#.5('.02 '(/.=0"%0 4**2'0242'!,-00 7./#0/! )./.702/.. ?*+!:%23 /.570/ 42*+>$. % #*+%:042'* 104&2%:"(6 5".#*' ' 42*+>$0 7*="* )/% )*7*-% 8!"#$%% ODBC API SQLDescribeCol. ?./.702/ ParameterValuePtr ([Deferred Input]) &'+&024& !#.5.20+07 ". >!80/ 1+& 1.""(6, )0/01.'.07(6 ' #.:042'0 )./.702/.. F+%". G2*;* >!80/. *)/010+&024& )./.702/*7 BufferLength ([Input/Output]), . !#.5.20+3 ". >!80/ 1+& 1+%"( )./.702/. 5.1.024& )./.702/*7 StrLen_or_IndPtr ([Deferred Input]). H4+% )0/01 '()*+"0"%07 8!"#$%% SQLExecDirect, 5".:0"%& )./.702/*', %4)*+35!07(6 ' 5.)/*40 "0 )0/01."( ". 40/'0/, 2* 8!"#$%% '*5'/.-.02 #*1
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
74
*2'02. SQL_NEED_DATA. F+& )0/01.:% )./.702/*' ' )/%+*=0"%% 7*;!2 %4)*+35*'.234& 8!"#$%% SQLParamData % SQLPutData. !"#$%& SQLParamData %4)*+35!024& 4*'7042"* 4 8!"#$%0B SQLPutData 1+& )0/01.:% 5".:0"%B )./.702/*' '* '/07& '()*+"0"%&. H4+% 8!"#$%% SQLParamData '*5'/.-.02 5".:0"%0 SQL_NEED_DATA, 2* *". 2.#=0 '*5'/.-.02 % "*70/ )./.702/., 1+& #*2*/*;* 4+01!02 ''042% 5".:0"%0. ?0/01.:. 5".:0"%& )./.702/. '()*+"&024& 8!"#$%0B SQLPutData. I70"*'.""(0 )./.702/( 7*;!2 %4)*+35*'.234& ' 2*7 4+!:.0, #*;1. ' SQL*)0/.2*/0 '()*+"&024& '(5*' 6/."%7*B )/*$01!/(. I70"*'.""(0 )./.702/( %10"2%8%$%/!,24& ' 4**2'0242'%% 4 %6 %70".7%, . "0 )* )*/&1#! %6 /.4)*+*=0"%& (#.# )/% )*5%$%*""(6 )./.702/.6). I70"*'.""(0 )./.702/(, 2.#=0 #.# % )*5%$%*""(0 )./.702/( 4'&5('.,24& 4 )0/070""*B )*4/0142'*7 '(5*'. 8!"#$%% ODBC API SQLBindParameter, "* %10"2%8%$%/!,24& )*4/0142'*7 )*+& SQL_DESC_NAME IPD-104#/%)2*/. (Implementation Parameter Descriptor). I70"*'.""(0 )./.702/( 2.#=0 7*;!2 >(23 4'&5."( 4 )0/070""*B )/% '(5*'0 8!"#$%% SQLSetDescField %+% 8!"#$%% SQLSetDescRec. F.+00 )/%'010" )/%70/ %4)*+35*'."%& %70"*'.""(6 )./.702/*', %4)*+35!07(6 )/% '(5*'0 )/01'./%20+3"* 4*51.""*B 6/."%7*B )/*$01!/( t1 4 1'!7& )./.702/.7% (CREATE PROCEDURE t1 @f_id int = 10, @f2 char(30) AS ). ?0/'(B )./.702/ )/*$01!/( %7002 5".:0"%0 )* !7*+:."%, /.'"*0 10, . '2*/*B )./.702/ @f2 *>&5.20+3"* 1*+=0" >(23 !#.5." )/% '(5*'0 )/*$01!/(. ?./.702/ @f2 G2* 1%".7%:04#%B )./.702/, ".5('.07(B %70"*'.""(7 )./.702/*7. // 7)D-(%(.)$,'(& $?2)$, @.,'(D)B -.)1&65.? SQLPrepare(hstmt, "{call t1(?)}", SQL_NTS); // C,-)%'&'(& 2,-(4( 1 6%# IPD-6&4*.(-+)., SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
75
30, 0, sz_Fl2, 0, &cbValue); // E)%5A&'(& IPD-6&4*.(-+)., ( )-.&6&%&'(& -)%&B // SQL_DESC_NAMED ( SQL_DESC_UNNAMED 6%# 2,-(4( #1. SQLGetStmtAttr(hstmt, SQL_ATTR_IMP_PARAM_DESC, &hIpd, 0, 0); SQLSetDescField(hIpd, 1, SQL_DESC_NAME, "@fl2", SQL_NTS); SQLSetDescField(hIpd, 1, SQL_DESC_UNNAMED, SQL_NAMED, 0); // F4%( -&.&D&'',# sz_Fl2 !?%, *)..&*+') // ('(1(,%(2(.)$,',, +) D)/') $?-)%'#+: // $?2)$ @.,'(D)B -.)1&65.? SQLExecute(hstmt);
./"(,"?/%70"0"%0 #!/4*/*' )*5'*+&02 )/%+*=0"%, '()*+"&23 '(>*/#! *1"*B %+% "04#*+3#%6 42/*# 5. *1"! *)0/.$%, %5'+0:0"%& 1.""(6. @.#=0 #!/4*/( )*110/=%'.,2 '*57*="*423 )/%70"0"%& *)0/.2*/*' UPDATE, INSERT % DELETE # 20#!-0B )*5%$%% #!/4*/.. F+& %4)*+35*'."%& #!/4*/. 4+01!02 !42."*'%23 4*01%"0"%0 4 >.5*B 1.""(6 % !42."*'%23 "!="(0 5".:0"%& .2/%>!2.7 *)0/.2*/., #*"2/*+%/!,-%7 )*'010"%0 #!/4*/.. F+& %4)*+35*'."%& #!/4*/. 2/0>!024& '()*+"%23 4+01!,-%0 10B42'%&: 1. J42."*'%23 .2/%>!2( #!/4*/., '(5'.' 8!"#$%, SQLSetStmtAttr. K2. 8!"#$%& )*5'*+&02 !42.".'+%'.23 4+01!,-%0 .2/%>!2( : SQL_ATTR_CURSOR_TYPE % SQL_ATTR_CONCURRENCY, %+% SQL_CURSOR_SCROLLABLE % SQL_CURSOR_SENSITIVITY.. 2. D)/010+%20 /.570/ /05!+32%/!,-0;* ".>*/., '(5'.' 8!"#$%, SQLSetStmtAttr 4 .2/%>!2*7 SQL_ATTR_ROW_ARRAY_SIZE.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
76
3. F+& 2*;* :2*>( %4)*+35*'.23 )*5%$%*"%/*'.""(B SQL-*)0/.2*/ 4 8/.5*B WHERE CURRENT OF 4+01!02 *)/010+%23 %7& #!/4*/., '(5'.' 8!"#$%, SQLSetCursorName. 4. F+& 4*51."%& /05!+32%/!,-0;* ".>*/. 4+01!02 '()*+"%23 SQL*)0/.2*/ SELECT, '(5'.' 8!"#$%, SQLExecute %+% 8!"#$%%, SQLExecDirect. 5. H4+% %7& #!/4*/. "0 >(+* )/01'./%20+3"* *)/010+0"*, "* 2/0>!024& %4)*+35*'.23 '*57*="*423 )/%70"0"%& )*5%$%*"%/*'.""*;* SQL*)0/.2*/. 4 8/.5*B WHERE CURRENT OF, 2* 4+01!02 4*51.23 %7& #!/4*/., '(5'.' 8!"#$%, SQLGetCursorName. 6. F+& )*+!:0"%& %"8*/7.$%% * #*+%:042'0 42*+>$*' ' 48*/7%/*'.""*7 /05!+32%/!,-07 ".>*/0 7*="* '(5'.23 8!"#$%, SQLNumResultCols. 7. D)/010+%20 4'&5('."%0 42*+>$*' /05!+32%/!,-0;* ".>*/. 4 >!80/*7, )/01".5".:0""*7 1+& %5'+0:0"%& 5".:0"%& 42*+>$. (*1"*B 42/*#% %+% 4/.5! ".>*/. 42/*#). 8. C()*+"%20 8!"#$%, ODBC API, '()*+"&,-!, %5'+0:0"%0 42/*# %5 /05!+32%/!,-0;* ".>*/. ' 4'&5.""(B >!80/ (%+% 8!"#$%, '()*+"&,-!, %5'+0:0"%0 )**:0/01"* #.=1*;* 42*+>$. ' !#.5('.07(B )./.702/*7 >!80/, 04+% "0 >(+* '()*+"0"* )/01'./%20+3"*;* 4'&5('."%&). 9. H4+% '()*+"&07(B SQL-*)0/.2*/ 4*42*&+ %5 "04#*+3#%6 *)0/.2*/*' SELECT, 2* 48*/7%/*'.""(B /05!+32%/!,-%B ".>*/ >!102 4*42*&23 %5 "04#*+3#%6 7"*=042'. F+& )0/06*1. # 4+01!,-07! 7"*=042'! (/05!+32%/!,-07! ".>*/!) %4)*+35!024& 8!"#$%& SQLMoreResults. C 2*7 4+!:.0, 04+% 4+01!,-00 7"*=042'* 4!-042'!02, 2* G2. 8!"#$%& '()*+"&02 )0/06*1 # "07! % '*5'/.-.02 #*1 *2'02. SQL_SUCCESS. H4+% '(>/."( '40 7"*=042'. 48*/7%/*'.""*;* /05!+32%/!,-0;* ".>*/., 2* 8!"#$%& '*5'/.-.02 #*1 *2'02. SQL_NO_DATA. 10. F+& *4'*>*=10"%& 104#/%)2*/. *)0/.2*/. '(5*'%20 8!"#$%, SQLFreeStmt. ?/% G2*7 1+& 2*;*, :2*>( *1"*'/070""* '()*+"%23 *4'*>*=10"%0 4'&5.""(6 4 1.""(7 *)0/.2*/*7 >!80/*',
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
77
%4)*+35!07(6 1+& %5'+0:0"%& 5".:0"%B 42*+>$*', !42."*'%20 5".:0"%0 )./.702/. fOption /.'"(7 SQL_UNBIND. ?/%+*=0"%0, %4)*+35!,-00 8!"#$%% ODBC API !)/.'+&02 )*'010"%07 #!/4*/., !42.".'+%'.& .2/%>!2( *)0/.2*/.. L*="* %4)*+35*'.23 1'. /.5+%:"(6 4)*4*>. *)/010+0"%& 6./.#20/%42%# #!/4*/.: !"#$#%&'( !" #$%&'%(; !"#$#%&'( "')*+*,!* #$%&'%(. )%* !"#$#%#+&* '&!, -."/ ", 01210,#'/* 3.+-4&* SQLSetStmtAttr / 2+,5#+ ,'"&7.', SQL_ATTR_CURSOR_TYPE, - ' "18 &6##' '&! SQLUINTEGER & 6 9#' 2,$,0,'(/* /%#$.:;&6& 2+,5#+&*6&: SQL_CURSOR_FORWARD_ONLY – $+ +,!",0%#++18 -."/ "; SQL_CURSOR_STATIC /','&5#/-&8 -."/ ", !"#$#%*:;&8 5' &+3 "6,4&*, &20%#5#++,* 0 "#2.%('&".:;&8 +,7 " +# 7.$#' '",9,'( &26#+#+ $,++1< 0 =), !" &2 >#$> ! /%# / 2$,+&* "#2.%('&".:;#? +,7 ",; SQL_CURSOR_KEYSET_DRIVEN – -."/ ", .!",0%*#618 -%:5 6. @,- 8 -."/ " ! 20 %*#' "0&$#'(" &26#+#+ & .$,%#+ /'" - 0 =), !" &2 >#$> ! /%# / 2$,+&* "#2.%('&".:;#? +,7 ",, + +# ' 7",9,#' / 2$,+ + 01< /'" -. A %&5#/'0 /'" -, $%* - ' "1< / 2$,:'/* -%:5& .-,210,#'/* ,'"&7.' 6 SQL_ATTR_KEYSET_SIZE; SQL_CURSOR_DYNAMIC - $&+,6&5#/-&8 -."/ ", '",9,:;&8 &26#+#+ $,++1< 0 =) ! /%# / 2$,+&* "#2.%('&".:;#? +,7 ",. B, &/! %(2.#618 '&! -."/ ", +,-%,$10,#' ?",+&5#+ !"&6#+*#618 ODBC-$",80#". @,-, 6+ ? $",80#"1 +# ! $$#"9&0,:' 0 26 9+ /'( !"&6#+#+&* $&+,6&5#/- ? -."/ ", )%* !"#$#%#+&* ! 0#$#+&* -."/ ", 01210,#'/* 3.+-4&* SQLSetStmtAttr / 2+,5#+&*6& ,'"&7.' 0 SQL_ATTR_CURSOR_SCROLLABLE & SQL_ATTR_CURSOR_SENSITIVITY
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
78
C'"&7.' SQL_ATTR_CURSOR_SCROLLABLE .!",0%*#' "*%*5*6(*515 #$%&'%'5, &6##' '&! SQLUINTEGER & 6 9#' 2,$,0,'(/* /%#$.:;&6& $0.6* 2+,5#+&*6&: SQL_NONSCROLLABLE – !#"#6#;,#618 -."/ " "#,%&2.#'/* -,!" /' 8 $+ +,!",0%#++18 -."/ ". D"& 012 0# 0 !"&% 9#+&& 3.+-4&& SQLFetchScroll !,",6#'" FetchOrientation 6 9#' !"&+&6,'( ' %(- 2+,5#+ SQL_FETCH_NEXT. SQL_SCROLLABLE – !#"#6#;,#618 -."/ ", ! $$#"9&0,:;&8 $0.+,!",0%#++18 !" /6 '" "#2.%('&".:;#? +,7 ",, , ',-9# !"*6.: 017 "-. /'" -. D#"#6#;,#61# -."/ "1 $%* &20%#5#+&* $,++1< &/! %(2.:' 3.+-4&: SQLFetchScroll. E/%& !" /' 8 $+ +,!",0%#++18 -."/ ", &/! %(2.#618 3.+-4 SQLFetch, ! 20 %*#' !#"#6#;,'(/* ' %(- 0 $+ 6 !"*6 6 +,!",0%#+&& & 01! %+*'( 017 "-. ' %(- $+ 8 /'" -& 2, $&+ 012 0 3.+-4&&, ' !#"#6#;,#618 -."/ " ! 20 %*#': 01! %+*'( 2, $&+ 012 0 3.+-4&& &20%#5#+ 7 %## $+ 8 /'" -&; !#"#6#;,'(/* 0 $0.< +,!",0%#+&*< & +, %:7 # 5&/% /'" -; 01! %+*'( !"*6.: 017 "-. /'" -& &2 "#2.%('&".:;#? +,7 ", ! ## + 6#".. C'"&7.' SQL_ATTR_CURSOR_SENSITIVITY !"#$#%*#' 5.0/'0&'#%(+ /'( -."/ ", - &26#+#+&*6, 01! %+*#616 $".?&6& -."/ ",6&, &6##' '&! SQLUINTEGER & 6 9#' !"#$#%*'( "#9&61, 2,$,0,#61# /%#$.:;&6& 2+,5#+&*6&: SQL_UNSPECIFIED – +# !"#$#%#++18 -."/ ", !"& - ' " 6 &26#+#+&* 6 ?.' 71'( /$#%,+1 -,- 0&$&616&, ',- & +#0&$&616&, &%& 5,/'&5+ 0&$&616&. F' ' "#9&6 &/! %(2.#'/* ! .6 %5,+&:; SQL_INSENSITIVE – "#9&6 +#5.0/'0&'#%(+ ? -."/ ",, !"& - ' " 6 0/# -."/ "1 ! -,210,:' "#2.%('&".:;&8 +,7 " 7#2 '",9#+&* &26#+#+&8, 01! %+#++1< $%* $".?&< -."/ " 0. B#5./'0&'#%(+18 -."/ " *0%*#'/* -."/ " 6 "' %(- 5'#+". G+
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
79
/ '0#'/'0.#' /','&5#/- 6. '&!. -."/ ", / ." 0+#6 &2 %*4&& "' %(- 5'#+"; SQL_SENSITIVE – "#9&6 5.0/'0&'#%(+ ? -."/ ",, !"& - ' " 6 -."/ "1 "0&$*'" 0/# &26#+#+&*, 01! %+#++1# $".?&6& -."/ ",6&. H.+-4&* SQLSetStmtAttr ! 20 %*#' ./',+ 0&'( 2+,5#+&* ,'"&7.' 0 !#",' ", & &6##' /%#$.:;## 3 "6,%(+ # !&/,+: SQLRETURN SQLSetStmtAttr( SQLHSTMT StatementHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength); D,",6#'" StatementHandle ([Input]) .-,210,#' $#/-"&!' " !#",' ",. D,",6#'" Attribute ([Input]) !"#$#%*#' ,'"&7.', 2+,5#+ - ' " ? ./',+,0%&0,#'/*, , !,",6#'" ValuePtr ([Input]) *0%*#'/* .-,2,'#%#6 +, 2+,5#+, +,2+,5,#6 # ,'"&7.'. Attribute. D,",6#'" StringLength ([Input]) 2,0&/&' ' '&!, 2+,5#+&*. !#"#$,0,#6 ? !,",6#'" 6 ValuePtr & ' 0&$, ,'"&7.',, & 6 9#' / $#"9,'( $%&+. /'" -&, &?+ "&" 0,'(/* &%& .-,210,'(/* - +/',+',6& ',-&6&, -,- SQL_NTS, SQL_IS_POINTER.
!"#$%#'" ()*+$*' A."/ ", $%* - ' " ? !"#$#%#+ &6* -."/ ",, +,210,#'/* !5*,')(,,15 #$%&'%'5. C// 4&&" 0,'( &6* -."/ ", / ,-'&0+16 $#/-"&!' " 6 !#",' ", 6 9+ 012 0 6 3.+-4&& SQLSetCursorName. I ' 6 /%.5,# #/%& J', 3.+-4&* +# 01210,#'/* *0+16 7",2 6, ' $",80#" !"& +# 7< $&6 /'& 6 9#' ?#+#"&" 0,'( &6* -."/ ", !"& 01! %+#+&& SQL- !#",' ",.
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
80
K%#$.:;&8 !"&6#" &%%:/'"&".#' !"&6#+#+ 3.+-4&& SQLSetCursorName $%* ! %.5#+&* &6#+& -."/ ", & 01! %+#+&* 2,'#6 ! 2&4& +&" 0,++ ? SQL- !#",' ", UPDATE. #define N_LEN 24 #define PH_LEN 7 SQLHSTMT
hstmtS;
//
SQLHSTMT
hstmtU;
//
SQLRETURN SQLHDBC SQLCHAR SQLINTEGER
retcode; hdbc; szN[N_LEN], szPh[PH_LEN]; cbN, cbPh;
!"#$%&'($ (&!$)'($) // (*+, (&!$)'($) SELECT) !"#$%&'($ (&!$)'($) // (*+, (&!$)'($) UPDATE)
/* -(.*)!/ *!"#$%&'($ (&!$)'($) % (&$!*!+,!/ %/, #0$"($) */ SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmtS); SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmtU); SQLSetCursorName(hstmtS, "C1", SQL_NTS); /* 12&(+3,!/ SQL-(&!$)'($ SELECT *+, 4($/%$(5)3%, $!.0+6'%$078!9( 3):($) % "5,.25)!/ !9( "'(+:;2 " +(#)+632/% :04!$)/% *+, %.5+!
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
81
/* 12:%$)!/ "'$(#% $!.0+6'%$078!9( 3):($) *( '!= &($, &(#) 3! 3)>*!/ "'$(#0 "?3'%&(5 ?." */ do retcode = SQLFetch(hstmtS); while ((retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) && (strcmp(szN, "?3'%&(5 ?.") != 0)); /* 12&(+3,!/ &(.%;%(3%$(5)332> UPDATE *+, '!#08!> 52:$)33(> "'$(#% %/!3(5)33(9( #0$"($) */ if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { SQLExecDirect(hstmtU, "UPDATE Tbl2 SET PH=\"1373322\" WHERE CURRENT OF C1", SQL_NTS); }
,-$.#&/ %'0$*(& 1#'2 D"& &/! %(2 0,+&& !#"#6#;,#6 ? -."/ ", $%* &26#+#+&* '#-.;#8 ! 2&4&& -."/ ", & 017 "-& /'" - &/! %(2.#'/* 3.+-4&* SQLFetchScroll. F', 3.+-4&* ! 20 %*#' "#,%&2 010,'(: ' ,'&! *07,14 %'00!,8 - !#"#6#;#+ ! "#2.%('&".:;#6. +,7 ". 0 $0.< +,!",0%#+&*< & +, %:7 # 5&/% /'" -; (/&'09 ,14 %'00!,8 – !#"#6#;#+ +, !#"0.: &%& ! /%#$+:: /'" -., &%& /'" -. / .-,2,++16 + 6#" 6. H.+-4&* SQLFetchScroll 01! %+*#' 017 "-. ,(/'%( & %'# &2 /3 "6&" 0,++ ? %*.$07 !%$96*8' ,(/'%( & 0 20",;,#' $,++1# $%* 0/#< /0*2,++1< /' %74 0. B,7 "1 /'" - (rowset) 6 ?.' 71'( .-,2,+1 -,- 5#"#2 ,7/ %:'+ # &%& '+ /&'#%(+ # ! 2&4& +&" 0,+, ',- & ! /"#$/'0 6
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
82
2,-%,$ -(bookmark). I 0#"/&& ODBC 2.x $%* J'&< 4#%#8 &/! %(2 0,%,/( 3.+-4&* SQLExtendedFetch. H.+-4&* SQLFetchScroll &6##' /%#$.:;## 3 "6,%(+ # !&/,+: SQLRETURN SQLFetchScroll( SQLHSTMT StatementHandle, SQLSMALLINT FetchOrientation, SQLINTEGER FetchOffset); D,",6#'" StatementHandle ([Input]) .-,210,#' $#/-"&!' " !#",' ",. D#"#6#;#+ -."/ ", !"#$#%*#'/* '&! 6 017 "-&, .-,210,#'/* !,",6#'" 6 FetchOrientation ([Input]) & 6 9#' !"&+&6,'( /%#$.:; 2+,5#+&*: SQL_FETCH_NEXT – !#"#< $ - /%#$.:;#8 /'" -# ( '+ /&'#%(+18 /-" %%&+?); SQL_FETCH_PRIOR – !#"#< $ - !"#$1$.;#8 /'" -# ( '+ /&'#%(+18 /-" %%&+?); SQL_FETCH_FIRST – !#"#< $ - !#"0 8 /'" -# (,7/ %:'+18 /-" %%&+?); SQL_FETCH_LAST– !#"#< $ - ! /%#$+#8 /'" -# (,7/ %:'+18 /-" %%&+?); SQL_FETCH_ABSOLUTE – !#"#< $ - /'" -# / .-,2,++16 + 6#" 6 (,7/ %:'+18 /-" %%&+?); SQL_FETCH_RELATIVE– !#"#6#;#+ 0!#"#$ &%& +,2,$ +, .-,2,++ # - %&5#/'0 /'" -# ( '+ /&'#%(+18 /-" %%&+?); SQL_FETCH_BOOKMARK – !#"#< $ - /'" -# ! 2,-%,$-# (,7/ %:'+18 /-" %%&+?). A %&5#/'0 /'" -, +, - ' "1# 01! %+*#'/* !#"#6#;#+ -."/ ", & + 6#" ,7/ %:'+ 8 ! 2&4&&, .-,210,#'/* !,",6#'" 6 FetchOffset ([Input]). D,",6#'"1 FetchOrientation & FetchOffset 3.+-4&& SQLFetchScroll / 06#/'+ !"#$#%*:' +,7 " /'" -, - ' "18 7.$#' &20%#5#+ &2 "#2.%('&".:;#? +,7 ",.
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12 B, /%#$.:;#8 /<#6# ! -,2,+ 6#<,+&26 7% 5+ 8 017 "-& /'" - !"& ! 2&4& +&" 0,+&& +, /%#$.:;.:, !"#$1$.;.:, !#"0.: &%& ! /%#$+:: /'" -..
L#<,+&26 017 "-& /'" - 3.+-4 SQLFetchScroll ',-9# ! 20 %*#' "#,%&2 010,'( ! 2&4& +&" 0,+ ! ,7/ %:'+ 6. .-,2,++ 6. + 6#"., ! 2&4& +&" 0,+ / /6#;#+ +, 2,$,++ # 5&/% /'" -. &%& ! 2&4& +&" 0,+ ! /"#$/'0 6 2,-%,$-&. F'& 6#<,+&261 ' 7",9#+1 +, /%#$.:;#8 /<#6#.
83
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
H.+-4&* SQLFetchScroll 01! %+*#' ! 2&4& +&" 0,+ -."/ ", +, .-,2,++.: /'" -. "#2.%('&".:;#? +,7 ", & 0 20",;,#' +,7 " /'" -, +,5&+,* / ./',+ 0%#++ 8 ! 2&4&& -."/ ",. D /%# ' ? , -,- +,7 " /'" - &20%#5#+, $%* ! 2&4& +&" 0,+&* 0 +#6 - +-"#'+ 8 /'" -& &%& &20%#5#+&* /'" -& 6 9+ &/! %(2 0,'( 3.+-4&: SQLSetPos. D"& &/! %(2 0,+&& 7% 5+ ? -."/ ", 2, $&+ 012 0 3.+-4&& 6 9#' 0 20",;,'(/* +#/- %(- /'" -. D J' 6. / -,9$16 /' %74 6 $ %9+,
84
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
85
/0*210,'(/* +# !" /' !#"#6#++,* / '0#'/'0.:;#? '&!,, , 6,//&0. @,- 8 6,//&0 715+ +,210,#'/* /$:*%'5 ,(/'%( & %'# (rowset buffer). I 26 9+ $0, '&!, /0*210,+&*: &);.1)(,!* "' & '0/<$ (column-wise binding), !"& - ' " 6 / -,9$16 /' %74 6 /0*210,#'/* '$#%(+18 6,//&0 (/'".-'.", $,++1<, / $#"9,;,* J%#6#+'1 $+ ? '&!,); &);.1)(,!* "' & %'#* (row-wise binding), !"& - ' " 6 / -,9$ 8 /'" - 8 /0*210,#'/* '$#%(+18 6,//&0 (/'".-'.", $,++1<, '&!1 J%#6#+' 0 - ' " 8 / '0#'/'0.:' '&!,6 /' %74 0 /'" -& +,7 ", $,++1<). )%* 01! %+#+&* %:7 ? '&!, /0*210,+&* &/! %(2.#'/* 3.+-4&* ODBC API SQLBindCol. D"& J' 6 '&! /0*210,+&* !"#$#%*#'/* ,'"&7.' 6 SQL_ATTR_ROW_BIND_TYPE. I /%.5,# &/! %(2 0,+&* 7% 5+ ? -."/ ", / /0*210,+ ! /'" -# &%& ! /' %74. 3.+-4&& SQLBindCol 0 -,5#/'0# !,",6#'", 06#/' ,$"#/, !" /' 8 !#"#6#++ 8 !#"#$,#'/* ,$"#/ 6,//&0,. D"& &/! %(2 0,+&& /0*210,+&* ! /' %74. / -,9$16 /' %74 6 6 9#' 71'( /0*2,+ ' $+ ? $ '"#< 6,//&0 0: !#"018 6,//&0 – $%* &20%#-,#61< 2+,5#+&8, 0' " 8 – $%* $%&+1/ &+$&-,' ", 7.3#" 0, '"#'&8 – $%* &+$&-,' ", 7.3#" 0 (#/%& &+$&-,' "1 & $%&+, <",+*'/* ! '$#%(+ /'&). A,9$18 6,//&0 $ %9#+ / $#"9,'( 5&/% J%#6#+' 0 ",0+ # 5&/%. /'" - 0 &20%#-,#6 6 +,7 "# /'" -. K%#$.:;&8 !"&6#" &%%:/'"&".#' !"&6#+#+ /0*210,+&* ! /' %74,6 $%* +,7 ", /'" -, / /' *;#? &2 $+ ? /' %74,. #define ROW_ARRAY_SIZE 30
SQLUINTEGER SQLINTEGER SQLUSMALLINT
// @(+-5( "'$(# // 5 3):($! "'$(#
ID1Array[ROW_ARRAY_SIZE], NumRowsFetched; ID1IndArray[ROW_ARRAY_SIZE]; RowStatusArray[ROW_ARRAY_SIZE], i;
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12 SQLRETURN SQLHSTMT
rc; hstmt;
// A"')3)5+%5)!/ )'$%:0' (&!$)'($) // SQL_ATTR_ROW_BIND_TYPE *+, %"&(+6.(5)3%, "5,.25)3%, // &( "'(+:;)/ SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0); // B)./!$ 3):($) "'$(# .)*)!/ )'$%:0'(/ (&!$)'($) // SQL_ATTR_ROW_ARRAY_SIZE SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE, ROW_ARRAY_SIZE, 0); // A"')3)5+%5)!/ )'$%:0' (&!$)'($) // SQL_ATTR_ROW_STATUS_PTR *+, (&$!*!+!3%, /)""%5) // "("'(,3%> "'$(# SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_STATUS_PTR, RowStatusArray, 0); // A"')3)5+%5)!/ )'$%:0' (&!$)'($) // SQL_ATTR_ROWS_FETCHED_PTR *+, 0#).)3%, 3) // cRowsFetched SQLSetStmtAttr(hstmt, SQL_ATTR_ROWS_FETCHED_PTR, &NumRowsFetched, 0); // -5,.25)!/ /)""%5 "( "'(+:;(/ ID1 SQLBindCol(hstmt, 1, SQL_C_ULONG, ID1Array, 0, ID1IndArray); // 12&(+3,!/ SQL-(&!$)'($ SELECT *+, "(.*)3%, // $!.0+6'%$078!9( 3):($) SQLExecDirect(hstmt, "SELECT ID1 FROM TBL1", SQL_NTS);
86
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
87
// 12&(+3,!/ :+(<307 52:($#0 %. $!.0+6'%$078!9( 3):($) // 1 &!$!/!33(> NumRowsFetched 5(.5$)8)!'", <%"+( // 5 *!>"'5%'!+63("'% 52:$)332= "'$(# while ((rc = SQLFetchScroll(hstmt,SQL_FETCH_NEXT,0)) != SQL_NO_DATA) { for (i = 0; i < NumRowsFetched; i++) { // C'(:$)D)!/ '(+6#( 0"&!E3( %.5+!
D"& &/! %(2 0,+&& /0*210,+&* ! /'" -,6 !"#$#%*#'/* /'".-'.",, / $#"9,;,* ' $+ ? $ '"#< J%#6#+' 0 $%* -,9$ ? /' %74, &20%#-,#61< $,++1<. D#"018 J%#6#+' !"#$+,2+,5,#'/* $%* &20%#-,#61< $,++1<, 0' " 8 – $%* $%&+1 / &+$&-,' ", 7.3#",, '"#'&8 – $%* &+$&-,' ", 7.3#", !"& ",2$#%(+ 6 / <",+#+&& 2+,5#+&8 $%&+1 & &+$&-,' ",. L,//&0 ',-&< /'".-'." $ %9#+ / $#"9,'( - %&5#/'0 J%#6#+' 0 ",0+ # 5&/%. /'" - 0 &20%#-,#6 6 +,7 "# /'" -. K%#$.:;&8 !"&6#" &%%:/'"&".#' !"&6#+#+ /0*210,+&* ! /'" -,6 $%* +,7 ", /'" -, / /' *;#? &2 $0.< /' %74 0.
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
#define ROW_ARRAY_SIZE 30 // C&$!*!+,!/ "'$0#'0$0 AR_INFO % "(.*)!/ /)""%5 // "'$0#'0$, "(*!$D)8%> 30 G+!/!3'(5 typedef struct { SQLUINTEGER F1; // +, .3)
NumRowsFetched; RowStatusArray[ROW_ARRAY_SIZE], i; rc; hstmt;
// C&$!*!+,!/ $)./!$ "'$0#'0$2, %"&(+6.0, )'$%:0' (&!$)'($) SQL_ATTR_ROW_BIND_TYPE, % (*3(5$!/!33( 0"')3)5+%5)!/ '%& "5,.25)3%, &( "'$(#)/ SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_TYPE, sizeof(AR_INFO), 0); // I"&(+6.0, )'$%:0' (&!$)'($) SQL_ATTR_ROW_ARRAY_SIZE // 0"')3)5+%5)!/ #(+%
88
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12 // A"')3)5+%5)!/ )'$%:0' (&!$)'($) // SQL_ATTR_ROWS_FETCHED_PTR *+, 0#).)3%, 3) // NumRowsFetched SQLSetStmtAttr(hstmt, SQL_ATTR_ROWS_FETCHED_PTR, &NumRowsFetched, 0); // -5,.25)!/ &(+, "'$0#'0$2 &!$5(9( G+!/!3') /)""%5) // "( "'(+:;)/% ID1, Sal % Status SQLBindCol(hstmt, 1, SQL_C_ULONG, &TBL_Array[0].F1, 0, &TBL_Array[0].F1Ind); SQLBindCol(hstmt, 2, SQL_C_CHAR, TBL_Array[0].F2, sizeof(TBL_Array[0].F2), &TBL_Array[0].F2LenOrInd); // 12&(+3,!/ SQL-(&!$)'($ SELECT *+, 4($/%$(5)3%, // $!.0+6'%$078!9( 3):($) SQLExecDirect(hstmt, "SELECT F1, F2 FROM TBL1", SQL_NTS); // I"&(+6.0, :+(<32> #0$"($ %.5+!#)!/ 0"')3(5+!33(! <%"+( "'(# while ((rc = SQLFetchScroll(hstmt,SQL_FETCH_NEXT,0)) != SQL_NO_DATA) { // J!$!/!33), NumRowsFetched "(*!$D%' <%"+( // 5 *!>"'5%'!+63("'% %.5+!
89
-(.%(/' #( %(&"%*+*0*,,12 "%!0'3*,!4 /(. +(,,12
90
else std::cout<< TBL_Array[i].F1; if (TBL_Array[i].F2LenOrInd == SQL_NULL_DATA) std::cout<< " NULL "; else std::cout<< TBL_Array[i].F2; } } } // F)#$25)!/ #0$"($) SQLCloseCursor(hstmt);
!"# 5. $!%#&'(") *+#,!&&-.- +-/0*1#. 2&0!34!5/ ODBC. 304"(5#&/
!"#$% OLE DB
OLE DB !"#$%&'()"% $*+*, -&+*! COM-.-%"!/",$*' (Component Object Model), 0*%*!1" !"#*$%&'()2% !.(*3"-.2-0(."-%4 4-./.5.!*'&--1, #*$%4 0 !&6(.7-18 .$%*7-.0&8 #&--19. :*3-* $0&6&%;, 7%* OLE DB <%* 8"%*# #*$%4 & 0 (2+18 #&--18 7"!"6 $%&-#&!%-1" COM-.-%"!/",$1, '-" 6&'.$.8*$%. *% %. & #&--19 . 8"$%& .9 !&$ *(*3"-.). = 0&7"$%'" #&--19 8*>4% '1$%4 &%; +&61 #&--19, !*$%1" #*048"-%1, %&+(.51 Excel . (2+1" #!4>." .$%*7-.0. #&--19. = *%(.7." *% #*$%4 &, !"#*$%&'()"8*>* *$!"#$%'*8 #!&,'"!*' OBDC, OLE DB *6'*()"% !"&(.6*'1'&%; #*$%4 0 .$%*7-.0&8 #&--19, 0&0 $ !.8"-"-."8 )610& SQL (0 SQL-$"!'"!&8), %&0 . 0 (2+18 #!4>.8 !*.6'*(;-18 .$%*7-.0&8 #&--19. ?!"#$%'&, !"#*$%&'()2@." #*$%4 0 .$%*7-.04 #&--19 $ .$ *(;6*'&-."8 %"9-*(*>.. OLE DB. -&61'&2%$) OLE DB !"#$%&'!$(). A!*>!&8810(."-%1, .$ *(;642@." #() #*$%4 & OLE DB !*'&,#"!1, -&61'&2%$) "*!'+)*',-() &$../0.
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
91
= %*8 $(47&", "$(. $4@"$%'4"% %*(;0* ODBC #!&,'"! #() #*$%4 & 0 0*-0!"%-*84 .$%*7-.04 #&--19, %* #() !.8"-"-.) %"9-*(*>.. OLE DB 8*3-* .$ *(;6*'&%; OLE DB !*'&,#"!, !"#-&6-&7"--1, #() #*$%4 & 0 ODBC .$%*7-.04 #&--19. B&0 0&0 &!9.%"0%4!& OLE DB *$-*'&-& -& COM, %* 8"9&-.68 $*6#&-.) !"64(;%[email protected] -&+*!*' $*$%*.% .6 *$("#*'&%"(;-*$%", C&>*' %. &: 1. $*6#&-." *+D"0%& –> 2. 6& !*$ 40&6&%"() -& .-%"!/",$ $*6#&--*>* *+D"0%& -> 3. '16*' 8"%*#& .-%"!/",$&. E-&(*>.7-* #",$%'.)8, '1 *(-)"818 *$(" $*6#&-.) !"64(;%.!42@">* -&+*!& !. !.8"-"-.. %"9-*(*>.. ODBC – '1 *(-"-.2 $')61'&-.), ' %"9-*(*>.. OLE DB .$ *(;64"%$) 8"9&-.68 &0$"$$*!*'. 634'44"!/ * .$1'&2% 0&0.8 *+!&6*8 #&--1" 6& .$1'&2%$) ' *+(&$%; &8)%. *%!"+.%"() #&--19, 4$%&-&'(.'&) !"$-*" $**%'"%$%'." 8"3#4 *+(&$%;2 &8)%. ' +4/"!" *%!"+.%"() #&--19 . $%*(+5&8. #&--19 ' !"64(;%.!42@"8 -&+*!". F-*>#& %&0*, -&+*! $')6", -&61'&2% 0&!%*, $%*(+5*' (column map).
&'(#)*+,-
!"#$% OLE DB
? "5./.0&5.) OLE DB * .$1'&"% -&+*! .-%"!/",$*', !"&(.64"819 *+D"0%&8. OLE DB. G&3#1, *+D"0%-1, %. * !"#"("- 0&0 -&+*! .-%"!/",$*'. ? "5./.0&5.) OLE DB * !"#"()"% -&+*! .-%"!/",$*' +&6*'*>* 4!*'-), 0*%*!1" #*(3-1 !"&(.6*'1'&%;$) (2+18. OLE DB !*'&,#"!&8.. = +&6*'42 8*#"(; OLE DB '9*#)% $("#42@." *+D"0%1: *+D"0% DataSource (.$%*7-.0 #&--19), .$ *(;64"81, #() $*"#.-"-.) $ .$%*7-.0*8 #&--19 . $*6#&-.) *#-*>* .(. -"$0*(;0.9 $"&-$*'. H%*% *+D"0% 4 !&'()"% $*"#.-"-."8, .$ *(;64"% .-/*!8&5.2 * *(-*8*7.)9 . &4%"-%./.0&5.. *(;6*'&%"(). *+D"0% Session ($"&-$) 4 !&'()"% '6&.8*#",$%'."8 $ .$%*7-.0*8 #&--19 – '1 *(-)"% 6& !*$1 . $*6#&"% !"64(;%.!42@." -&+*!1.
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
92
?"&-$ %&03" 8*3"% '*6'!&@&%; 8"%&--1". = $"&-$" 8*3"% $*6#&'&%;$) *#-& .(. -"$0*(;0* 0*8&-#. *+D"0% Rowset (!"64(;%.!42@., -&+*!) !"#$%&'()"% $*+*, #&--1", .6'("0&"81" ' !"64(;%&%" '1 *(-"-.) 0*8&-#1 .(. $*6#&'&"81" ' $"&-$". I& $("#42@", $9"8" !.'"#"- !.8"! .$ *(;6*'&-.) .-%"!/",$*' +&6*'*>* 4!*'-) #() $*6#&-.) !"64(;%.!42@">* -&+*!&. ?*6#&-." *+D"0%&: CoCreateInstance
!"#$% DataSource !" IDBInfo !" IDBInitialize !" IDBCreateSession # pIDBCreateSession->CreateSession # !"#$% Session !" IOpenRowSet # pIOpenRowset->OpenRowset # !"#$% Rowset !" IAccessor !" IRowsetInfo !" IColumnsInfo !" IRowset
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
93
? "5./.0&5.) OLE DB * !"#"()"% *+D"0% Command, 0*%*!1, !"#-&6-&7&"%$) #() '1 *(-"-.) %"0$%*'*, 0*8&-#1. = 0&7"$%'" %&0*, %"0$%*'*, 0*8&-#1 8*3"% '1$%4 &%;: SQL-* "!&%*! %"$%, *-.8&"81, OLE DB !*'&,#"!*8. A!. '1 *(-"-.. 0*8&-#1 8*3"% $*6#&'&%;$) !"64(;%.!42@., -&+*!. I"0*%*!1" OLE DB !*'&,#"!1 *##"!3.'&2% !&+*%4 $* $9"8*, (Schema), 0*%*!&) !"#*$%&'()"% 8"%&--1" * +&6" #&--19. :"%&--1" $%&-*')%$) #*$%4 -1 0&0 *+17-1" !"64(;%.!42@." -&+*!1. = 6&>*(*'*7-*8 /&,(" oledb.h $*#"!3&%$) 4-.0&(;-1" .#"-%./.0&%*!1 '$"9 #*$%4 -19 %. *' !"64(;%[email protected] -&+*!*' $9"81 #&--19. J() *+"$ "7"-.) !&$C.!"--19 '*68*3-*$%", 4 !&'("-.) %!&-6&05.)8. *+D"0%-&) 8*#"(; OLE DB '0(27&"% *+D"0% Transaction. OLE DB !*'&,#"!1, 0&0 . '$" COM 0*8 *-"-%1, !">.$%!.!42%$) ' !""$%!" Windows. J() *.$0& .-/*!8&5.. * 6&!">.$%!.!*'&--19 .$%*7-.0&9 #&--19 .$ *(;642%$) $ "5.&(;-1" *+D"0%1, -&61'&"81" -48"!&%*!&8.. I48"!&%*! <%* *+17-1, ?K: $"!'"!, *6'*()2@., *(47.%; .-/*!8&5.2 *+ .$%*7-.0&9 #&--19 ' '.#" !"64(;%.!42@">* -&+*!&. J() $*6#&-.) %&0*>* !"64(;%.!42@">* -&+*!& ' *+D"0%-*8 %. " DataSource $ "5./.5.!*'&.-%"!/",$ IDBEnumerateSources. J() 0&3#*>* *+D"0%-*>* %. & $ "5./.0&5.) OLE DB * !"#"()"% -&+*! .-%"!/",$*', 0*%*!1, #*(3"- *+)6&%"(;-* +1%; !"&(.6*'&- #() #&--*>* *+D"0%&. B&0." .-%"!/",$1 *%8"7&2%$) 0&0 [mandatory]. F-%"!/",$1, 0*%*!1" 8*>4% *%$4%$%'*'&%; *%8"7&2%$) 0&0 [optional]. J() *+D"0%& .$%*7-.0 #&--19 $ "5./.5.!*'&- $("#42@., -&+*! .-%"!/",$*': CoType TDataSource { [mandatory] interface IDBCreateSession; [mandatory] interface IDBInitialize;
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0 [mandatory] [mandatory] [optional] [optional] [optional] [optional] [optional] [optional]
interface interface interface interface interface interface interface interface
IDBProperties; IPersist; IConnectionPointContainer; IDBAsynchStatus; IDBDataSourceAdmin; IDBInfo; IPersistFile; ISupportErrorInfo;
} J() *+D"0%& $"&-$ $ "5./.5.!*'&- $("#42@., -&+*! .-%"!/",$*': CoType TSession { [mandatory] interface IGetDataSource; [mandatory] interface IOpenRowset; // !"#$%&' // %$(!)$ #$%%*+ [mandatory] interface ISessionProperties; [optional] interface IAlterIndex; [optional] interface IAlterTable; [optional] interface IBindResource; [optional] interface ICreateRow; [optional] interface IDBCreateCommand; [optional] interface IDBSchemaRowset; [optional] interface IIndexDefinition; [optional] interface ISupportErrorInfo; [optional] interface ITableCreation; [optional] interface ITableDefinition; // ,-. // /!"#$%&. 0$(-&1* [optional] interface ITableDefinitionWithConstraints;
94
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0 [optional] [optional] [optional] [optional]
interface interface interface interface
95
ITransaction; ITransactionJoin; ITransactionLocal; ITransactionObject;
} J() *+D"0%& !"64(;%.!42@., -&+*! $ "5./.5.!*'&- $("#42@., -&+*! .-%"!/",$*': CoType TRowset { [mandatory] interface IAccessor; [mandatory] interface IColumnsInfo; [mandatory] interface IConvertType; [mandatory] interface IRowset;// 2!/-'#!3$0'-4%!' // 50'%&' 0$(-&1* [mandatory] interface IRowsetInfo; [optional] interface IChapteredRowset; [optional] interface IColumnsInfo2; [optional] interface IColumnsRowset; [optional] interface IConnectionPointContainer; [optional] interface IDBAsynchStatus; [optional] interface IGetRow; [optional] interface IRowsetChange; // ,-. // 6#$-'%&., &"7'%'%&. & #!($3-'%&. // /0)!8 3 %$(!) #$%%*+ [optional] interface IRowsetChapterMember; [optional] interface IRowsetCurrentIndex; [optional] interface IRowsetFind; [optional] interface IRowsetIdentity; [optional] interface IRowsetIndex;
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
96
[optional] interface IRowsetLocate; // 2).7!' // 9!"&"&!%&)!3$%&' %$ "$9&/4 %$(!)$ #$%%*+ [optional] interface IRowsetRefresh; // ,-. // !(%!3-'%&. #$%%*+ 3 /!"#$%%!7 %$(!)' #$%%*+ [optional] interface IRowsetScroll; // 2!##'):8$ // /8)!--&%;$ 9! %$(!)6 #$%%*+ [optional] interface IRowsetUpdate; [optional] interface IRowsetView; [optional] interface ISupportErrorInfo; [optional] interface IRowsetBookmark; } =$" *+D"0%1 *+D"0%-*>* %. & Rowset #*(3-1 !"&(.6*'1'&%; $("#42@." .-%"!/",$1: .-%"!/",$ IRowset, .$ *(;64"81, #() .6'("7"-.) $%!*0; .-%"!/",$ IAccessor, .$ *(;64"81, #() * !"#"("-.) $')61'&-.); .-%"!/",$ IColumnsInfo, !"#*$%&'()2@., .-/*!8&5.2 * $%*(+5&9 !"64(;%.!42@">* -&+*!&; .-%"!/",$ IRowsetInfo, !"#*$%&'()2@., .-/*!8&5.2 * $&8*8 !"64(;%.!42@"8 -&+*!"; .-%"!/",$ IConvertType, !"#*$%&'()2@., .-/*!8&5.2 * !"*+!&6*'&-.. %. *' #&--19, *##"!3.'&"819 ' !"64(;%.!42@"8 -&+*!". A!. !"&(.6&5.. #*$%4 & 0 LJ *$!"#$%'*8 OLE DB !*'&,#"!& $-&7&(& $("#4"% $*6#&%; *+D"0% #&--19 . 4$%&-*'.%; $*"#.-"-." $ +&6*, #&--19. J&("" -"*+9*#.8* $*6#&%; *+D"0% $"&-$. F %*(;0* *%*8 8*3-* $*6#&'&%; !"64(;%.!42@., -&+*!. :"9&-.68 $*6#&-.) *+D"0%& $"&-$ !.'"#"- -& $("#42@", $9"8". CoCreateInstanse ! DSO
IDBCreateSession ! Session
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
97
M"64(;%.!42@., -&+*! 8*3"% +1%; $*6#&- *#-.8 .6 $("#[email protected] $ *$*+*': J() *+D"0%& $"&-$ '161'&"%$) 8"%*# IOpenRowset::OpenRowset, '1 *(-)2@., -" *$!"#$%'"--*" $*6#&-." !"64(;%.!42@">* -&+*!& (.-%"!/",$ IOpenRowset #*(3"*##"!3.'&%;$) (2+18 !*'&,#"!*8); J() *+D"0%& $"&-$ '161'&"%$) 8"%*# IDBCreateCommand::CreateCommand, $*6#&2@., *+D"0% Command. J&("" #() *+D"0%& 0*8&-#& '161'&"%$) 8"%*# ICommand::Execute. ( !. .$ *(;6*'&-.. .-%"!/",$& IMultipleResults 8*3-* !&+*%&%; $ -"$0*(;0.8. !"64(;%[email protected]. -&+*!&8.); =161'&"%$) *#..6 $("#[email protected] 8"%*#*' IColumnsRowset::GetColumnsRowset, IDBSchemaRowset::GetRowset, IViewRowset::OpenViewRowset .(. ISourcesRowset::GetSourcesRowset. N%*+1 !"64(;%.!42@., -&+*!, 9!&-.81, -& $"!'"!", 8*3-* +1(* .$ *(;6*'&%;, -"*+9*#.8* '1 *(-.%; $')61'&-." . .6'("7"-." #&--19. J() <%*>* $("#4"% * !"#"(.%; $%!40%4!1 %. & DBBINDING, * .$1'&2@." $%*(+51 . $*6#&%; &0$"$$*!. J&("" #() *(47"-.) $%!*0 !"64(;%.!42@">* -&+*!& 8*3-* .$ *(;6*'&%; !&6(.7-1" 8"%*#1, '0(27&) $("#42@.": IRowset::GetNextRows; IRowsetLocate::GetRowsByBookMarks; IRowsetLocate::GetRowAt; IRowsetScroll:: GetRowAtRatio. = 6&'"!C"-." #() 6& .$. #&--19 ' $%!40%4!4, * !"#"("--42 &0$"$$*!*8 '161'&"%$) 8"%*# IRowset::GetData. J() *$'*+*3#"-.) $%!*0 *$(" .9 *+!&+*%0. $("#4"% '16'&%; 8"%*# IRowset::ReleaseRows. A*$(" !*$8*%!& '$">* !"64(;%.!42@">* -&+*!& $("#4"% %&03" *$'*+*#.%; &0$"$$*!, '16'&' 8"%*# IRowset::ReleaseAccessor, . *$'*+*#.%; $&8 !"64(;%.!42@., -&+*!, '16'&' 8"%*# IRowset::Release. F-%"!/",$ IAccessor * !"#"()"% $("#42@." 8"%*#1:
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
98
AddRefAccessor – 4'"(.7.'&"% 7.$(* $$1(*0 -& #&--1, &0$"$$*!; CreateAccessor – $*6#&"% &0$"$$*! .6 -&+*!& $')61'&-.,; GetBindings – '*6'!&@&"% $')61'&-.), 4$%&-*'("--1" #&--18 &0$"$$*!*8; ReleaseAccessor – *$'*+*3#&"% &0$"$$*!. J() $*6#&-.) &0$"$$*!& $("#4"% 6& !*$.%; .-%"!/",$ IAccessor . '1 *(-.%; $("#42@., 0*#: HRESULT hr=pIAccessor-> CreateAccessor(); :"%*# CreateAccessor .8""% $("#42@"" /*!8&(;-*" * .$&-.": HRESULT CreateAccessor ( DBACCESSORFLAGS dwAccessorFlags, // 3!03$ // $8/'//!)$ & 8$8 !% &/9!-4"6'0/. DBCOUNTITEM cBindings, // =&/-! /3."'< // 3 $8/'//!)' const DBBINDING rgBindings[], // >9&/$%&' // /0!-(1$ &-& 9$)$7'0)$ DBLENGTH cbRowSize, // =&/-! ($<0!3, // &/9!-4"6'7*+ #-. !#%!;! %$(!)$ 9$)$7'0)!3 HACCESSOR *phAccessor, // ?8$"$0'-4 //%$ /!"#$%%*< $8/'//!) DBBINDSTATUS rgStatus[]); // @$//&3 "%$5'%&<, // !9)'#'-.AB&< /0$06/ // 8$:#!;! /3."*3$%&. G&3#1, $%*(+"5 /*!8.!4"8*>* !"64(;%.!42@">* -&+*!& .(. &!&8"%! * .$1'&"%$) $%!40%4!*, DBBINDING, 0*%*!&) .8""% $("#42@"" /*!8&(;-*" * .$&-.": typedef struct tagDBBINDING { DBORDINAL iOrdinal; // 2!).#8!3*< %!7')
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
99
// /0!-(1$ &-& 9$)$7'0)$ (%$5&%$. / 1) DBBYTEOFFSET obValue; // #3&; 3 ($<0$+ #-. // "%$5'%&. /0!-(1$ &-& 9$)$7'0)$ 3 (6C')' // (68$"$0'-4 %$ (6C') "$#$'0/. 9)& // /!"#$%&& $8/'//!)$) DBBYTEOFFSET obLength; DBBYTEOFFSET obStatus; ITypeInfo *pTypeInfo; DBOBJECT *pObject; DBBINDEXT *pBindExt; DBPART dwPart; DBMEMOWNER dwMemOwner; DBPARAMIO eParamIO; DBLENGTH cbMaxLen; DWORD dwFlags; DBTYPE wType; BYTE bPrecision; BYTE bScale; } DBBINDING; A*(" wType * !"#"()"% %. $%*(+5& .(. &!&8"%!&, 0*%*!1, * !"#"()"%$) $("#[email protected] *+!&6*8: typedef WORD DBTYPE; enum DBTYPEENUM { // -'#6AB&' "%$5'%&. 0!5%! /!!03'0/036A0 VARENUM // 9)& $30!7$0&"$1&& & %' 7!;60 (*04 &/9!-4"!3$%* // 8$8 VARIANT. DBTYPE_EMPTY = 0, // D%$5'%&' !0/60/036'0, // /!!03'0/036AB';! 0&9$ %'0 DBTYPE_NULL = 1, // D%$5'%&' )$3%! NULL,
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
DBTYPE_I2 = 2, DBTYPE_I4 = 3,
// // // // //
100
/!!03'0/036AB';! 0&9$ %'0 ,36+($<0!3!' 1'-!' /! "%$8!7, /!!03'0/036'0 0&96 short ='0*)'+($<0!3!' 1'-!' /! "%$8!7, /!!03'0/036'0 0&96 long
DBTYPE_R4 = 4, DBTYPE_R8 = 5, // E'B'/03'%%!' #3!<%!< 0!5%!/0&, // /!!03'0/036'0 0&96 Double DBTYPE_CY = 6, // F&9 #-. "%$5'%&. Cyrrency DBTYPE_DATE = 7, // F&9 #-. "%$5'%&. #$0* // (#$0$ +)$%&0/. 3 3' 3'B'/03'%%!;! 5&/-$: // 1'-!5&/-'%%$. 5$/04 !9)'#'-.'0 #$06, // $ #)!(%$. – 3)'7.) DBTYPE_BSTR = 8, // ?8$"$0'-4 %$ /0)!86 BSTR DBTYPE_IDISPATCH = 9, // ?8$"$0'-4 %$ &%0')C' // IDispatch DBTYPE_ERROR = 10, // 32-(&0!3*< 8!# !G&(8& DBTYPE_BOOL = 11, // ,-. -!;&5'/8!;! "%$5'%&. DBTYPE_VARIANT = 12, // ,-. "%$5'%&. VARIANT DBTYPE_IUNKNOWN = 13, // ?8$"$0'-4 %$ &%0')C' // IUnknown DBTYPE_DECIMAL = 14, DBTYPE_UI1 = 17, // >#%!($<0!3!' ('""%$8!3!' 1'-!', // /!!03'0/036'0 0&96 byte DBTYPE_ARRAY = 0x2000, DBTYPE_BYREF = 0x4000, DBTYPE_I1 = 16, DBTYPE_UI2 = 18,
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
101
DBTYPE_UI4 = 19, // -'#6AB&' "%$5'%&. 0!5%! /!!03'0/036A0 VARENUM // 9)& $30!7$0&"$1&&, %! %' 7!;60 (*04 &/9!-4"!3$%* // 8$8 VARIANT. DBTYPE_I8 = 20, DBTYPE_UI8 = 21, DBTYPE_GUID = 72, // ,-. 6%&8$-4%!;! // '%0&C&8$0!)$ GUID DBTYPE_VECTOR = 0x1000, DBTYPE_FILETIME = 64, DBTYPE_RESERVED = 0x8000, //
-'#6AB&' "%$5'%&. %'#!96/0&7* 3 VARENUM #-. OLE. DBTYPE_BYTES = 128, DBTYPE_STR = 129, DBTYPE_WSTR = 130, DBTYPE_NUMERIC = 131, DBTYPE_UDT = 132, DBTYPE_DBDATE = 133,// ,-. #$0*, !9)'#'-.'7!< // 8$8 /0)6806)$ // Typedef struct tagDBDATE { // SHORT year; // USHORT month; // USHORT day; // } DBDATE; DBTYPE_DBTIME = 134, DBTYPE_DBTIMESTAMP = 135 // ,-. #$0* & 3)'7'%&, // !9)'#'-.'7*+ 8$8 /0)6806)$
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0 // Typedef struct tagDBTIMESTAMP { // SHORT year; // USHORT month; // USHORT day; // USHORT hour; // USHORT minute; // USHORT second; // ULONG fraction; } DBTIMESTAMP; DBTYPE_HCHAPTER = 136 DBTYPE_PROPVARIANT = 138, DBTYPE_VARNUMERIC = 139 }; A"!"# .$ *(;6*'&-."8 *+D"0%& Command $("#4"% * !"#"(.%;, *##"!3.'&"%$) (. #&--1, *+D"0%. J( <%*>* $ *8*@;2 8"%*#& QueryInterface $("#4"% 6& !*$.%; .-%"!/",$ IDBCreateCommand *+D"0%& $"&-$. K+D"0% Command #*(3"- !"&(.6*'1'&%; $("#42@." .-%"!/",$1: ICommand IAccessor ICommandText IColumnInfo ICommandProperties. J() $*6#&-.) 0*8&-#1 '161'&"%$) 8"%*# IDBCreateCommand::CreateCommand *+D"0%& $"&-$. I& !.8"!: ICommandText pICommandText;
102
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
103
HRESULT hr= pIDBCreateCommand-> CreateCommand( NULL, // O$(. "$%; &>!">.!*'&-.", %* 40&6&%"(; // -& 4 !&'()2@., IUnknown IID_ICommandText, // P& !&C.'&"81, .-%"!/",$ (IUnknown**)& pICommandText); // Q0&6&%"(; -& // 6& !&C.'&"81, .-%"!/",$ B"0$%, '1 *(-)"81, 0*8&-#*,, 4$%&-&'(.'&"%$) !. '16*'" 8"%*#& ICommandText::SetCommandText. A!. <%*8 40&61'&"%$) 4-.0&(;-1, .#"-%./.0&%*! GUID $.-%&0$.$& 0*8&-#1. I& !.8"!: pICommandText->SetCommandText(DBGUID_SQL, "SELECT f1, f2 FROM TBL1 WHERE f3>10"); J() '1 *(-"-.) 0*8&-#1 '161'&"%$) 8"%*# ICommand::Execute (<%*% 8"%*# -&$("#4"%$) .-%"!/",$*8 ICommandText). I& !.8"!: ULONG ulRs=0; IRowset** ppRowsets=NULL; HRESULT hr= pICommandText->Execute ( NULL, // "$(. "$%; &>!">.!*'&-.", %* 40&6&%"(; // -& 4 !&'()2@., IUnknown IID_IRowset, // P& !&C.'&"81, .-%"!/",$ NULL, // Q0&6&%"(; -& $%!40%4!4 %. & // struct DBPARAMS { // void *pData; // DB_UPARAMS cParamSets; // HACCESSOR hAccessor; //};
ulRs, // G*(.7"$%'* $%!*0, -* 0*%*!1" // '*6#",$%'*'&(& 0*8&-#& INSERT, UPDATE
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
104
//.(. DELETE (IUnknown**)& ppRowsets); // Q0&6&%"(; -& // 40&6&%"(. -&+*!*' #&--19 E(>*!.%8 '1 *(-"-.) 0*8&-#1 !.'"#"- -& $("#42@", $9"8": Session " pIDBCreateCommand->CreateCommand # Command " pICommand->Execute # Rowset J* '1 *(-"-.) 0*8&-#1 8*3-* * !"#"(.%; *'"#"-." $*6#&'&"8*>* !"64(;%.!42@">* -&+*!& '16*'*8 8"%*#& ICommandProperties::SetProperties. J() 8-*>*0!&%-*>* '1 *(-"-.) 6& !*$& . !. .$ *(;6*'&-.. &!&8"%!*' $("#4"% '16'&%; 8"%*# ICommandPrepare::Prepare, & 6&%"8 * !"#"(.%; &!&8"%!1 '16*'*8 8"%*#& ICommandWithParameters::SetParameterInfo. O$(. ' !"64(;%&%" '1 *(-"-.) 0*8&-#1 '*6'!&@&"%$) -"$0*(;0* !"64(;%[email protected] -&+*!*', %* .$ *(;64"%$) 8"%*# IMultipleResults::GetResult. A!.8"-"-." OLE DB *6'*()"% *##"!3.'&%; !*$%1", '(*3"--1" . !&$ !"#"("--1" %!&-6&05... K+D"0% Session #() !&+*%1 $ %!&-6&05.)8. *##"!3.'&"% $("#42@." .-%"!/",$1: .-%"!/",$ ITransactionLocal. J() -&7&(& %!&-6&05.. '161'&"%$) 8"%*# ITransactionLocal::StartTransaction(). O$(. <%*% 8"%*#
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
105
'161'&"%$) .6 &0%.'-*, %!&-6&05.., %* *%0!1'&"%$) -*'&) '(*3"--&) %!&-6&05.); .-%"!/",$ ITransaction, *##"!3.'&2@., 8"%*#1 Abort, Commit . GetTransactionInfo; .-%"!/",$ ITransactionJoin, !"&(.642@., *##"!304 !&$ !"#"("--19 %!&-6&05.,. K+D"0% Transaction *6'*()"% !"&(.6*'1'&%; +*("" C.!*0." '*68*3-*$%. 4 !&'("-.) %!&-6&05.)8., *##"!3.'&) $("#42@." .-%"!/",$1: ITransaction, *6'*()2@., '1 *(-.%; !"!1'&-." %!&-6&05.. (8"%*#1 Abort, Commit, GetTransactionInfo); IConnectionPointContainer, *##"!3.'&2@., 4 !&'("-." %*70&8. $*"#.-"-.) #() $*"#.-)"819 *+D"0%*'.
!"# 6. $%&"!'!'&! (%!)(*+ ADO.NET ATL ATL !"#*$%&'()"% OLE DB C&+(*-1, 0&0 ?++ C&+(*-1, #() !"&(.6&5.. 0(."-%*' . $"!'"!*' OLE DB. J() !"&(.6&5.. 0(."-%& OLE DB !*'&,#"!& 8*3-* .$ *(;6*'&%; $("#42@." 0(&$$1: CDataConnection – 0(&$$, 4 !&'()2@., $*"#.-"-."8 $ .$%*7-.0*8 #&--19 . .-0& $4(.!42@%, *'"#"-." *+D"0%*' OLE DB .$%*7-.0 #&--19 (DataSource) . $"&-$ (Session); CDataSource–0(&$$, $**%'"%$%'42@., *+D"0%4 OLE DB .$%*7-.0 #&--19, !"#*$%&'()2@">* $*"#.-"-." $ .$%*7-."*8 #&--19 7"!"6 OLE DB !*'&,#"!&.J() *#-*>* $*"#.-"-.) 8*3-* $*6#&%; -"$0*(;0* *+D"0%*' $"&-$*' (CSession); CEnumerator – 0(&$$, $**%'"%$%'42@., *+D"0%4 OLE DB -48"!&%*!, !"#*$%&'()2@"84 $!"#$%'& #() *(47"-.) -&+*!*' #&--19, $*#"[email protected] .-/*!8&5.2 *+ .$%*7-.0&9 #&--19;
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
106
CEnumeratorAccessor – 0(&$$, .$ *(;64"81, 0(&$$*8 CEnumerator #() #*$%4 & 0 #&--18 .6 -&+*!& #&--19 -48"!&%*!&; CSession – 0(&$$, !"&(.642@., $"&-$ #*$%4 & 0 +&6" #&--19. CAccessor – 0(&$$ &0$"$$*!&, .$ *(;64"81, #() 6& .$", $%&%.7"$0. $')6&--19 $ .$%*7-.0*8 #&--19. H%*% 0(&$$ .$ *(;64"%$) ' %*8 $(47&", "$(. .6'"$%-& $%!40%4!& .$%*7-.0& #&--19; CAccessorBase – +&6*'1, 0(&$$ '$"9 0(&$$*' &0$"$$*!*'; CDynamicAccessor – 0(&$$ &0$"$$*!&, .$ *(;64"81, #() !"64(;%[email protected] -&+*!*' ' %*8 $(47&", "$(. $%!40%4!& .$%*7-.0& #&--19 -" .6'"$%-& ($*6#&-." &0$"$$*!& 8*3"% +1%; * !"#"("-* ' !"3.8" '1 *(-"-.)); CDynamicParameterAccessor – 0(&$$ &0$"$$*!&, .$ *(;64"8*>* ' %*8 $(47&", "$(. %. 1 0*8&-#1 -".6'"$%-1. A*(47.%; .-/*!8&5.2 * &!&8"%!&9 8*3-* *$!"#$%'*8 .-%"-!/",$& ICommandWithParameters; CDynamicStringAccessor – 0(&$$ &0$"$$*!&, *6'*()2@., !"&(.6*'&%; #*$%4 0 .$%*7-.04 #&--19, "$(. $%!40%4!& +&61 #&--19 -".6'"$%-&; CDynamicStringAccessorA – 0(&$$ &0$"$$*!&, !"#*$%&'()2@., '*68*3-*$%. &-&(*>.7-* 0(&$$4 CDynamicStringAccessor, -* $ %"8 *>!&-.7"-."8, 7%* #&--1", #*$%4 -1" .6 .$%*7-.0& #&--19, +1(. #*$%4 -1 0&0 ANSI-$%!*0. #&--19; CDynamicStringAccessorW – 0(&$$ &0$"$$*!&, !"#*$%&'()2@., '*68*3-*$%. &-&(*>.7-* 0(&$$4 CDynamicStringAccessor, -* $ %"8 *>!&-.7"-."8, 7%* #&--1", #*$%4 -1" .6 .$%*7-.0& #&--19, +1(. #*$%4 -1 0&0 UNICODE -$%!*0. #&--19; CManualAccessor – 0(&$$ &0$"$$*!&, !"#*$%&'()2@., 8"%*#1 #() $')61'&-.) 0&0 $%*(+5*', %&0 . &!&8"%!*' '* '!"8) '1 *(-"-.). CNoAccessr – .$ *(;64"%$) ' %*8 $(47&", "$(. -" -43"- 0(&$$ #() $')61'&-.) &!&8"%!*' .(. $%*(+5*' !"64(;%.!42@">* -&+*!&; CXMLAccessor – 0(&$$ &0$"$$*!&, !"#*$%&'()2@., '*68*3-*$%. &-&(*>.7-* 0(&$$4 CDynamicStringAccessor, -* $ %"8 *>!&-.7"-."8,
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
107
7%* #&--1", #*$%4 -1" .6 .$%*7-.0& #&--19 0*-'"!%.!42%$) ' XML/*!8&%; CAccessorRowset – .-0& $4(.!4"% !&+*%4 $ !"64(;%[email protected] -&+*!*8 . $**%'"%$%'[email protected] "84 &0$"$$*!*8; CArrayRowset – *6'*()"% !"&(.6*'1'&%; #*$%4 0 !"64(;%.!42@"84 -&+*!4 0&0 0 8&$$.'4; CBulkRowset – *6'*()"% '1 *(-)%; 6& *#.- '16*' /4-05.. '1+*!04 -"$0*(;0.9 $%!*0 !"64(;%.!42@">* -&+*!&; CNoRowset – 8*3"% .$ *(;6*'&%;$) ' %*8 $(47&", "$(. !"64(;%.!42@">* -&+*!& -" '*6'!&@&"%$) (#() CCommand .(. CTable); CRestrictions – .$ *(;64"%$) #() 6&-.) *>!&-.7"-., #() !"64(;%[email protected] -&+*!*' $9"81; CRowset – !.8"-)"%$) #() .6'("7"-.) #&--19 . 4 !&'("-.) !"64(;%[email protected] -&+*!*8; CStreamRowset – '*6'!&@&" *+D"0% %. & ISequentialStream, .$ *(;64"81, #&("" #() '1+*!0. #&--19 ' XML-/*!8&%" ('16*'*8 8"%*#& Read); CCommand – !.8"-)"%$) #() 6&-.) . '1 *(-"-.) 0*8&-# OLE DB; 8*3"% .$ *(;6*'&%;$) #() $*6#&-.) !"64(;%[email protected] -&+*!*'; *6'*()"% 6&'&%; &!&8"%!1; CMultipleResults – !.8"-)"%$) #() 0*8&-#, $*6#[email protected] -"$0*(;0* !"64(;%[email protected] -&+*!*'; CNoMultipleResults – !.8"-)"%$) * 48*(7&-.2 #() 0*8&-#, $*6#[email protected] %*(;0* *#.- !"64(;%.!42@., -&+*!; CTable – .$ *(;64"%$) #() #*$%4 & 0 !"64(;%.!42@"84 -&+*!4 +"6 40&6&-.) &!&8"%!*'. J() *#0(27"-.) 0 +&6" #&--19 $("#4"% $*6#&%; *+D"0% %. & CDataSource, 6&%"8 *+D"0% %. & CSession.
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
108
G(&$$1 0*8&-#1 . %&+(.51 *6'*()2% !"&(.6*'1'&%; #*$%4 0 !"64(;%[email protected] -&+*!&8. H%. 0(&$$1 -&$("#42%$) *% 0(&$$& CAccessorRowset. = $!"#" Visual Studio.NET 8&$%"! ATL OLE DB Consumer *6'*()"% * !"#"(.%; 0&0*, 0(&$$ #() $*6#&-.) !"64(;%.!42@">* -&+*!& +4#"% .$ *(;6*'&-. G(&$$ CTable $("#4"% .$ *(;6*'&%; ' %*8 $(47&", "$(. !"64(;%.!42@., -&+*! %*(;0* *#.- . -" %!"+4"%$) .$ *(;6*'&-." &!&8"%!*'. A!.8"-"-." 0(&$$& CCommand *%0!1'&"% +*("" C.!*0." '*68*3-*$%.: $*6#&-." *#-*>* .(. -"$0*(;0.9 !"64(;%[email protected] -&+*!*'; .$ *(;6*'&-." &!&8"%!.7"$0.9 6& !*$*'. K%0!1%; !"64(;%.!42@., -&+*! 8*3-* '16*'*8 /4-05.. Open, & !. -"*+9*#.8*$%. 8-*>*0!&%-*>* '1 *(-"-.) 0*8&-#1 $("#4"% .$ *(;6*'&%; /4-05.2 Prepare. G(&$$ CCommand .8""% %!. &!&8"%!& C&+(*-&: %. &0$"$$*!&, %. !"64(;%.!42@">* -&+*!& . %. !"64(;%&%&. B. !"64(;%&%& 8*3"% 40&61'&%;$) 6-&7"-."8 %. & CNoMultipleResults ( * 48*(7"-.2) .(. CMultipleResults. B"9-*(*>.) OLE DB *6'*()"% '1 *(-)%; !"#&0%.!*'&-." !"64(;%.!42@">* -&+*!&, #*+&'()), 4#&()) .(. .68"-)) 6& .$.. J() '1 *(-"-.) <%.9 #",$%'., ' 0(&$$" CRowset !"&(.6*'&- .-%"!/",$ IRowsetChange, !"#*$%&'()2@., $("#42@." 8"%*#1: SetData – #() .68"-"-.) 6& .$.; Insert – #() #*+&'("-.) 6& .$.; Delete – #() 4#&("-.) 6& .$.. J() !"#&0%.!*'&-.) 6& .$", #*(3-* +1%; $**%'"%$%'[email protected] *+!&6*8 4$%&-*'("-* 6-&7"-." $'*,$%'& DBPROP_UPDATABILITY !"64(;%.!42@">* -&+*!&. H%* 8*3-* '1 *(-.%; 0&0 ' 8&$%"!" ATL OLE DB Consumer, %&0 . ' *$("#$%'.. '16*'*8 8"%*#& AddProperty. B. *##"!3.'&"8*>* !"#&0%.!*'&-." 40&61'&"%$) $("#[email protected]. 0*-$%&-%&8.:
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
109
DBPROPVAL_UP_CHANGE – !&6!"C"-." .68"-"-.) 6& .$.; DBPROPVAL_UP_INSERT – !&6!"C"-." '$%&'0. -*'*, 6& .$.; DBPROPVAL_UP_DELETE – !&6!"C"-." 4#&("-.) 6& .$.. :"%*# CRowset::SetData 4$%&-&'(.'&"% 6-&7"-." #() *#-*>* .(. -"$0*(;0.9 $%*(+5*' %"04@", $%!*0.. :"%*# CRowset::Insert $*6#&"% -*'42 $%!*04, .$ *(;64) #&--1" .6 &0$"$$*!& . '$%&'()"% "" *$(" %"04@", $%!*0..
&'(#)*+,-
!"#$% ADO
K+D"0%-&) 8*#"(; ADO !"&(.6*'&-& -& *$-*'" OLE DB. H%& 8*#"(; ' *$-*'-*8 !"#-&6-&7"-& #() .$ *(;6*'&-.) *$%*)--*>* $*"#.-"-.) $ +&6*, #&--19. F6'("0&"81" -&+*!1 #&--19 "!"#&2%$) 0(."-%4 *$!"#$%'*8 8&!C&(.->& COM. G(&$$ Connection !"&(.64"% /.6.7"$0*" $*"#.-"-." $ '-"C-.8 .$%*7-.0*8 #&--19. I&+*! #&--19 !"&(.64"%$) *+D"0%*8 Recordset. K+D"0% Command !"#-&6-&7"- #() '1 *(-"-.) 0*8&-#, '0(27&) 0*8&-#1, $*6#&2@." !"64(;%.!42@." -&+*!1. =1 *(-)"8&) 0*8&-#& 8*3"% $*6#&'&%; +*("" *#-*>* !"64(;%.!42@">* -&+*!&. M"64(;%.!42@., -&+*! 8*3"% +1%; $*6#&- 0&0 '1 *(-"-."8 * "!&%*!& SELECT, %&0 . )'()%;$) !"64(;%&%*8 '1 *(-"-.) 9!&-.8*, !*5"#4!1. K+D"0% Command 8*3"% *%0!1%; -*'*" $*"#.-"-." .(. .$ *(;6*'&%; 43" $4@"$%'42@"". H%* * !"#"()"%$) 6-&7"-."8 ">* $'*,$%'& ActiveConnection: !. 6&-.. ' 0&7"$%'" 6-&7"-.) $'*,$%'& $%!*0. $*"#.-"-.) – '1 *(-)"%$) *%0!1%." -*'*>* $*"#.-"-.); !. 40&6&-." -& $4@"$%'42@., *+D"0% Connection – .$ *(;64"%$) $*"#.-"-.", 4$%&-*'("--*" *$!"#$%'*8 #&--*>* *+D"0%&. B&0.8 *+!&6*8, ADO .-0& $4(.!4"% 8"9&-.68 $*"#.-"-.), !.8"-)"81, !. .$ *(;6*'&-.. OLE DB, *6'*()) '1 *(-.%; 0*8&-#4 +"6 )'-*>* $*6#&-.) *+D"0%*' .$%*7-.0 #&--19 . $"&-$ (-" $*6#&'&) *+D"0%& $*"#.-"-.").
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
110
K#-* . %*3" $*"#.-"-." 8*3"% +1%; .$ *(;6*'&-* -"$0*(;0.8. *+D"0%&8. Command. J() '1 *(-"-.) 6& !*$& *$!"#$%'*8 *+D"0%& Command #*(3"- +1%; '16'&- 8"%*# Execute *+D"0%&. H%*% 8"%*# '*6'!&@&"% *+D"0% %. & Recordset: cmd.Execute(NumRecords, // G*(-'* .6'("7"--19 $%!*0 Parameters, CommandOptions); //B. 6& !*$&: // adCmdText – SQL-* "!&%*! // adCmdStoreProc – 9!&-.8&) !*5"#4!& I& !.8"!: var Con = Server.CreateObject("ADODB.Connection") var strCon = "Provider='sqloledb';Data Source=" + Request.ServerVariables("SERVER_NAME") + ";" + "Initial Catalog='pubs';Integrated Security='SSPI';"; var cmd = Server.CreateObject("ADODB.Command"); // ?*6#&-." // *+D"0%& Command var rs = Server.CreateObject("ADODB.Recordset"); Con.Open(strCon); // Q$%&-&'(.'&"8 $*"#.-"-." cmd.CommandText = "MyProc1"; // CREATE PROCEDURE MyProc1 // @i1 int // AS // SELECT f1from tbl1 WHERE tbl1.f2 = @i1 // GO cmd.CommandOptiona = adCmdStoredProc; cmd.CommandTimeOut = 15;
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
111
prm = Server.CreateObject("ADODB.Parameter"); // ?*6#&-." &!&8"%!& prm.Type = adInteger; // B. &!&8"%!& prm.Size = 3; prm.Direction = adParamInput; prm.Value = 123; cmd.Parameters.Append(prm); cmd.ActiveConnection = Con; // F$ *(;64"8*" $*"#.-"-." rs = cmd.Execute(); // A*(47"-." !"64(;%.!42@">* -&+*!& while (!rs.EOF) { rs.MoveNext; // A"!"9*# 0 $("#42@", 6& .$. } } if (rs.State == adStateOpen) rs.Close; if (Con.State == adStateOpen) Con.Close; rs = null; Con = null; ?*6#&%; !"64(;%.!42@., -&+*! 8*3-* . -" *$!"#$%'"--*, .$ *(;64) 8"%*# Open *+D"0%& Recordset. I& !.8"!: var strCon = "Provider='sqloledb';Data Source=" + Request.ServerVariables("SERVER_NAME") + ";" + "Initial Catalog='pubs';"; var rs = Server.CreateObject("ADODB.Recordset");
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
rs.open( "select f1,f2 from tbl1", strCon, adOpenForwardOnly, adLockReadOnly, adCmdText); rs.MoveFirst(); while (rs.EOF != true) { // … rs.MoveNext(); }
&'(#)*+,-
112
// ?%!*0& $*"#.-"-.) // A*'"#"-." 04!$*!&
// A"!"9*# 0 "!'*, $%!*0"
!"#$% ADO.NET
?!"#& Visual Studio .NET !"#*$%&'()"% #() #*$%4 & 0 #&--18 *+D"0%1 ADO.NET. J() #*$%4 & . 8&-. 4(.!*'&-.) #&--18. $4@"$%'42% #'& 4%.: .$ *(;6*'&%; 0(&$$ DataReader !*'&,#"!& #&--19, *6'*()2@., *#-*-& !&'("--1, !*$8*%! #&--19, !"3.8 "%*(;0* 7%"-.""; .$ *(;6*'&%; 0(&$$ DataSet, !"&(.642@., !&+*%4 $ #&--18., 9!&-.818. ' 0"C" -& 0(."-%". .NET Framework !*'&,#"!1 #&--19 !"#*$%&'()2% $("#42@." *+D"0%1 : Connection - #() $*"#.-"-.) $ +&6*, #&--19; Command – #() '1 *(-"-.) 0*8&-#, *6'*()[email protected] .6'("0&%; . .68"-)%; #&--1", '1 *(-)%; 9!&-.81" !*5"#4!1; DataReader – #() +1$%!*, '1+*!0. #&--19; DataAdapter – #() !"&(.6&5.. 8*$%& 8"3#4 *+D"0%*8 DataSet . .$%*7-.0*8 #&--19.
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0
113
K+D"0% DataSet, ' $'*2 *7"!"#;, !"#*$%&'()"% $*+*, 0*(("05.2 .6 *#-*>* .(. -"$0*(;0.9 *+D"0%*' DataTable (& DataTable !"&(.64"%$) 0&0 0*(("05.) *+D"0%*' %. & DataRowCollection . DataColumnCollection). I& $("#42@", $9"8" *0&6&-* '6&.8*#",$%'." 8"3#4 !*'&,#"!&8. #&--19 . *+D"0%*8 -&+*!& #&--19 DataSet.
&'()*+,#'- ,*..-/
0*1* ,*..-/ K+D"0% DataReader .$ *(;64"% #() 9!&-"-.) #&--19 0
1$2!$+"*3$ !$4 !'&','../0 !),"5'.)% +$2 &$../0 K+D"0%1
A!*'&,#"! #&--19 #() OLE DB
A!*'&,#"! #&--19 #() SQL Server
.$%*7-.0& #&--19
A!*'&,#"! #&--19 #() ODBC
114 A!*'&,#"! #&--19 #() Oracle
.$%*7-.0& #&--19
Connection
OleDbConnection
SqlConnection
OdbcConnection
OracleConnection
Command
OleDbCommand
SqlCommand
OdbcCommand
OracleCommand
DataReader
OleDbDataReader
SqlDataReader
OdbcDataReader
OracleDataReader
DataAdapter OleDbDataAdapter SqlDataAdapter OdbcDataAdapter OracleDataAdapter
.!#"/+#+/#
! "#$%!' ()%%*+
!"#$%$&'( ADO.NET #! $)'!#*')$+,##, #, -)'.!#!#'! /%'*!%0#1" 2$!/'#!#'3. 4.!2*$ 5*$&$ 2$!/'#!#'! $*6)1+,!*2( *$%06$ #, +)!.( +7,'.$/!32*+'( 6%'!#*, 2 '2*$8#'6$. /,##1". 9%( *$&$ 8*$:1 + 2)!/! Visual Studio.NET ;2*,#$+'*0 2$!/'#!#'! 2 '2*$8#'6$. /,##1" 2%!/;!* '%' +1-$%#'*0 .!*$/ Open /%( $:*$ .$?#$ 2/!%,*0, '2-$%07;( .,2*!) 2$7/,#'( $:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
4 /',%$&! Add Connection 2%!/;!* ++!2*' #!$:"$/'.;= '#B$).,C'=.
115
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
116
>*$* /',%$& -$7+$%(!* -)$+!)'*0 -),+'%0#$2*0 7,/,#'( +2!" -,),.!*)$+ 2$!/'#!#'( 2 '2*$8#'6$. /,##1". @,-)'.!), -)' +1:$)! + 6,8!2*+! '2*$8#'6, /,##1" :,71 /,##1" Microsoft Access :;/!* 2B$).')$+,#, 2%!/;=A,( 2*)$6, 2$!/'#!#'(: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DB\Mydb1.mdb, , -)' +1:$)! '2*$8#'6$. /,##1" :,7; /,##1" MyDB cthdthf PK Microsoft SQL Server (:,7; /,##1" MyDB 2!)+!), PK) – 2*)$6, 2$!/'#!#'(: Data Source=PK;Initial Catalog=MyDB;Integrated Security=True. D$7/,+,!.;= 2*)$6; 2$!/'#!#'( .$?#$ /$:,+'*0 6 -,),.!*),. -)$!6*,. 4 5*$. 2%;8,! !! 7#,8!#'! .$?#$ -$2.$*)!*0 + 2!6C'' app.config -)$!6*, +
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
117
$6#! Solution Explorer. E /%( ;6,7,#'( + 6,8!2*+! -,),.!*), -)' 2$7/,#'' $:
D%!/;=A'3 -)'.!) 2$/!)?'* 6$/, +1-$%#(=A'3 2$!/'#!#'! 2 :,7$3 /,##1": using System; using System.Data; using System.Data.OleDb; namespace MyDb1 { class Program { static void Main(string[] args) { using ( OleDbConnection con = new OleDbConnection( // ./0%$ *($%', *%&+,-&-," MyDb1.Properties.Settings.Default.Mydb1ConnectionString)) try { con.Open(); // 1('$/2)&! *%&+,-&-,& } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
F)' $-)!/!%!#'' 2*)$6' 2$!/'#!#'( .$?#$ #! (+#$ ;6,71+,*0 '2*$8#'6 /,##1", , '2-$%07$+,*0 UDL-B,3% 2+(7' 2 /,##1.'. >*$* B,3% ;6,71+,!*2( 2+$32*+$. FileName:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
118
G2-$%07;( UDL-B,3% .$?#$ 7,/,*0 -$2*,+A'6, /,##1" ' $-)!/!%'*0 -$/6%=8!#'! 6 '2*$8#'6; /,##1".
,*-#.%/%!/ SQL-#-/0)"#0#1 9%( +1-$%#!#'( SQL-$-!),*$)$+ -)!/#,7#,8!#1 $:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ Cancel
F)!)1+,!* +1-$%#!#'! 6$.,#/1.
Clone
D$7/,!* #$+13 $:
CreateObjRef
D$7/,!* $:
CreateParameter
D$7/,!* #$+13 567!.-%() $:
Dispose
H2+$:$?/,!* '2-$%07;!.1! 6$.-$#!#*$. )!2;)21.
Equals
H-)!/!%(!* 56+,+,%!#*#$2*0$:
ExecuteNonQuery
41-$%#(!* SQL-$-!),*$) /%( 2$!/'#!#'( (Connection) ' +$7+),?,!* 6$%'8!2*+$ 2*)$6, #, 6$*$)1! +$7/!32*+$+,% SQL$-!),*$).
ExecuteReader
F$21%,!* '2*$8#'6; /,##1" *!62* 6$.,#/1 ( 2+$32*+$ CommandText) ' &!#!)');!* $:
ExecuteScalar
41-$%#(!* 7,-)$2 ' +$7+),A,!* + +'/! #,:$), /,##1" -!)+13 2*$%:!C -!)+$3 2*)$6'.
GetHashCode
F)'.!#(!*2( 6,6 "!I-B!#6C'( /%( #!6$*$)1" *'-$+.
119
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ GetLifetimeService
4$7+),A,!* $:
GetType
4$7+),A,!* *'- /,##$&$ $:
120
InitializeLifetimeService @,2*),'+,!* $:
41-$%#(!* 6$.-'%(C'= 6$.,#/1.
ReferenceEquals
F)$+!)(!* 56+'+,%!#*#$2*0$:
ResetCommandTimeout
J2*,#,+%'+,!* 7#,8!#'! -$ ;.$%8,#'= /%( 2+$32*+, CommandTimeout.
ToString
4$7+),A,!* '.( 6$.-$#!#*,.
234/&" OleDbDataReader 9%( $/#$#,-),+%!##$&$ -)$2.$*), /,##1" '7 2!)+!)#$&$ 6;)2$), 2%!/;!* -)'.!#(*0 $:
$:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
121
2;A!2*+$+,#'( 2!)+!)#$&$ 6;)2$),, 2%!/;!* +171+,*0 *,6?! *$%06$ -$2%! !&$ $2+$:$?/!#'(. @,-)'.!): String cS="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DB\Mydb1.mdb"; String queryS="select f1,f2 from Tbl1"; using (OleDbConnection con = new OleDbConnection(cS)) { OleDbCommand cmd = new OleDbCommand(queryS, con); con.Open(); // 3%4+)&! *&$2&$-/5 '6$*%$: OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { // 7)#,*8 2 #%(%' 2 #%(%' 2/2%+) // 4-)9&-," +26: #&$2/: *(%;0<%2 // -)0%$) +)--/: Console.Write(reader[0].ToString()); Console.Write(" "); Console.WriteLine(reader[1].ToString()); } reader.Close(); } H:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
122
234/&" DataSet F)' '2-$%07$+,#'' $:
H:%,2*0 $:#$+%!##1" /,##1"
K!*$/ AcceptChanges
H:%,2*0 *!6;A'" /,##1"
K!*$/ Update
L9
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
123
@,:$) /,##1" .$?!* :1*0 2B$).')$+,# ),7%'8#1.' 2-$2$:,.': ! +1:$)6$3 '7 :,71 /,##1"; ! 8*!#'!. '7 XML-B,3%,; ! -)$&),..#$! B$).')$+,#'! #$+$&$ #,:$), /,##1".
9%( +7,'.$/!32*+'( .!?/; #,:$)$. /,##1" DataSet ' :,7$3 /,##1" '2-$%07;!*2( 6%,22 !+!)&*#! +!--./. 9%( *$&$ 8*$:1 '7+%!80 /,##1! '7 :,71 /,##1" ' $*$:),7'*0 '" + $6#! B$).1 /,##1", 2 '2-$%07$+,#'!. $:
H:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
124
OleDbDataAdapter tbl2Adapter = new OleDbDataAdapter( "SELECT * FROM Tbl2", // =;" *%&+,-&-," tbl2Con); // ,*#%;846&! OleDbConnection DataSet tbl_1_2 = new DataSet(); // ./0%$') 2 -)0%$ +)--/: +26: ()0;,< tbl1Adapter.Fill(tbl_1_2, "Tbl1"); tbl2Adapter.Fill(tbl_1_2, "Tbl2"); // 1#$&+&;&-,& %(-%>&-,5 !&?+6 ()0;,<)!, // -)0%$) +)--/: DataRelation rel = tbl_1_2.Relations.Add( "t_1_2", tbl_1_2.Tables["Tbl1"].Columns["id1"], tbl_1_2.Tables["Tbl2"].Columns["id1"]); foreach (DataRow pRow in tbl_1_2.Tables["Tbl1"].Rows) { // 7)#,*/2)&! 2 #%(%' 2/2%+) 4-)9&-,& // #%;" id1 ,4 Tbl1 Console.WriteLine(pRow["id1"]); foreach (DataRow cRow in pRow.GetChildRows(rel)) // 7)#,*/2)&! 2 #%(%' 2/2%+) 4-)9&-,& // #%;" id2 ,4 +%9&$-&5 ()0;, tbl2 Console.WriteLine("\t" + cRow["id2"]); } M%,22 DataTable -)!/$2*,+%(!* /%( ),:$*1 2 #,:$)$. /,##1" )(/ .!*$/$+, +6%=8,( 2%!/;=A'!: AcceptChanges
N'62');!* +2! 2/!%,##1! + *,:%'C! '7.!#!#'( 2 .$.!#*, -$2%!/#!&$ +17$+, 5*$&$ .!*$/,.
BeginInit
41-$%#(!* '#'C',%'7,C'= DataTable (-)'
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ '2-$%07$+,#'' B$). '%' /);&'" 6$.-$#!#*$+). BeginLoadData
H*6%=8,!* '2-$%07$+,#'! 2%;?!:#$3 '#B$).,C'' #, +)!.( 7,&);76' /,##1"..
Clear
J/,%(!* '7 DataTable +2! /,##1!.
Clone
M$-');!* +2! 2*);6*;)1 $:
Compute
418'2%(!* +1),?!#'!, ;6,71+,!.$! -!)+1. -,),.!*)$., /%( ;2%$+'(, 7,/,+,!.$&$ +*$)1. -,),.!*)$.. @,-)'.!): DataTable table; table = dataSet.Tables["Tbl1"]; object mySum; mySum = table.Compute( "Sum(f1)", "f2 > 20");
Copy
M$-');!* '7 DataTable 2*);6*;); ' /,##1!.
CreateDataReader
4$7+),A,!* $:
Dispose
H2+$:$?/,!* )!2;)21, '2-$%07;!.1! MarshalByValueComponent.
EndInit
O,+!)I,!* '#'C',%'7,C'= $:
EndLoadData
H-)!/!%(!*, 8*$ -)$C!22 7,&);76' /,##1"
125
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ 7,+!)I!# ' +2! 2%;?!:#,( '#B$).,C'( #,:$), /,##1" .$?!* :1*0 '2-$%07;!.,. Equals
H-)!/!%(!* 56+'+,%!#*#$2*0 /+;" $:
GetChanges
4$7+),A,!* 6$-'= $:
GetDataTableSchema K!*$/+ +$7+),A,!* $:
4$7+),A,!* .,22'+ $:
GetType
4$7+),A,!* Type /%( *!6;A!&$ $:
ImportRow
M$-');!* DataRow + DataTable.
Load
O,-$%#(!* $:
LoadDataRow
@,"$/'* ' $:#$+%(!* ;6,7,##;= 2*)$6;. P2%' 2$$*+!*2*+;=A,( 2*)$6, #! #,3/!#,, *$ /$:,+%(!*2( #$+,( 2*)$6,, 2$/!)?,A,( ;6,7,##1! /,##1!.
Merge
H:
126
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
127
NewRow
D$7/,!* #$+;= 2*)$6; DataRow + #,:$)! /,##1".
ReadXml
Q'*,!* 2"!.; XML ' 7,&);?,!* XML-/,##1! + DataTable. @,-)'.!): // 3%4+)&! *($6'(6$6 ()0;, DataTable table = new DataTable("TblXml"); DataColumn column = new DataColumn( "id1", typeof(System.Int32)); column.AutoIncrement = true; // =%0)2;"&! %#,*)-,& *(%;0<) table.Columns.Add(column); column = new DataColumn( "f1", // !" *(%;0<) typeof(System.String)); table.Columns.Add(column);
// @,#
// =%0)2;"&! ($, *($%', DataRow row; for (int i = 0; i <= 2; i++) { row = table.NewRow(); row["f1"] = "string " + i; table.Rows.Add(row); } // A$,*2),2)&! ,4!&-&-,": table.AcceptChanges(); // 7)#,*/2)&! %0B&'( DataTable // 2 XML-C)5;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ table.WriteXml( "C:\\TblData.xml", XmlWriteMode.WriteSchema); // 3%4+)&! -%2/5 %0B&'( DataTable DataTable newTable = new DataTable(); // D,()&! ,4 XML-C)5;) // 2 %0B&'( DataTable newTable.ReadXml("C:\\TblData.xml");
ReadXmlSchema
Q'*,!* + DataTable 2"!.; XML.
ReferenceEquals
H-)!/!%(!* 56+'+,%!#*#$2*0 $:
RejectChanges
H*6,* '7.!#!#'3, 2/!%,##1" + *,:%'C! 2 .$.!#*, 7,&);76', '%' 2 .$.!#*, -$2%!/#!&$ +17$+, .!*$/, AcceptChanges.
Reset
J2*,#,+%'+,!* $:
Select
4$7+),A,!* .,22'+ 2*)$6, -)!/2*,+%(=A'" $:
ToString
4$7+),A,!* 2*)$6;, 2$/!)?,A;= '.( *,:%'C1. @,-)'.!): foreach(DataTable tbl in dataSet.Tables) { Console.WriteLine(tbl.ToString()); }
128
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ WriteXml
O,-'21+,!* *!6;A!! 2$/!)?'.$! $:
WriteXmlSchema
O,-'21+,!* *!6;A;= 2*);6*;); $:
129
9%( 7,-'2' + -$*$6 +1+$/, /,##1" $:
// =%0)2;&-,& *(%;0<)
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
130
// 2 %0B&'( (,#) DataTable // =%0)2;&-,& %E$)-,9&-," UniqueConstraint table.Constraints.Add(new UniqueConstraint(col)); // =%0)2;&-,& 7 *($%'. DataRow newRow; // 10B&'( *($%') ()0;, for(int i = 0;i<7; i++) { // A$, ')?+%5 %#&$)<,, 2*()2', 06+&( // #$%,*:%+,(8 *%0/(,& RowChanged newRow= table.NewRow(); newRow[0]= i; table.Rows.Add(newRow); // =%0)2;"&! -%26F *($%'6 } // A$,*2),2)&! *+&;)--/& ,4!&-&-," table.AcceptChanges(); // ./4/2)&! #&$&+ ')?+/! ,4!&-&-,&! *($%', BeginEdit // , 4)#,*/2)&! *()-+)$(-/5 #%(%' 2/2%+) // #&$2%-)9);8-%& , (&'6G&& 4-)9&-," foreach(DataRow row in table.Rows) { row.BeginEdit(); row[0]=(int) row[0]+1000; Console.Write("A&$2%-)9);8-%& 4-)9&-,&: " + row[0, DataRowVersion.Original]); // A%*;& ,4!&-&-," *($%') ,!&&( +2& 2&$*,,: // Current – (&'6G6F, , // Original – #&$2%-)9);8-6F, // +% #$,*2%&-," ,4!&-&-,5 2&$*,F:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
131
// Proposed - #$&+#%;)E)&!)" Console.Write("A$&+#%;)E)&!%& 4-)9&-,&: " + row[0,DataRowVersion.Proposed] + "\n"); }
// // //
// //
Console.WriteLine("\n"); // A$,*2),2)&! *+&;)--/& ,4!&-&-," table.AcceptChanges(); A%*;& 2/4%2) BeginEdit 6*()-)2;,2)&! +;" +26: *($%' %+,-)'%2/& 4-)9&-,", (&! *)!/! -)$6>)" %E$)-,9&-,& <&;%*(-%*(,: table.Rows[0].BeginEdit(); table.Rows[1].BeginEdit(); table.Rows[0][0]= 100; table.Rows[1][0]=100; try { ./4%2 !&(%+) EndEdit. ,-,<,,$6&( #$%2&$'6 %E$)-,9&-," UniqueConstraint table.Rows[0].EndEdit(); table.Rows[1].EndEdit(); } catch(Exception e) { Console.WriteLine("@,# %>,0', {0} occurred.", e.GetType()); }
} // 10$)0%(') *%0/(," RowChanged private void Row_Changed( object sender, System.Data.DataRowChangeEventArgs e) { DataTable table = (DataTable) sender;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
132
Console.WriteLine("RowChanged " + e.Action.ToString() + "\table" + e.Row.ItemArray[0]); // 32%5*(2% ItemArray // #$&+*()2;"&( !)**,2, // ,*#%;846&!/5 +;" +%*(6#) // ' 4-)9&-,"! *($%', } K!*$/ Delete $:
!"# 7. !"##$ %&%!&'()*& MFC, +'!,-.)/$) 0!1 0'#(.+" * %"-"/ 0"22$3 !"#$%&'( )*+,-.# / 0123 !"#$%&'"(, !)*$+),-"& . *)/)0" 1)''23, 0$45+ *2+6 !&)#"/$7)'2 8)8 7 !)08)3 )!3"+&8+5!2 1$850&'+-$+$*!)%&'"&, +)8 " 8)8 9!"#$%&'"(-1")#$4", "#" 8$'.$#6'2& 9!"#$%&'"(. :"*#"$+&8) MFC 9$/7$#(&+ !&)#"/$727)+6 1$.+59 8 *)/)0 1)''23 9$.!&1.+7$0 9!"0&'&'"( ODBC 1!);7&!$7. ".9$#6/5(; !" <+$0 1#( !&)#"/)="" 1$.+59) 8 *)/& 1)''23 0$%'$ ')9!(05, ".9$#6/$7)+6 >5'8="" ODBC API. ?#( +$4$, @+$*2 9!"#$%&'"& 0$4#$ $+$*!)%)+6 1)''2& "/ +)*#"= *)/2 1)''23, 7'$."+6 7 '"3 "/0&'&'"( " 9&!&1)7)+6 "/0&'&'"( 7 *)/5 1)''23, 1$.+)+$@'$, @+$*2 *2# 5.+)'$7#&' 1!);7&! ODBC 1#( ".9$#6/5&0$; *)/2 1)''23.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
133
A!&1.+7) MFC Application Wizard 9$/7$#(,+ 9!)8+"@&.8" *&/ 7.(8$4$ 9!$4!)00"!$7)'"( !)/!)*)+27)+6 9!"#$%&'"(, 729$#'(,-"& 9$18#,@&'"& 8 +)*#"=& *)/2 1)''23 " $+$*!)%&'"& .$1&!%"0$4$ && 9$#&;. !" <+$0 1#( !&)#"/)="" 1$.+59) 8 *)/& 1)''23 ".9$#6/5&+.( "#" 0&3)'"/0 ODBC "#" OLE DB. ?#( +$4$ @+$*2 ".9$#6/$7)+6 ODBC-1!);7&!2 9&!7$')@)#6'$ .#&15&+ .$/1)+6 0(&%3-0' +!--./ DSN (DataSourceName). B+$ 0$%'$ .1)+6 8)8 7 0$0&'+ >$!0"!$7)'"( 9!$&8+) . 9$0$-6, MFC Application Wizard , +)8 " ".9$#6/5( ODBC32 9)'" 59!)7#&'"( Windows. !" .$/1)'"" ".+$@'"8) 1)''23 $9!&1(&+.( "0( ".+$@'"8) 1)''23, ".9$#6/5&0$& 9!"#$%&'"(0", 72*"!)&+.( +!&*5&02; 1#( 9$18#,@&'"( 8 *)/& 1)''23 ODBC-1!);7&! " 58)/27)&+.( 0&.+$9$#$%&'"& .)0$; *)/2 1)''23. C9!&1&'"& "0&'" ".+$@'"8) 1)''23 729$#'(&+.( +$#68$ $1"' !)/ " /)+&0 0$%&+ *2+6 0'$4$8!)+'$ ".9$#6/$7)'$ 1#( .$/1)'"( 7.&3 9!"#$%&'";, ".9$#6/5,-"3 <+$+ %& ODBC-1!);7&! 1#( <+$; %& *)/2 1)''23. ?#( +$4$ @+$*2 .$/1)+6 9!"#$%&'"&, !&)#"/5,-&& 1$.+59 8 *)/& 1)''23 729$#'"+& .#&15,-"& 1&;.+7"(: 1. A$/1);+& '$72; 9!$&8+. D2*&!"+& 7 8)@&.+7& E)*#$') .$/1)7)&0$4$ 9!$&8+) MFC Application. 2. F7+$0)+"@&.8" >$!0"!5&02& 9!"#$%&'"(, !&)#"/5,-"& !)*$+5 . *)/$; 1)''23, 1$#%'2 9$11&!%"7)+6 )!3"+&8+5!5 1$850&'+$+$*!)%&'"&. 3. &!&;1"+& ') .+!)'"=5 Database Support " $9!&1"+& 9$11&!%85 !)*$+2 . *)/$; 1)''23. C9="( Database view without file support $*&.9&@"7)&+ >$!0"!$7)'"( 8$1), 729$#'(,-&4$ 9$18#,@&'"& 8 ".+$@'"85 1)''23, '$ *&/ 9$11&!%8" 0&3)'"/0) .&!")#"=)="".
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
134
4. D2*&!"+& 0&3)'"/0 1$.+59) (Client type). C9="( ODBC $*&.9&@"7)&+ 78#,@&'"& 7 9!$&8+ /)4$#$7$@'$4$ >);#) AFXDB.H " #"'85&023 *"*#"$+&8. !" <+$0 '"8)8"& 8#)..2 9!$"/7$1'2& $+ 8#)..$7, !&)#"/5,-"3 !)*$+5 . *)/$; 1)''23, '& .$/1),+.(. C9="( OLE DB $*&.9&@"7)&+ 78#,@&'"& 7 9!$&8+ /)4$#$7$@'23 >);#$7 ATLBASE.H, AFXOLEDB.H " ATLPLUS.H. G.#" 1#( Database Support *2#) 5.+)'$7#&') $9="( Database view without file support "#" Database view with file support, +$ 1#( 0&3)'"/0) ODBC .$/1)7)&02; 8#).. $+$*!)%&'"( *51&+ ').#&1$7)+6.( $+ CRecordView " *51&+ )..$=""!$7)' . 8#)..$0 !&/5#6+"!5,-&4$ ')*$!), ').#&15&0$4$ $+ CRecordset. ?#( 0&3)'"/0) OLE DB 8#).. $+$*!)%&'"( ').#&15&+.( $+ COleDBRecordView " )..$=""!5&+.( . 8#)..$0, ').#&15&020 $+ CTable "#" CCommand.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
135
H"9 !&/5#6+"!5,-&4$ ')*$!) 0$%'$ $9!&1"+6 $1'$; "/ .#&15,-"3 $9="; (+!&*5&+.( 9$11&!%8) ODBC 1!);7&!)): dynaset – 1"')0"@&.8"; !&/5#6+"!5,-"; ')*$!, 9$/7$#(,-"; "0&+6 1$.+59 8 "/0&'(&0$; "'>$!0)="" !&/5#6+)+) /)9!$.); snapshot – !&/5#6+"!5,-"; ')*$! (.'"0$8), '& $+$*!)%),-"; "/0&'&'";, .1)''23 9$.#& 729$#'&'"( 1)''$4$ /)9!$.). ?)#&& .#&15&+ "#" 72*!)+6 5%& .5-&.+75,-"; ".+$@'"8 1)''23, . 8$+$!20 5.+)')7#"7)&+.( .$&1"'&'"&, "#" .$/1)+6 '$72;. $.#& $9!&1&'"( ".9$#6/5&0$4$ ".+$@'"8) 1)''23 " "1&'+">"8)="" 9$#6/$7)+( $+$*!)%)&+.( 1")#$4 .$ 7.&0" +)*#"=)0" " 9!&1.+)7#&'"(0" ".+$@'"8) 1)''23, 9!"')1#&%)-"3 1)''$05 9$#6/$7)+,. D <+$0 1")#$4& 0$%'$ 58)/)+6 +)*#"=5 *)/2 1)''23, 8 8$+$!$; *51&+ 729$#'(+6.( 1$.+59.
I) .+!)'"=& Generated Classes *51&+ $+$*!)%&' .9".$8 8#)..$7, 8$+$!2; MFC Application Wizard )7+$0)+"@&.8" .$/1).+ 7 >$!0"!5&0$0 9!"#$%&'"".
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
136
J#).. $+$*!)%&'"( *51&+ ').#&1$7)' $+ 8#)..) ARecordView, ) 8#).. !&/5#6+"!5,-&4$ ')*$!) $+ 8#)..) CRecordset. ?)#&& AppWizard )7+$0)+"@&.8" .$/1).+ E)*#$' 9!"#$%&'"(. A$/1)''$& 9!"#$%&'"& *51&+ .$1&!%)+6 9)' "'.+!50&'+$7 . 8'$98)0" 1#( 9&!&3$1) 0&%15 /)9".(0" +)*#"=2 *)/2 1)''23, '$ '& *51&+ .$1&!%)+6 <#&0&'+$7 59!)7#&'"( 1#( $+$*!)%&'"( 9$#&; +)*#"=2 *)/2 1)''23. C+$*!)%&'"& 9$#&; *)/2 1)''23 K).+&! MFC Application Wizard .$/1)&+ 7 8#)..& ').#&15&0$0 $+ CRecordset 9&!&0&''2& @#&'2 8#)..) 1#( 8)%1$4$ 9$#( +)*#"=2 9$18#,@)&0$; *)/2 1)''23. I)9!"0&!: CODBC_1Set::CODBC_1Set(CDatabase* pdb) // !"#$%&'$!% '()##) : CRecordset(pdb) // ")#(*+&*,!-! !$ CRecordset { m_f1 = 0; // .*%*,*"")/ 0(*" '()##) +(/ 1--! 1!(/ m_f2 = 0; // .*%*,*"")/ 0(*" '()##) +(/ 2--! 1!(/ m_f3 = L""; m_f4; m_nFields = 4; // !(20*#$3! 1!(*4 m_nDefaultType = dynaset; } void CODBC_1Set::DoFieldExchange(CFieldExchange* pFX) { pFX->SetFieldType(CFieldExchange::outputColumn); RFX_Long(pFX, _T("[f1]"), m_f1); RFX_Long(pFX, _T("[f2]"), m_f2); RFX_Text(pFX, _T("[f3]"), m_f3); RFX_Date(pFX, _T("[f4]"), m_f4); ?#( +$4$ @+$*2 $+$*!)/"+6 7 >$!0& 9$#( *)/2 1)''23 .#&15&+: 1. D !&1)8+$!& !&.5!.$7 1#( 8)%1$4$ 9$#( +)*#"=2 '&$*3$1"0$ 1$1)7"+6 .$$+7&+.+75,-"; &05 <#&0&'+ 59!)7#&'"(.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
137
2.
D 0&+$1 DoDataExchange 8#)..) $+$*!)%&'"( .#&15&+ 7.+)7"+6 72/$72 0&+$1$7 DDX_FieldText ("#" DDX_FieldCheck, DDX_FieldRadio, DDX_FieldSlider " +.9.), 729$#'(,-"3 .7(/6 0&%15 "1&'+">"8)+$!$0 !&.5!.) " 9&!&0&''$; @#&'$0 8#)..) !&/5#6+"!5,-&4$ ')*$!). I)9!"0&!: void CODBC_1View::DoDataExchange(CDataExchange* pDX) { CRecordView::DoDataExchange(pDX); DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_f1, m_pSet); DDX_FieldText(pDX, IDC_EDIT2, m_pSet->m_f2, m_pSet); } $.#& <+$4$ /)9"." *)/2 1)''23 *515+ $+$*!)%)+6.( 7 $8'& 1$850&'+) 7 <#&0&'+)3 59!)7#&'"( $8'$ !&1)8+"!$7)'"(.
:"*#"$+&8) MFC 9!&1$.+)7#(&+ 51$*'2; "'+&!>&;. 1$.+59) 8 *)/)0 1)''23 " 72.$8"; 5!$7&'6 )*.+!)8="" $+ 8$'8!&+'23 ".9$#6/5&023 .!&1.+7 ODBC "#" DAO. ?#( 8)%1$; $+8!2+$; *)/2 1)''23 .$/1)&+.( $+1'2; $*L&8+ $!"! +!--./ (8#)..) 9!$"/7$1'$4$ $+ CDatabase "#" CDaoDatabase). M.9$#6/5( <+$ $*L&8+ 0$%'$ 729$#'"+6 9$1.$&1"'&'"& 8 *)/& 1)''23.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
138
?!54$; $*L&8+ *"*#"$+&8" MFC #*"4,5&0#46702 -!$%# (8#)..) 9!$"/7$1'$4$ $+ CRecordset "#" CDaoRecordset), 9$/7$#(&+ 729$#'(+6 /)9!$.2 " 7'$."+6 "/0&'&'"( 7 +)*#"=2 *)/2 1)''23. J#)..2 DAO 9!&1$.+)7#(,+ +)8%& 1$9$#'"+'2& 7$/0$%'$.+" - ".9$#6/$7)'"& $*L&8+$7 1#( !)*$+2 .$ .+!58+5!$; +)*#"=2 (CDaoTableDef) " 1#( .$3!)'(&023 /)9!$.$7 (CDaoQueryDef). ?#( $+$*!)%&'"( /)9".&; *)/2 1)''23 7 !)08)3 )!3"+&8+5!2 1$850&'+$+$*!)%&'"& !&)#"/$7)'2 8#)..2 $+$*!)%&'"; CRecordView " CDaoRecordView, 9!$"/7$1'2& $+ 8#)..) CView. N)..0$+!"0 9!"0&! SDI-9!"#$%&'"(, !)/!)*$+)''$4$ ') $.'$7& E)*#$') 9!"#$%&'"( .$/1)''$4$ MFC Application Wizard . B+$ 9!"#$%&'"& !&)#"/5&+ 1$.+59 8 +)*#"=& *)/2 1)''23. D 8)@&.+7& *)/$7$4$ 8#)..) $*L&8+) $+$*!)%&'"( *51&+ ".9$#6/$7)' 8#).. CRecordView. D 8#)..& 1$850&'+) *51&+ .$/1)' ')*$! /)9".&; 8#)..) 9!$"/7$1'$4$ $+ 8#)..) CRecordset. C*0&' 1)''20" . +)*#"=&; *51&+ !&)#"/$7)' 9$.!&1.+7$0 RFX-0&+$1$7 7 DoFieldExchange. N)..0$+!"0 ')"*$#&& 7)%'2& >!)40&'+2 8$1).
80(&0-9 ',!((! %&%$#!1*-0: -!(,*+4*;%9% %& CRecordView: // ODBC_1View.cpp : %*)(25)62/ '()##) !$!7%)8*"2/ #include "stdafx.h" #include "ODBC_1.h" #include "ODBC_1Set.h" #include "ODBC_1Doc.h" #include "ODBC_1View.h" IMPLEMENT_DYNCREATE(CODBC_1View, CRecordView) BEGIN_MESSAGE_MAP(CODBC_1View, CRecordView) // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview) END_MESSAGE_MAP() CODBC_1View::CODBC_1View() // !"#$%&'$!% : CRecordView(CODBC_1View::IDD) { m_pSet = NULL;} CODBC_1View::~CODBC_1View() { }
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
139
void CODBC_1View::DoDataExchange(CDataExchange* pDX) { CRecordView::DoDataExchange(pDX); // 91%*+*(*"2* #3/52 ,*8+& :(*,*"$),2 &1%)3(*"2/ 2 1*%*,*"";,2 // 0(*"),2 '()##) %*5&(<$2%&=>*-! ")7!%) DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_f1, m_pSet); DDX_FieldText(pDX, IDC_EDIT2, m_pSet->m_f2, m_pSet); DDX_FieldText(pDX, IDC_EDIT3, m_pSet->m_f3, m_pSet); } BOOL CODBC_1View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: ,*#$! +(/ %*+)'$2%!3)"2/ // #$%&'$&%; CREATESTRUCT cs return CRecordView::PreCreateWindow(cs); } void CODBC_1View::OnInitialUpdate() { m_pSet = &GetDocument()->m_ODBC_1Set; // ?3/5;3)"2* +!'&,*"$) // # %*5&(<$2%&=>2, ")7!%!, CRecordView::OnInitialUpdate(); } BOOL CODBC_1View::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CODBC_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: ,*#$! +(/ '!+), 3;1!("/*,!-! 1*%*+ 1*0)$<= +!'&,*"$) } void CODBC_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } // CODBC_1View – 1!++*%8') %)7!$; # 7)5!4 +)"";@ CRecordset* CODBC_1View::OnGetRecordset() { return m_pSet; }
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ 80(&0-9 ',!((! #*"4,5&0#467*9% -!$%#! -!(,*+4*;%9% %& CRecordset: // ODBC_1Set.cpp : %*)(25)62/ '()##) %*5&(<$2%&=>*-! ")7!%) #include "stdafx.h" #include "ODBC_1.h" #include "ODBC_1Set.h" IMPLEMENT_DYNAMIC(CODBC_1Set, CRecordset) CODBC_1Set::CODBC_1Set(CDatabase* pdb) // !"#$%&'$!% : CRecordset(pdb) { m_f1 = 0; // A"262)(25)62/ 0(*"!3 '()##) m_f2 = 0; m_f3 = L""; m_f4; m_nFields = 4; m_nDefaultType = dynaset; } CString CODBC_1Set::GetDefaultConnect() { return _T("DSN=MySQLDB;DBQ=C:\\C_Project\\db1.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;PWD=one;UID=admin;"); } CString CODBC_1Set::GetDefaultSQL() { return _T("[tbl1]"); // A,/ 1%!#,)$%23)*,!4 $)7(26; // 7)5; +)"";@ } void CODBC_1Set::DoFieldExchange(CFieldExchange* pFX) { pFX->SetFieldType(CFieldExchange::outputColumn); RFX_Long(pFX, _T("[f1]"), m_f1); RFX_Long(pFX, _T("[f2]"), m_f2); RFX_Text(pFX, _T("[f3]"), m_f3); RFX_Date(pFX, _T("[f4]"), m_f4); }
140
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
141
45#++ CDatabase C*L&8+ CDatabase $*&.9&@"7)&+ .!&1.+7) 9$18#,@&'"( 8 ".+$@'"85 1)''23. D 8)@&.+7& ".+$@'"8) 1)''23 0$45+ 72.+59)+6 Microsoft SQL Server, Microsoft Access, Oracle " +.9. C1'$7!&0&''$ 7 9!"#$%&'"" 0$%&+ ".9$#6/$7)+6.( '&.8$#68$ )8+"7'23 $*L&8+$7 CDatabase. C*L&8+ CDatabase 9$/7$#(&+ 5.+)')7#"7)+6 9$.+$(''$& .$&1"'&'"& . *)/$; 1)''23, 8$+$!$& '& 9!&19$#)4)&+.( /)8!27)+6 9$.#& 8)%1$; $9&!)="" 72*$!8" "#" $*'$7#&'"( 1)''23. H&0 .)020, &.#" 1)''2& 9!&19$#)4)&+.( ".9$#6/$7)+6 7 "'+&!)8+"7'$0 !&%"0&, +$ 7!&0( .$&1"'&'"( *51&+ 1$.+)+$@'$ 7"8$. B+$+ 9$13$1 9!$+"7$9$#$%&' 9$13$15, ".9$#6/5&0$05 ADO.NET, 41& 7!&0( .$&1"'&'"( .7&1&'$ 8 0"'"0505. ?#( ".9$#6/$7)'"( *)/2 1)''23 .#&15&+: 1. A$/1)+6 $*L&8+ CDatabase. 2. O.+)'$7"+6 .$&1"'&'"&, $+8!27 *)/5 1)''23 (72/7)7 0&+$1 CDatabase::OpenEx "#" CDatabase::Open). 3. A$/1)+6 $*L&8+ CRecordset 1#( $9&!)="; ')1 9$1.$&1"'&''20 ".+$@'"8$0 1)''23, 9&!&1)7 8$'.+!58+$!5 58)/)+ ') CDatabase. 4. ?#( /)7&!E&'"( !)*$+2 /)8!2+6 *)/5 1)''23, 72/7)7 0&+$1 CDatabase::Close. B+$ +)8%& /)8!$&+ 7.& ')*$!2 /)9".&;. ?#( ".9$#6/$7)'"( 8#)..) CDatabase .#&15&+ 9$18#,@"+6 /)4$#$7$@'2; >);# afxdb.h. D 8#).. CDatabase 7,@&'2 .#&15,-"& @#&'2 8#)..): CDatabase::m_hdbc O8)/)+ ') 9$1.$&1"'&''2; ODBC ".+$@'"8 1)''23. I)9!"0&!: // A#1!(<5!3)"2* m_hdbc +(/ 1%/,!-! 3;5!3) ODBC API. // m_db 3(/*$#/ !7B*'$!, CDatabase, // m_hdbc - 0(*" '()##) CDatabase nRetcode = ::SQLGetInfo( m_db.m_hdbc, SQL_ODBC_SQL_CONFORMANCE, &nValue, sizeof( nValue ), &cbValue );
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
142
CDatabase( ); J$'.+!58+$! $*L&8+) CDatabase. virtual BOOL OpenEx (LPCTSTR lpszConnectString, DWORD dwOptions = 0); throw( CDBException, CMemoryException ); K&+$1 $+8!27)&+ *)/5 1)''23. !" 5.9&E'$0 /)7&!E&'"" 0&+$1 7$/7!)-)&+ '&'5#&7$& /')@&'"&. G.#" 9$#6/$7)+ -)&+ 9$ 8'$98& Cancel 7 1")#$4&, /)9!)E"7),-&0 1$9$#'"+'5, "'>$!0)=",, +$ 0&+$1 7$/7!)-)&+ /')@&'"& 0. D $.+)#6'23 .#5@)(3 *!$.)&+.( ".8#,@&'"&. )!)0&+!2: lpszConnectString - $9!&1(&+ ODBC .+!$85 9$18#,@&'"(. C') 78#,@)&+ ".+$@'"8 1)''23 " '&8$+$!5, 1$9$#'"+'5, "'>$!0)=",, +)85, 8)8 "1&'+">"8)+$! 9$#6/$7)+( " 9)!$#6. I)9!"0&!: "DSN=SQLServer_Source;UID=U1;PWD=user1". G.#" 58)/)+6 9)!)0&+! lpszConnectString !)7'20 NULL, +$ 9$(7"+.( .+)'1)!+'2; 1")#$4 Data Source, 9$/7$#(,-"; 9$#6/$7)+, 72*!)+6 1$.+59'2; ".+$@'"8 1)''23. dwOptions - 9$ 50$#@)'", 9)!)0&+! !)7&' '5#,, @+$ 9!&19$#)4)&+, @+$ *)/) 1)''23 *51&+ $+8!2+) . !)/1(&020 1$.+59$0 " . 9!)7)0" /)9"."; ODBC Cursor Library DLL '& *51&+ /)4!5%&'); ODBC 1")#$4 *51&+ 9$(7#(+6.( +$#68$ 7 +$0 .#5@)&, &.#" '&1$.+)+$@'$ "'>$!0)="" 1#( 729$#'&'"( 9$18#,@&'"( 8 *)/& 1)''23. B+$+ 9)!)0&+! (7#(&+.( *"+$7$; 0).8$;, $9!&1(&0$; 8$0*"')="&; .#&15,-"3 /')@&'";: CDatabase::openExclusive – 7 +&85-&; 7&!."" MFC-*"*#"$+&8" '& 9$11&!%"7)&+.(: ".+$@'"8 1)''23 7.&41) $+8!27)&+.( 7 !)/1(&0$0 !&%"0&; CDatabase::openReadOnly – $+8!2+"& ".+$@'"8) 1)''23 +$#68$ 1#( @+&'"(;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
143
CDatabase::useCursorLib – 58)/27)&+ /)4!5/85 *"*#"$+&8" ODBC Cursor Library DLL, 0).8"!5,-&; '&8$+$!5, >5'8="$')#6'$.+6 ODBC-1!);7&!); CDatabase::noOdbcDialog – 9!&1$+7!)-)&+ 9$(7#&'"& 1")#$4) 1#( 9$1.$&1"'&'"( ".+$@'"8) 1)''23; CDatabase::forceOdbcDialog – $*&.9&@"7)&+ $+$*!)%&'"& 1")#$4) . =, $9!&1&'"( "'>$!0)="" 1#( ODBC .$&1"'&'"(. I)9!"0&!: // C#$%)23)"2* !7B*'$) CDatabase 3 1%!253!+";4 '()## +!'&,*"$) CDatabase m_dbBData( ); // 97B*'$ CDatabase: !$'%;$2* 2#$!0"2') +)"";@ $!(<'! ") 0$*"2* m_dbBData.OpenEx( _T( "DSN=MYDATASOURCE;UID=U1" ), CDatabase::openReadOnly | CDatabase::noOdbcDialog ) ); virtual void Close( ); !"#$ %&'()*&!" +#!$,-!-,! + .&%#/ $&--)0. 1(!$*&(,"!23-# +2!$4!" %&'()"3 *+! -&.#() %&5,+!/ &++#6,,(#*&--)0 + .&%#/ $&--)0. 7+! #5!(&6,, AddNew , Edit -&$ -&.#(&8, %&5,+!/ .4$4" 5(!(*&-) , *)5#2-!- #"'&" -!%&*!(9!--)0 "(&-%&'6,/. 1#+2! %&'()",: +#!$,-!-,: #.;!'" CDatabase 8#<-# ,+5#23%#*&"3 $2: #"'()",: $(4=#=# ,+"#>-,'& $&--)0. BOOL IsOpen( ) const; !"#$ #5(!$!2:!", 4+"&-#*2!-# 2, +#!$,-!-,! + .&%#/ $&--)0 $2: #.;!'"& CDatabase. const CString& GetConnect ( ) const; !"#$ *#%*(&?&!" +"(#'4 5#$'2@>!-,:, ,+5#23%#*&--4@ $2: #"'()",: ,+"#>-,'& $&--)0. CString GetDatabaseName ( ) const; !"#$ *#%*(&?&!" ,8: "!'4?!/ 5#$+#!$,-!--#/ .&%) $&--)0. A"# -! #$-# , "#!-,!8 DSN, 4'&%)*&!8)8 $2: 8!"#$#* OpenEx , Open, , %&*,+," #" ODBC. 7 +24>&! *#%-,'-#*!-,: #9,.', 8!"#$ *#%*(&?&!" 54+"4@ +"(#'4.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
144
BOOL CanTransact( ) const; !"#$ 5#%*#2:!" #5(!$!2,"3, 8#<-# 2, ,+5#23%#*&"3 "(&-%&'6,, $2: .&%) $&--)0. void SetLoginTimeout ( DWORD dwSeconds ); !"#$ 4+"&-&*2,*&!" ,-"!(*&2 *(!8!-,, 5# ,+"!>!-,, '#"#(#=# 5#5)"'& 5#$+#!$,-!-,: ' ,+"#>-,'4 $&--)0 .4$!" #"8!-!-&. 1# 48#2>&-,@ B"# %-&>!-,! (&*-# 15 +!'4-$&8. C"8!",8, >"# -! *+! .&%) $&--)0 5#$$!(<,*&@" B"# %-&>!-,!.
BOOL BeginTrans ( ); !"#$ 4+"&-&*2,*&!" -&>&2# "(&-%&'6,,. D(&-%&'6,: 8#&"3 #$,- ,2, -!+'#23'# *)%#*#* 8!"#$#* AddNew, Edit, Delete, Update $2: #.;!'"& '2&++& CRecordset. 1(, %&*!(9!-,, "(&-%&'6,, *)%)*&!"+: 8!"#$ CommitTrans $2: 5#$"*!(<$!-,: *+!0 +$!2&--)0 ,%8!-!-,/ * ,+"#>-,'! $&--)0, ,2, Rollback -$2: #"8!-). BOOL CommitTrans ( ); !"#$ *)5#2-:!" %&*!(9!-,! "(&-%&'6,,. C"8!",8, >"# $2: -&.#(& %&5,+!/ 5#$$!(<,*&!"+: "#23'# #$,- 4(#*!-3 "(&-%&'6,/. 72# <EditItemTemplate> <EditRowStyle /> <EmptyDataRowStyle /> <EmptyDataTemplate> <PagerSettings FirstPageImageUrl="uri" FirstPageText="string" LastPageImageUrl="uri" LastPageText="string" Mode="NextPrevious|Numeric|NextPreviousFirstLast| NumericFirstLast" NextPageImageUrl="uri" NextPageText="string" OnPropertyChanged="PropertyChanged event handler" PageButtonCount="integer" Position="Bottom|Top|TopAndBottom" PreviousPageImageUrl="uri" PreviousPageText="string" Visible="True|False"
191
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
192
/> <PagerStyle /> <PagerTemplate> <SelectedRowStyle />
A#/$70, $#$'/,1,28(2 7$8.$323#$8 GridView 8$13$ /2+,7#0/$9,#./$%,883(8 .4#28, 0).$!-*4" 82#$+ UpdateRow. A!2+4560: ./082/ 0!!5)#/0/42# 0).$!-*$9,302 +,33$%$ 82#$+,. <%@ Page language="C#" %> <script runat="server"> void UpdateRowButton_Click(Object sender, EventArgs e) { // +), - ./$ 0# 01* $/'23/4 ,#5&(& // - "/6&./ "/0#'$&" -#7&* GridView1.UpdateRow(GridView1.EditIndex, true); } void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) { // 8/1#/. '7 5'2 UpdateRowButton 0 ($257 4 $ 19' , // /(1& ' .5 7/7$ GridView 7#: 0&$(* // - "/6&./ "/0#'$&" -#7&* switch (e.CommandName) { case "Edit": UpdateRowButton.Enabled = true; break; case "Cancel": UpdateRowButton.Enabled = false;
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
193
break; case "Update": UpdateRowButton.Enabled = false; break; default: UpdateRowButton.Enabled = false; break; } }
!"# 10. $#%&'# ( )#**+", - .&/"#'! XML !"#$%&'( )*+#,-+*$.)#&%' XML DOM (Document Object Model) "9!"2#)" 8$+2!-5 ./$+)#,9!230" +,33(=, .$)#/$233$: 3, ',*2 )#/47#4/( $'D27#$9. B)2 +$74823#( 9 DOM 0825# +/29$90+345 !$%0&2)745 )#/47#4/4. B2/;03$: +2/29, )!410# $+03 7$/329$:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
195
328 +,33(2. G2%0 8$%4# )$+2/1,#- 982)#$ +,33(= +/4%02 #2%0. H#7/(9,560: #2% 8$12# )$+2/1,#-,#/0'4#(. XML-C,:! 3,&03,2#)" ) ./$!$%,, 9 7$#$/$8 8$12# '(#- 47,*,3 3$82/ 92/)00, 7$+0/$97,, *,+,3$ $./2+2!2302 #0., +$74823#, DTD (Document Type Defenition). H./2+2!2302 #0., +$74823#, 8$12# '(#- /,).$!$123$ 9 ),8$8 C,:!2 0!0 3,=$+0#-)" 9 $#+2!-3$8 C,:!2. @#$ 47,*(9,2#)" 9 )#/$72 ./$!$%, DOCTYPE. F,./082/: H./2+2!2302 #0., +$74823#, $.0)(9,2# A4CC07) + $./2+2!"2# 3,!0&02 +$&2/30=
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
196
E)!0 *,+,#- .,/,82#/ #CDATA, #$ +!" <#0= +,33(= 32 '4+2# $./2+2!"#-)" 0= )$$#92#92#)#902 $./2+2!2305 #0., +$74823#,. ?$)!2 ./$!$%, /,).$!,%,2#)" #2!$ +$74823#,, )$)#$"622 0* 4*!$9, $.0)(9,560= 03)#/47>00 .$ $'/,'$#72 +,33(=, )$+2/108$2, ,#/0'4#(, ))(!70 3, $'D27#(, 7$8823#,/00, C/,%823#( +$74823#$9. K#/0'4#( 8$%4# '(#- .$!-*$9,#2!-)702 0 9)#/$23392 ($./2+2!"28(2 ).2>0C07,>02: "*(7, XML). A)(!7, 3, $'D27# 47,*(9,2#)" 2%$ 082328 ) ./2C07)$8 &. J$8823#,/00 47,*(9,5#)" 934#/0 #2%, . J!,)) DOM XML ./2+)#,9!"2# XML-+$74823# 9 .,8"#0, 0 .$*9$!"2# &0#,#0 *,.0)(9,#- +$74823#. !" +$)#4., 7 XML-+$74823#4 8$13$ 0).$!-*$9,#-C437>00 Simple API for XML (SAX). ?/0 <#$8 92)- XML-+$74823# 32 *,%/41,2#)" 9 .,8"#-, , 9(.$!3"2#)" )03#,7)0&2)70: ,3,!0* +$74823#,, ./0 7$#$/$8 030>00/45#)" )$$#92#)#945602 )$'(#0". B Framework .NET 7,1+(: $#+2!-3(: 4*2!, ./2+)#,9!"560:
/,'00 XmlDocument !" #$%&'#() XML-*+(',-./$ *+0/$/+1.+ 234+!.)/5 0!-*'67)8 (+*: XmlDocument doc= new XmlDocument(); doc.Load("MyDataFile.xml"); Console.WrineLine(doc.InnerXml.ToString); !" #$%&'#() XML-*+(',-./$ ,+9.+ )04+!5#+2$/5 0!-*'67)- ,-/+*3; Load("),"_xml_:$8!$") LoadXml("0+*-&9$.)-_xml_*+(',-./$").
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
197
!" 0+;&$.-.)" XML-*+(',-./$ ,+9.+ 4&),-."/5,-/+* Save("),"_:$8!$") (!$00$ XmlDocument. <$9*38 '#-! XML-*+(',-./$ 4&-*0/$2!"-/0"(!$00+, XmlNode. !" 4+0!-*+2$/-!5.+%+ *+0/'4$ (+ 20-, *+1-&.), '#!$, XML-*+(',-./$ 0!-*'-/ )04+!5#+2$/5 02+80/2+ ChildNodes (!$00$ XmlDocument. =$4&),-&: private void button2_Click(object sender, EventArgs e) { mydb1DataSet.Tbl1.WriteXml("MyDataFile.xml"); XmlDocument doc = new XmlDocument(); doc.Load("MyDataFile.xml"); int i, count; count = 0; i = 1; XmlNode node = doc.ChildNodes[1]; foreach (XmlNode node1 in node.ChildNodes) { Console.WriteLine("-------"); // !"#$%&'( )*+ ,&-. /"0+$1'+ 2 /%11"(, foreach (XmlNode node2 in node1.ChildNodes) { Console.WriteLine(doc.DocumentElement.FirstChild.ChildNodes[count] .Name + "= " + node2.FirstChild.Value); count++; } i++; count = 0; } }
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
198
>32+* 0/&+( XML-*+(',-./$ 4&) 4+0!-*+2$/-!5.+, 1/-.)) '#!+2 *+(',-./$.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
199
!"##$ XmlReader % XmlWriter !" &-$!)#$?)) +*.+.$4&$2!-..+%+ 1/-.)" XML-*+(',-./$ )04+!5#'6/0" (!$003, .$0!-*'-,3- +/ XmlReader: XmlTextReader – 4+#2+!"-/ 1)/$/5 XML-*+(',-./3 @-# ); 4&+2-&(); XmlValidatingReader – 4+#2+!"-/ 1)/$/5 XML-*+(',-./3 ) 4&-*+0/$2!"-/ *+0/'4 ( 0;-,- *+(',-./$.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
200
// 3%4'*5 %!$'#,!% writer.WriteAttributeString("xmlns", null, "urn:ReadWriteXML"); // 3%4'*5 7-+(+1!% title writer.WriteStartElement("title"); writer.WriteString("8%&)%1'+ 1"); writer.WriteEndElement(); // 3%4'*5 7-+(+1!% autor writer.WriteElementString ("autor", "'(9 1"); // 3%4'*5 7-+(+1!% price. writer.WriteElementString("price", "135");
// 3%2$.)%+( !+6 /-9 2"$1+)"6" 7-+(+1!% writer.WriteEndElement(); // 3%4'*5 XML-:%;-% ' &%2$.!'+ 4"!"2% XML-/%11.< writer.Flush(); writer.Close(); } finally { if (writer != null) writer.Close(); } } }
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
201
A-/+* WriteStartElement (!$00$ XmlTextWriter #$4)032$-/ +/(&32$67)8 /-% #$*$2$-,+%+ B!-,-./$, $ ,-/+* WriteEndElement – #$4)032$-/ #$(&32$67/8 /-%. A-/+* WriteStartDocument #$4)032$-/ 2 .$1$!+ *+(',-./$ 0/&+(' . A-/+* WriteElementString #$4)032-/ 0),2+!5.3- *$..3- B!-,-./$.