機能 特徴

➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
䕔 ᶵ⬟
䝕䞊䝍䝥䝻䝞䜲䝎䞊䛿䚸䝕䞊䝍䝧䞊䝇䛻᥋⥆䛧䛯䜚䚸䜽䜶䝸䜢ᐇ⾜䛧䛯䜚䛩䜛䛸䛝䛻฼⏝䛧䜎䛩䚹䛣䛾
㝿䚸」ᩘ䛾䜸䝤䝆䜵䜽䝖䜢⤌䜏ྜ䜟䛫䛶౑⏝䛧䜎䛩䚹䛯䛸䛘䜀䚸䝕䞊䝍䝧䞊䝇䛸䛾᥋⥆䛿 Connection
䜸䝤䝆䜵䜽䝖䛷䛚䛣䛺䛔䚸㑅ᢥ䜽䜶䝸䜔᭦᪂䜽䜶䝸䛾ᐇ⾜䛿 Command 䜸䝤䝆䜵䜽䝖䛷䛚䛣䛺䛔䜎䛩䚹䜎
䛯䚸䜽䜶䝸䛻䜘䛳䛶㏉䛥䜜䜛⤖ᯝ䛿䚸DataReader 䜸䝤䝆䜵䜽䝖䛷ㄞ䜏ྲྀ䜚䜎䛩䚹
䕔 ≉ᚩ
䝕䞊䝍䝥䝻䝞䜲䝎䞊䛜ᥦ౪䛩䜛 DataReader 䜸䝤䝆䜵䜽䝖䛿䚸㧗㏿䛻ㄞ䜏ྲྀ䜚ᑓ⏝䛾䜰䜽䝉䝇䛜䛷䛝
䜛䛯䜑䚸䝕䞊䝍䝧䞊䝇䛾᳨⣴⤖ᯝ䜢⾲♧䛩䜛䛾䛻㐺䛧䛶䛔䜎䛩䚹䜎䛯䚸DataReader 䜸䝤䝆䜵䜽䝖䛿䚸1
⾜䛪䛴ฎ⌮䛩䜛䛾䛷䚸䝯䝰䝸䛻䛿ᖖ䛻 1 ௳ศ䛾䝕䞊䝍䛧䛛Ꮡᅾ䛧䜎䛫䜣䚹䛭䛾䛯䜑䚸㑅ᢥ䜽䜶䝸䛾ᐇ
⾜䛻䜘䛳䛶኱㔞䛾䝕䞊䝍䛜᳨⣴䛥䜜䛶䜒䚸౑⏝䛥䜜䜛䝯䝰䝸䛿᭱ᑠ㝈䛷䛩䜏䜎䛩䚹
14
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
䝕䞊䝍䝥䝻䝞䜲䝎䞊䛿䚸䝕䞊䝍䝋䞊䝇䛸䛾䜲䞁䝍䞊䝣䜵䜲䝇䛷䛒䜛䛯䜑䚸䝕䞊䝍䝋䞊䝇䛤䛸䛻⏝ព䛩䜛
ᚲせ䛜䛒䜚䜎䛩䚹.NET Framework 2.0 ௨㝆䛷䛿䚸௨ୗ䛾 4 䛴䛾䝕䞊䝍䝥䝻䝞䜲䝎䞊䛜ྵ䜎䜜䛶䛔䜎
䛩䚹
䕔 SQL Server 䝕䞊䝍䝥䝻䝞䜲䝎䞊
SQL Server 7.0 ௨㝆䛾䛯䜑䛻㛤Ⓨ䛥䜜䛯䝕䞊䝍䝥䝻䝞䜲䝎䞊䛷䛩䚹䜰䞁䝬䝛䞊䝆䝁䞊䝗䜢฼⏝䛧䛺
䛔䛯䜑䚸OLE DB 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛩䜛䜘䜚䜒㧗㏿䛷䛩䚹
䕔 Oracle 䝕䞊䝍䝥䝻䝞䜲䝎䞊
Oracle DB 䛾䛯䜑䛻㛤Ⓨ䛥䜜䛯䝕䞊䝍䝥䝻䝞䜲䝎䞊䛷䛩䚹.NET Framework 4.5 ௨㝆䛷䛿䝃䝫䞊䝖
䛥䜜䜎䛫䜣䛾䛷䚸Oracle ♫䛜ᥦ౪䛧䛶䛔䜛 ODP.NET䠄Oracle Data Provider for .NET䠅䜢฼⏝䛧䜎
䛩䚹
䕔 OLE DB 䝕䞊䝍䝥䝻䝞䜲䝎䞊
OLE DB 䝥䝻䝞䜲䝎䞊䛜ᥦ౪䛥䜜䛶䛔䜛䝕䞊䝍䝋䞊䝇⏝䛾䝕䞊䝍䝥䝻䝞䜲䝎䞊䛷䛩䚹OLE DB 䝥䝻䝞
䜲䝎䞊䜢฼⏝䛩䜛䛯䜑䚸䝬䝛䞊䝆䝁䞊䝗䛸䜰䞁䝬䝛䞊䝆䝁䞊䝗䛾ቃ⏺㉺䛘䛾䜸䞊䝞䞊䝦䝑䝗䛜䛒䜚䜎
䛩䚹
䕔 ODBC 䝕䞊䝍䝥䝻䝞䜲䝎䞊
ODBC 䝗䝷䜲䝞䞊䛜ᥦ౪䛥䜜䛶䛔䜛䝕䞊䝍䝋䞊䝇⏝䛾䝕䞊䝍䝥䝻䝞䜲䝎䞊䛷䛩䚹ODBC 䝗䝷䜲䝞䞊䜢
฼⏝䛩䜛䛯䜑䚸䝬䝛䞊䝆䛸䜰䞁䝬䝛䞊䝆䝁䞊䝗䛾ቃ⏺㉺䛘䛾䜸䞊䝞䞊䝦䝑䝗䛜䛒䜚䜎䛩䚹
䛣䜜䜙䛾.NET 䝥䝻䝞䜲䝎䞊௨እ䛻䜒䚸䛥䜎䛦䜎䛺䝧䞁䝎䞊䜔䝃䞊䝗䝟䞊䝔䜱䛛䜙䚸䝕䞊䝍䝥䝻䝞䜲
䝎䞊䛜ᥦ౪䛥䜜䛶䛔䜎䛩䚹
15
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
䝫䜲䞁䝖䟿䠖SQL Server 䜔 Oracle 䛻䛿䚸OLE DB 䝥䝻䝞䜲䝎䞊䜔 ODBC 䝗䝷䜲䝞䞊䛜ᥦ౪䛥䜜䛶䛔
䜛䛯䜑䚸OLE DB䚸ODBC 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛩䜛䛣䛸䜒䛷䛝䜎䛩䚹䛯䛰䛧䚸䛣䜜䜙䛿䜰䞁
䝬䝛䞊䝆䝁䞊䝗䛾䛯䜑䚸䜰䞁䝬䝛䞊䝆䛸䝬䝛䞊䝆䛾ቃ⏺㉺䛘䛜㉳䛣䜚䚸䜸䞊䝞䞊䝦䝑䝗䛜⏕䛨䜎
䛩䚹ᑓ⏝䛾䝕䞊䝍䝥䝻䝞䜲䝎䞊䛜ᥦ౪䛥䜜䛶䛔䜛ሙྜ䛿䚸䛭䜜䜢฼⏝䛧䛯䜋䛖䛜䚸䝟䝣䜷䞊䝬䞁
䝇䛜ྥୖ䛧䜎䛩䚹
㻌
䝫䜲䞁䝖䟿䠖䝕䞊䝍䝥䝻䝞䜲䝎䞊䛾ྛ䜽䝷䝇ྡ䛻䛿䚸䝥䝻䝞䜲䝎䞊ẖ䛾䝥䝸䝣䜱䝑䜽䝇䛜䛴䛔䛶䛔䜎䛩䚹
䛯䛸䛘䜀䚸Connection 䜸䝤䝆䜵䜽䝖䛾䜽䝷䝇ྡ䛿䚸௨ୗ䛾䜘䛖䛻䛺䜚䜎䛩䚹
䝕䞊䝍䝥䝻䝞䜲䝎䞊䛾✀㢮
Connection 䛾䜽䝷䝇ྡ
SQL Server 䝕䞊䝍䝥䝻䝞䜲䝎䞊
SqlConnection
Oracle 䝕䞊䝍䝥䝻䝞䜲䝎䞊
OracleConnection
OLE DB 䝕䞊䝍䝥䝻䝞䜲䝎䞊
OleDbConnection
ODBC 䝕䞊䝍䝥䝻䝞䜲䝎䞊
OdbcConnection
㻌
㻌
㻌
16
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
䕔 Connection
≉ᐃ䛾䝕䞊䝍䝋䞊䝇䛸䛾᥋⥆ᶵ⬟䜢ᥦ౪䛧䜎䛩䚹
䕔 Transaction
䝕䞊䝍䝋䞊䝇䛾䝖䝷䞁䝄䜽䝅䝵䞁ᶵ⬟䜢฼⏝䛩䜛䛸䛝䛻౑⏝䛧䜎䛩䚹(5 ❶)
䕔 Command
䝕䞊䝍䝋䞊䝇䛻ᑐ䛧䛶䚸䝁䝬䞁䝗䛾ᐇ⾜䜢せồ䛧䜎䛩䚹SQL 䜽䜶䝸䜔䝇䝖䜰䝗䝥䝻䝅䞊䝆䝱䜢ᐇ⾜䛩䜛
䛸䛝䛻౑⏝䛧䜎䛩䚹
䕔 Parameter
SQL 䜽䜶䝸䜔䝇䝖䜰䝗䝥䝻䝅䞊䝆䝱䛾ධຊ䝟䝷䝯䞊䝍䞊䚸ฟຊ䝟䝷䝯䞊䝍䞊䚸䛚䜘䜃ᡠ䜚್䝟䝷䝯䞊
䝍䞊䜢ᐃ⩏䛩䜛䛸䛝䛻౑⏝䛧䜎䛩䚹
䕔 DataReader
䝕䞊䝍䝋䞊䝇䛛䜙๓᪉ྥ䚸ㄞ䜏ྲྀ䜚ᑓ⏝䛾䝕䞊䝍䝇䝖䝸䞊䝮䜢ྲྀᚓ䛧䜎䛩䚹
䕔 DataAdapter
DataSet 䜸䝤䝆䜵䜽䝖䛸䝕䞊䝍䝋䞊䝇䛾ᶫΏ䛧䜢䛧䜎䛩䚹(3 ❶)
䕔 ConnectionStringBuilder
Connection 䜸䝤䝆䜵䜽䝖䛾 ConnectionString 䝥䝻䝟䝔䜱䛜䚸⡆༢䛻タᐃ䛷䛝䜎䛩䚹
䕔 CommandBuilder
DataAdapter 䜸䝤䝆䜵䜽䝖䛜౑⏝䛩䜛᭦᪂䜽䜶䝸䜢⮬ື⏕ᡂ䛧䜎䛩䚹(3 ❶)
17
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
䕔 Exception
䝕䞊䝍䝋䞊䝇䛷౛እ䛜Ⓨ⏕䛧䛯䛸䛝䛻⏕ᡂ䛥䜜䜎䛩䚹
18
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
19
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
䕔 Connection 䜸䝤䝆䜵䜽䝖䛾⏕ᡂ
䝕䞊䝍䝋䞊䝇䛻᥋⥆䛩䜛䛯䜑䛻䛿䚸Connection 䜸䝤䝆䜵䜽䝖䜢౑⏝䛧䜎䛩䚹Connection 䜸䝤䝆䜵䜽䝖
䛿 New / new 䛷⏕ᡂ䛧䜎䛩䚹
䕔 ᥋⥆ᩥᏐิ䛾タᐃ
䝕䞊䝍䝋䞊䝇䛻᥋⥆䛩䜛䛯䜑䛻䛿䚸䝴䞊䝄䞊ྡ䜔䝟䝇䝽䞊䝗䛺䛹䛾᝟ሗ䛜ᚲせ䛷䛩䚹䛣䜜䜙䛾᝟ሗ
䛿䚸᥋⥆ᩥᏐิ䛸䛧䛶 ConnectionString 䝥䝻䝟䝔䜱䛻タᐃ䛧䜎䛩䚹
᥋⥆ᩥᏐิ䛿䚸䛂䝟䝷䝯䞊䝍䞊=್䛃䛾⤌䜏ྜ䜟䛫䜢䛂;䛃(䝉䝭䝁䝻䞁)䛷༊ษ䛳䛶సᡂ䛧䜎䛩䚹᥋⥆ᩥᏐ
ิ䛿䝕䞊䝍䝥䝻䝞䜲䝎䞊䛻┤᥋Ώ䛥䜜䜛್䛷䛒䜛䛯䜑䚸䝕䞊䝍䝥䝻䝞䜲䝎䞊䛾✀㢮䛻䜘䛳䛶ᚲせ䛺䝟
䝷䝯䞊䝍䞊䛿␗䛺䜚䜎䛩䚹䛚䜒䛺䝕䞊䝍䝥䝻䝞䜲䝎䞊䛾᥋⥆ᩥᏐิ䛾౛䛿䚸௨ୗ䛾䛸䛚䜚䛷䛩䚹
䖃 SQL Server 䝕䞊䝍䝥䝻䝞䜲䝎䞊
Data Source=localhost; Initial Catalog=coffeeDB;
User Id=sa; Password=P@ssw0rd
䖃 Olacle 䝕䞊䝍䝥䝻䝞䜲䝎䞊
Data Source=localhost;User ID=scott;Password=tiger
䖃 OLE DB 䝕䞊䝍䝥䝻䝞䜲䝎䞊
Microsoft OLE DB Provider for SQL Server 䛾ሙྜ
Provider=SQLOLEDB㸹Data Source=localhost;
Initial Catalog=coffeeDB; User Id=sa; Password=pass
Microsoft OLE DB Provider for Oracle 䛾ሙྜ
Provider=MSDAORA; User Id=scott㸹 Password=tiger;
20
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
Data Source=localhost
Microsoft Jet 4.0 OLE DB Provider 䛾ሙྜ
Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:¥NWind.mdb
㻌
ཧ⪃䠖ConnectionString 䝥䝻䝟䝔䜱䜢タᐃ䛩䜛௦䜟䜚䛻䚸䜲䞁䝇䝍䞁䝇䜢⏕ᡂ䛩䜛㝿䛾ᘬᩘ䛸䛧䛶䚸᥋
⥆ᩥᏐิ䜢ᣦᐃ䛩䜛䛣䛸䜒䛷䛝䜎䛩䚹
䛆VB䛇
1. cn = New SqlConnection("Data Source=localhost;" _
2.
& "Initial Catalog=coffeeDB;" _
3.
& "User Id=sa; Password=pass;")
䛆C#䛇
1. cn = new SqlConnection("Data Source=localhost;" _
2.
+ "Initial Catalog=coffeeDB;"
3.
+ "User Id=sa; Password=pass;");
䕔 ᥋⥆䞉ษ᩿
䝕䞊䝍䝧䞊䝇䛸᥋⥆䛩䜛䛻䛿 Connection 䜸䝤䝆䜵䜽䝖䛾 Open 䝯䝋䝑䝗䜢ᐇ⾜䛧䜎䛩䚹䝕䞊䝍䝧䞊䝇
䜢౑䛔⤊䛘䛯䜙ษ᩿䛧䜎䛩䚹ษ᩿䛩䜛䛻䛿 Close 䝯䝋䝑䝗䜢ᐇ⾜䛧䜎䛩䚹
䛆VB䛇
1. Dim cn As New SqlConnection
2. cn.ConnectionString = "Data Source=localhost;" _
3.
& "Initial Catalog=coffeeDB;User Id=sa; Password=pass"
4. cn.Open()
5. 'ࢹ࣮ࢱ࣮࣋ࢫࡢ฼⏝
6. cn.Close()
䛆C#䛇
1. SqlConnection cn = new SqlConnection();
2. cn.ConnectionString = "Data Source=localhost;"
3.
+ "Initial Catalog=coffeeDB;User Id=sa; Password=pass";
4. cn.Open();
5. //ࢹ࣮ࢱ࣮࣋ࢫࡢ฼⏝
6. cn.Close();
21
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
Connection 䜸䝤䝆䜵䜽䝖䛿䚸䝁䞁䝖䝻䞊䝹䛷సᡂ䛩䜛䛣䛸䜒䛷䛝䜎䛩䚹䛣䜜䛻䜘䜚䚸䝁䞊䝗䛷⏕ᡂ䛩䜛
ᚲせ䛜䛺䛟䛺䛳䛯䜚䚸䝥䝻䝟䝔䜱䜢䝥䝻䝟䝔䜱䜴䜱䞁䝗䜴䛷タᐃ䛩䜛䛣䛸䛜䛷䛝䜛䜘䛖䛻䛺䛳䛯䜚䛩䜛䛯䜑䚸
㛤Ⓨ䛾ຠ⋡䛜ྥୖ䛧䜎䛩䚹
䕔 Connection 䝁䞁䝖䝻䞊䝹䛾⾲♧
Connection 䝁䞁䝖䝻䞊䝹䛿䚸᪤ᐃ䛷䛿䝒䞊䝹䝪䝑䜽䝇䛻⾲♧䛥䜜䛺䛔䛯䜑䚸䝒䞊䝹䝪䝑䜽䝇䜢ྑ䜽
䝸䝑䜽䛧䚸[䜰䜲䝔䝮䛾㑅ᢥ]䜢㑅䜃䜎䛩䚹
䛂䝒䞊䝹䝪䝑䜽䝇䜰䜲䝔䝮䛾㑅ᢥ䛃䝎䜲䜰䝻䜾䝪䝑䜽䝇䛜⾲♧䛥䜜䛯䜙䚸䛂XxxConnection䛃䜢㑅ᢥ䛧䜎
䛩䚹
㻌
䝫䜲䞁䝖䟿䠖Connection 䝁䞁䝖䝻䞊䝹䛿䚸ASP.NET 䜰䝥䝸䜿䞊䝅䝵䞁䛷䛿⾲♧䛷䛝䜎䛫䜣䚹
22
➨ 2 ❶㻌 䝕䞊䝍䝥䝻䝞䜲䝎䞊䜢฼⏝䛧䛯䝕䞊䝍䜰䜽䝉䝇
䕔 Connection 䝁䞁䝖䝻䞊䝹䛾㏣ຍ
䝒䞊䝹䝪䝑䜽䝇䛻䝁䞁䝖䝻䞊䝹䛜⾲♧䛥䜜䛯䜙䚸䝣䜷䞊䝮ୖ䛻䝗䝷䝑䜾䛧䛶㓄⨨䛧䜎䛩䚹䛣䜜䛻䜘䜚䚸
Connection 䜸䝤䝆䜵䜽䝖䛜⏕ᡂ䛥䜜䜛䛯䜑䚸New(new)䜻䞊䝽䞊䝗䜢グ㏙䛩䜛ᚲせ䛜䛺䛟䛺䜚䜎䛩䚹
㻌
䕔 ᥋⥆ᩥᏐิ䛾タᐃ
䝥䝻䝟䝔䜱䜴䜱䞁䝗䜴䛛䜙䚸䜴䜱䝄䞊䝗䜢౑⏝䛧䛶䚸ConnectionString 䝥䝻䝟䝔䜱䜢タᐃ䛩䜛䛣䛸䛜䛷
䛝䜎䛩䚹
㻌
䕔 ᥋⥆䞉ษ᩿
Connection 䝁䞁䝖䝻䞊䝹䛾 Open 䝯䝋䝑䝗 / Close 䝯䝋䝑䝗䜢ᐇ⾜䛧䜎䛩䚹
23