Part. Chapter 10 ActiveX Data Objects.NET(ADO.NET).NET, ADO.NET..NET ADO(ActiveX Data Objects). ADO. ADO,. ADO,.NET ADO.NET... ADO.NET ADO Connection DataReader Command DataAdapter DataSet DataView DataSet 330
.NET (FCL) System.Data ( )..NET ADO. ADO,. ADO.NET., ADO ADO.NET. ADO ADO.NET. 10 Chapter...,,. ADO.NET. System.Data.SqlClient : SQL Server 7.0 SQL Server 2000. System.Data.OleDb : SQL Server (OLE DB). OLE DB, Microsoft Access, Excel, dbase. System.Data.Odbc : ODBC, 331
ODBC ODBC (DSN). ODBC. System.Data.OracleClient :. SqlClient OleDb.,,,. (System.Data.SqlClient System.Data.OleDb)., System.Data.SqlClient Connection System.Data.SqlClient.SqlConnection, OleDb System.Data.OleDb.OleDbConnection.. (SQL Server OleDb ). ADO 10.1 ADO ADO.NET. 332
, ADO.NET ADO.,.,. Connection,, Open. Command ADO. (Select), (Insert), (Update), (Delete) SQL Command. ADO.NET Command Parameters,. DataReader. ADO ForwardOnly., DataReader. 10 Chapter DataAdapter ADO.NET, ADO. DataAdapter, DataSet. DataAdapter. DataAdapter InsertCommand, UpdateCommand, SelectCommand, DeleteCommand. DataAdapter. DataSet ADO. DataSet,. DataSet, extensible markeup language(xml), XML. DataSet DataAdapter, DataAdapter Connection Command, DataSet. 333
. ADO.NET... (,,, ),. ADO. ADO.NET DataAdapter DataSet. (,, ).,,... DataSet. (rule), (constraint), (relationship). XML ADO.NET. XML. XML ADO.NET. ADO. NET XML. ADO.NET XML 334
,. XML.NET,. Visual Studio.NET FCL., [ 10.2] ADO.NET DataReader, DataAdapter, DataSet 10 Chapter 335
[ 10.2]. Visual Basic 6 ASP.,..NET.., ( ).. DataSet DataAdapter. DataReader 11.,. ADO.NET Connection. SQL Server SqlConnection (System.Data.SqlClient ) Connection, OLE DB OleDbConnection (System.Data.OleDb ) Connection.,,, (connection string)., SQL Server.. 336
OleDbConnection Microsoft Access.NET SDK (MSDE) SQL Server ID SQL Server ' System.Data.OleDb.OleDbConnection ' Dim stroledb As String = _ "Provider=Microsoft.Jet.OLEDB.4.0; " _ & "Data Source=..\Northwind.mdb" Dim cnoledb As New OleDbConnection() cnoledb.connectionstring = stroledb cnoledb.open() 10 Chapter ' System.Data.SqlClient.SqlConnection ' MSDE SDK Dim strmsde As String = _ "Server=(local)\NetSDK;DataBase=Northwind; " _ & "Integrated Security=SSPI" Dim cnmsde As New SqlConnection() cnmsde.connectionstring = strmsde cnmsde.open() ' System.Data.SqlClient.SqlConnection ' SQL Server Dim strsql As String = _ "Server=localhost;DataBase=Northwind;" & _ "Integrated Security=SSPI" Dim cnsql As New SqlConnection() cnsql.connectionstring = strsql cnsql.open() ' ID ' System.Data.SqlClient.SqlConnection ' SQL Server Dim strsql1 As String = _ 337
"Server=localhost;DataBase=Northwind;" & _ "uid=sa;pwd=password" Dim cnsql1 As New SqlConnection() cnsql1.connectionstring = strsql1 cnsql1.open() // System.Data.OleDb.OleDbConnection // string stroledb; stroledb = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\Northwind.mdb" OleDbConnection cnoledb = new OleDbConnection(); cnoledb.connectionstring = stroledb; cnoledb.open(); // System.Data.SqlClient.SqlConnection // MSDE SDK string strmsde; strmsde = @"Server=(local)\NetSDK;DataBase=Northwind;Integrated Security=SSPI" SqlConnection cnmsde = new SqlConnection(); cnmsde.connectionstring = strmsde; cnmsde.open(); // System.Data.SqlClient.SqlConnection // SQL Server string strsql; strsql = @"Server=localhost;DataBase=Northwind;Integrated Security=SSPI" SqlConnection cnsql = new SqlConnection(); cnsql.connectionstring = strsql; cnsql.open(); // ID // System.Data.SqlClient.SqlConnection // SQL Server string strsql1; strsql1 = 338
@"Server=localhost;DataBase=Northwind;uid=sa;pwd="; SqlConnection cnsql1 = new SqlConnection(); cnsql1.connectionstring = strsql1; cnsql1.open();, OleDbConnection SqlConnection., SQL Server ( localhost),, ID. OleDbConnection, OLE DB, OLE DB. OLE DB. Access. Oracle SQL Server 6.5, SQL Server 7.0 SQL Server 2000. 10 Chapter [ 10.1] SqlConnection OleDbConnection. 339
SqlConnection OleDbConnection ( ) (overload), Connection., Visual Basic.NET (, ). Dim cn as New SqlConnection("Server=localhost;DataBase=Northwind;" _ & "uid=sa;pwd=password"),. 340
Command SQL, SQL SQL (stored procedure). SqlCommand Command SQL Server, OleDbComand Command OLE DB. Command. Connection CreateCommand SqlCommand OleDbCommand (Command ) Connection Command SQL Command. [ 10.2] SqlCommand OleDbCommand Command. 10 Chapter 341
Command SQL Command Text, [ 10.3] SQL. Command,. ExecuteReader, DataReader.. DataReader. SqlDataReader OleDbDataReader.,. [ 10.1]., Command, DataReader. 342
Dim cn As New SqlConnection( _ "Server=(local)\NetSDK;DataBase=pubs;" _ & "Integrated Security=SSPI") ' SqlDataReader. Dim dr As SqlDataReader ' SqlCommand Dim cmd As New SqlCommand ' CommandText SQL Select, ' Connection SqlConnection ( ) cn. With cmd.commandtext = "Select au_lname, au_fname from Authors".Connection = cn End With 10 Chapter ' Connection. cn.open() ' Command ExecuteReader. dr = cmd.executereader(commandbehavior.closeconnection) Dim strname As String ' DataReader Read. While dr.read ' ListBox1. strname = dr("au_lname") & ", " & dr("au_fname") ListBox1.Items.Add(strName) End While ' Connection. cn.close() SqlConnection cn = new SqlConnection (@"Server=(local)\NetSDK;DataBase=pubs;Integrated 343
Security=SSPI"); // SqlDataReader. SqlDataReader dr; // SqlCommand SqlCommand cmd = new SqlCommand(); // CommandText SQL Select, // Connection SqlConnection ( ) cn. cmd.commandtext = "Select au_lname, au_fname from Authors"; cmd.connection = cn; // Connection. cn.open(); // Command ExecuteReader. dr = cmd.executereader(commandbehavior.closeconnection); string strname; // DataReader Read. while (dr.read()) { // listbox1. strname = dr.getstring(0) + ", " + dr.getstring(1); listbox1.items.add(strname); } // Connection. cn.close(); ExecuteReader, DataReader Read.,. While. Read False, While. DataReader 344
.. Visual Basic.NET. Do Until dr.read = False '. Loop [ 10.1] Visual Basic.NET C#, DataReader Read. Visual Basic.NET, C# GetString., 0, 1.,. [ 10.4] SqlDataReader. 10 Chapter 345
SQL Server OLE DB.. Visual Studio.NET DataReader. (DataReader ). C#, GetString. Visual Basic.NET string., System.Convert.. Insert, Update, Delete SQL ExecuteNonQuery. [ 10.2]. Sub DoNonQuery() Dim cn As New SqlConnection( _ "Server=(local)\NetSDK;DataBase=pubs;" _ & "Integrated Security=SSPI") Dim cmd As New SqlCommand With cmd.commandtext = "Delete from Authors where au_lname = 'Smith ".Connection = cn.commandtype = CommandType.Text End With Try cn.open() cmd.executenonquery() Catch ex As Exception MessageBox.Show(ex.Message) Finally 346
If cn.state = ConnectionState.Open Then cn.close() End If End Try End Sub private void DoNonQuery() { SqlConnection cn = new SqlConnection (@"Server=(local)\NetSDK;DataBase=pubs;Integrated Security=SSPI"); SqlCommand cmd = new SqlCommand(); cmd.commandtext = "Delete from Authors where au_lname = 'Smith " cmd.connection = cn; cmd.commandtype = CommandType.Text ; 10 Chapter } try { cn.open(); cmd.executenonquery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (cn.state == ConnectionState.Open) { cn.close(); } } [ 10.2] [ 10.1]. Connection Command, SQL., SQL. Insert, Update, Delete SQL,, 347
. ExecuteNonQuery. ExecuteReader. Connection State, connection close. Try ~ Catch, connection., connection close Finally close. Connection, Command, DataReader.., C# Visual Basic.NET. DataAccess_vb(Visual Basic.NET) DataAccess_cs(C#). DataAccess. Form1 ([ 10.3] ). 1. TabControl.. Dock : Fill TabPages: TabPage. TabPage. Text ( TabPage ). TabPage1 Text: Readers and Adapters TabPage2 Text: DataGrid Binding TabPage3 Text: Simple Data Entry TabPage. 2. Form1 TabPage1 Button.. 348
Button1: Text: Using a DataReader Name: UseDataReader Button2: Text: Using a DataSet Name: UseDataSet Button3: Text: Show Checked Items Name: ShowCheckedItems Button4: Text: DataBind with DataSet Name: DataBindWithDataSet 3. ListBox TabPage1. 4. CheckedListBox TabPage1. 5. ComboBox TabPage1. 10 Chapter 6. 3 Label TabPage1. [ 10.3]. (TabPage2, TabPage3).,,. [ 10.3] [ 10.1].,. 349
,. TabPage1, UseDataReader_Click. [ 10.3] ( )., Form1 Imports using.. Visual Basic.NET,. Imports System.Data.SqlClient Imports System.Text C#,. using System.Data.SqlClient; using System.Text; Private Sub UseDataReader_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles UseDataReader.Click ListBox1.Items.Clear() Dim cn As New SqlConnection( _ "Server=(local)\NetSDK;DataBase=pubs;" _ & "Integrated Security=SSPI") ' SqlDataReader. Dim dr As SqlDataReader ' SqlCommand. Dim cmd As New SqlCommand() ' CommandText SQL Select, ' Connection SqlConnection. With cmd.commandtext = "Select au_lname, au_fname from Authors".Connection = cn End With 350
' Connection open. cn.open() ' Command ExecuteReader. dr = cmd.executereader(commandbehavior.closeconnection) Dim strname As String ' DataReader Read ListBox1. While dr.read strname = dr("au_lname") & ", " & dr("au_fname") ListBox1.Items.Add(strName) End While End Sub private void UseDataReader_Click(object sender, System.EventArgs e) { 10 Chapter listbox1.items.clear(); SqlConnection cn = newsqlconnection(@"server=(local)\netsdk;database=pubs; Integrated Security=SSPI"); // SqlDataReader. SqlDataReader dr; // SqlCommand. SqlCommand cmd = new SqlCommand(); // CommandText SQL Select, // Connection SqlConnection. cmd.commandtext = "Select au_lname, au_fname from Authors"; cmd.connection = cn; // Connection open. cn.open(); // Command ExecuteReader. 351
dr = cmd.executereader(commandbehavior.closeconnection); string strname; // DataReader Read ListBox1. while (dr.read()) { strname = dr.getstring(0) + ", " + dr.getstring(1); listbox1.items.add(strname); } } ListBox1 SelectedIndexChanged. Button Click, ListBox, ListBox1, SelectedIndexChanged. [ 10.4] ( ). Private Sub ListBox1_SelectedIndexChanged _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ListBox1.SelectedIndexChanged ' ListBox1 Label1. Label1.Text = "ListBox1_SelectedIndexChanged" ' ListBox1 Label2. Label2.Text = ListBox1.Items(ListBox1.SelectedIndex) ' ListBox1 Label3. Label3.Text = ListBox1.SelectedIndex End Sub private void listbox1_selectedindexchanged(object sender, 352
System.EventArgs e) { // listbox1 Label1. label1.text = "ListBox1_SelectedIndexChanged"; // listbox1 Label2. label2.text = listbox1.text; } // listbox1 Label3. label3.text =listbox1.selectedindex.tostring(); [ 10.4] ListBox, ListBox.. 10 Chapter,. F5. Form1,. ListBox. ListBox Label. [ 10.4]. 353
DataReader.,,. DataReader, DataSet. DataSet. [ 10.2] DataSet. DataSet. DataTable. DataSet DataAdapter., DataSet DataAdapter. DataAdapter, Fill Update. Command SQL (Select ) Fill DataSet DataTable. DataAdapter Command,. Command SQL. SQL Server SqlDataAdapter, OleDb OleDbDataAdapter.. SqlDataAdapter, Command, DataSet Authors. cn Connection. ' SqlDataAdapter. Dim da As SqlDataAdapter = New SqlDataAdapter() ' Connection Command. Dim cmd As New SqlCommand("Select * from Authors", cn) 354
' SelectCommand Command. da.selectcommand = cmd ' SqlDataAdapter DataSet. Dim ds As DataSet = New DataSet() ' Command DataSet Fill method. da.fill(ds, "Authors") // SqlDataAdapter. SqlDataAdapter da = new SqlDataAdapter(); // Connection Command. SqlCommand cmd = new SqlCommand("Select * from Authors", cn); // SelectCommand Command. da.selectcommand = cmd; 10 Chapter // SqlDataAdapter DataSet. DataSet ds = new DataSet(); // Command DataSet Fill method. da.fill(ds, "Authors"); DataAdapter Fill., Command SQL (Select ) (row) DataSet. connection open open open. close. 11, DataAdapter Update DataSet Insert, Update, Delete. DataAdapter, InsertCommand, UpdateCommand, DeleteCommand. SQL,., (batch). 355
DataAdapter, DataAdapter SQL. Visual Basic.NET, SqlDataAdapter SQL (Select) Connection. Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from Customers", cn ) DataSet DataAdapter. DataAccess.. DataAdapter, SQL Server CheckedListBox1. Form1 [ 10.5] UseDataSet_Click ( ). Private Sub UseDataSet_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles UseDataSet.Click ' CheckedListBox. CheckedListBox1.Items.Clear() 356
' Connection. Dim cn As New SqlConnection( _ "Server=(local)\NetSDK;DataBase=pubs;" _ & "Integrated Security=SSPI") ' SqlDataAdapter. ' SQL Connection. Dim da As SqlDataAdapter = New SqlDataAdapter _ ("SELECT au_id, au_lname + ', ' + au_fname As FullName FROM Authors", cn) ' DataSet. Dim ds As DataSet = New DataSet("Authors") da.fill(ds, "Authors") ' DataRow. Dim dr As DataRow 10 Chapter ' DataSet ' CheckedListBox. For Each dr In ds.tables("authors").rows CheckedListBox1.Items.Add(dr("FullName")) Next End Sub private void UseDataSet_Click(object sender, System.EventArgs e) { // checkedlistbox. checkedlistbox1.items.clear(); // Connection. SqlConnection cn = new SqlConnection(@"Server=(local)\NetSDK;DataBase=pubs; Integrated Security=SSPI"); // SqlDataAdapter. // SQL Connection. SqlDataAdapter da = new SqlDataAdapter("SELECT au_id, au_lname + ', ' + au_fname 357
As FullName FROM Authors", cn); // DataSet SqlDataAdapter. DataSet ds = new DataSet("Authors"); da.fill(ds, "Authors"); } // DataSet // CheckedListBox. foreach (DataRow dr in ds.tables["authors"].rows) { checkedlistbox1.items.add(dr["fullname"]); } CheckedListBox1 SelectedIndexChanged. ListBox1_SelectedIndexChanged,. CheckedListBox GetItemChecked., Form1 CheckedListBox1, SelectedIndex Changed [ 10.6] ( ). Private Sub CheckedListBox1_SelectedIndexChanged _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ HandlesCheckedListBox1.SelectedIndexChanged ' CheckedListBox1 Label1. Label1.Text = "CheckedListBox1_SelectedIndexChanged" ' CheckedListBox1 Label2. Label2.Text = CheckedListBox1.Items(CheckedListBox1.SelectedIndex) ' CheckedListBox1. Label3.Text = "Checked = " & CheckedListBox1.GetItemChecked _ (CheckedListBox1.SelectedIndex) 358
End Sub private void checkedlistbox1_selectedindexchanged(object sender, System.EventArgs e) { // checkedlistbox1 Label1. label1.text = "CheckedListBox1_SelectedIndexChanged"; // checkedlistbox1 Label2. label2.text = checkedlistbox1.text; // checkedlistbox1. label3.text = "Checked = " + checkedlistbox1.getitemchecked(checkedlistbox1.selectedindex); } 10 Chapter F5 [ 10.5]. [ 10.5] UseDataSet_Click SqlDataAdapter. SQL Select Connection. DataSet, SqlDataAdapter Fill ( ). Fill DataTable, 359
Authors. DataTable, ( ) DataSet. DataSet DataTable., DataSet, DataTable. DataSet DataTable,. (For ~ Each ) DataTable DataRow (DataRow ). DataRow. [ 10.5] FullName CheckedListBox., DataSet. DataSet DataTable. DataTable SQL, 16,777,216 (row) ( ). DataRow DataTable, DataTable. DataSet DataTable DataRow. [ 10.6]. 360
DataSet.. Visual Basic 6. Visual Studio.NET. Visual Basic 6. 10 Chapter,.,, XML,.,.,,.,.. (DisplayMember ValueMember ) 11 Visual Studio.NET Visual Studio.NET. ADO.NET ComboBox DataGrid DataSet. 361
, (simple) (complex). DataSet, DataSource DataSet.., List Grid. TextBox. DataBindings (Control ), Add DataSet. ' Connection. Dim cn As New SqlConnection( _ "Server=(local)\NetSDK;DataBase=northwind;" _ & "Integrated Security=SSPI") ' SqlDataAdapter. ' SQL Connection. Dim da As SqlDataAdapter = New SqlDataAdapter _ ("SELECT * from Customers", cn) ' DataSet. Dim ds As DataSet = New DataSet("Customers") ' DataSet Fill. da.fill(ds, "Customers") ' TextBox DataBinding. TextBox2.DataBindings.Add("Text", ds.tables("customers"), "CompanyName") TextBox3.DataBindings.Add("Text", ds.tables("customers"), "Address") TextBox4.DataBindings.Add("Text", ds.tables("customers"), "City") 362
// Connection. SqlConnection cn = new SqlConnection(@"Server=(local)\NetSDK;DataBase=northwind;Integrate d Security=SSPI"); // SqlDataAdapter. // SQL Connection. SqlDataAdapter adp = new SqlDataAdapter("SELECT * from Customers", cn); // DataSet. DataSet ds = new DataSet("Customers"); // DataSet Fill. adp.fill(ds, "Customers"); 10 Chapter // TextBox DataBinding. textbox2.databindings.add("text", ds.tables["customers"], "CompanyName"); textbox3.databindings.add("text", ds.tables["customers"], "Address"); textbox4.databindings.add("text", ds.tables["customers"], "City"); DataAccess, ComboBox DataGrid. ComboBox DisplayMember ValueMember. DisplayMember ComboBox (DataSet ), ValueMember ( primary ). Authors primary au_id, ValueMember au_id.,. Form1, BindWithDataSet_Click [ 10.7] ( ). 363
Private Sub BindWithDataSet_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles BindWithDataSet.Click Dimcn As New SqlConnection( _ "Server=(local)\NetSDK;DataBase=pubs;" _ & "Integrated Security=SSPI") Dimda As SqlDataAdapter = New SqlDataAdapter _ ("SELECT au_id, au_lname + ', ' + au_fname As FullName FROM Authors", cn) Dimds As DataSet = New DataSet("Authors") da.fill(ds, "Authors") With ComboBox1 ' ComboBox DataSource ' DataSet Authors DataTable..DataSource = ds.tables("authors") ' ComboBox DisplayMember DataSet FullName..DisplayMember = "FullName" ' ComboBox ValueMember au_id (DataTable primary )..ValueMember = "au_id" End With End Sub private void BindWithDataSet_Click(object sender, System.EventArgs e) { combobox1.items.clear(); SqlConnection cn = 364
new SqlConnection(@"Server=(local)\NetSDK;DataBase=pubs;Integrated Security=SSPI"); SqlDataAdapter da = new SqlDataAdapter("SELECT au_id, au_lname + ', ' + au_fname As FullName FROM Authors", cn); DataSet ds = new DataSet("Authors"); da.fill(ds, "Authors"); // combobox DataSource // DataSet Authors DataTable. combobox1.datasource = ds.tables["authors"]; // combobox DisplayMember DataSet FullName. combobox1.displaymember = "FullName"; 10 Chapter } // combobox ValueMember au_id (DataTable primary ). combobox1.valuemember = "au_id"; [ 10.5] [ 10.6] ListBox, ListBox Label. DataSet. [ 10.7] (ComboBox).,. ComboBox? [ 10.8]. ComboBox1 SelectedIndexChanged Label. ComboBox1 ( ). Visual Basic.NET C#., ComboBox. 365
Private Sub ComboBox1_SelectedIndexChanged( _ ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged ' ComboBox1 Label1. Label1.Text = "ComboBox1_SelectedIndexChanged DataBound" ' ComboBox1 Label2. Label2.Text = ComboBox1.Text ' ComboBox1. Dimitm As DataRowView itm = ComboBox1.SelectedItem Label3.Text = CType(itm("au_id"), String) End Sub F5, [ 10.7]. 366
, DisplayMember Authors DataTable FullName ComboBox1. ComboBox1 SelectedIndexChanged ValueMember au_id. DataAccess, DataGrid DataSet., DataGrid ComboBox TabPage2.. 1., (DataGrid Binding) TabPage2. 10 Chapter 2. ComboBox TabPage2. 3. DataGrid TabPage2. [ 10.8]. DataSet DataGrid. ComboBox 3. 367
DataGrid Binding DataGrid, TabControl1 TabIndexChanged. TabControl1, TabControl1_TabIndexChanged [ 10.9] ( ). TabControl. Northwind Orders DataGrid. Private Sub TabControl1_TabIndexChanged _ (ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles TabControl1.SelectedIndexChanged '. ', DataGrid. ' 0 1. IfTabControl1.SelectedTab.TabIndex = 1 Then ' Connection. Dim cn As New SqlConnection( _ "Server=(local)\NetSDK;DataBase=northwind;" _ & "Integrated Security=SSPI") ' SqlDataAdapter. ' SQL Connection. Dim da As SqlDataAdapter = New SqlDataAdapter _ ("SELECT * from Orders", cn) ' SqlDataAdapter ' DataSet. Dim ds As DataSet = New DataSet("Orders") ' Fill DataSet. da.fill(ds) ' DataSet 368
' DataGrid DataSource. DataGrid1.DataSource = dv End If End Sub private void tabcontrol1_tabindexchanged(object sendert, System.EventArgs e) { //. //, DataGrid. if(tabcontrol1.selectedtab.text == "DataGrid Binding") { // Connection. SqlConnection cn = new SqlConnection(@"Server=(local)\NetSDK;DataBase=northwind; Integrated Security=SSPI"); 10 Chapter // SqlDataAdapter. // SQL Connection. SqlDataAdapter da = new SqlDataAdapter("SELECT * from Orders", cn); // SqlDataAdapter // DataSet. DataSet ds = new DataSet("Orders"); // Fill DataSet. da.fill(ds); // DataSet // DataGrid DataSource. datagrid1.datasource = ds; } } 369
SqlDataAdapter. Connection Command, DataAdapter Fill. [ 10.9], DataGrid DataSource DataSet. F5. [ 10.9]., DataGrid SQL Server.,.,. Summary ADO.NET. FCL,. ADO.NET.,. ADO.NET,., DataReader. 370
DataSet DataAdapter. 11 Visual Studio.NET ADO.NET. Q & A Q A DataReader., IList IListSource (System.ComponentModel ). DataReader unbuffered,.,. 10 Chapter Q A,.. connection open,, close. DataReader..NET DataReader. DataSet.. Select, Insert, Update, Delete SQL. SQL. 371
Qui z 1. DataReader DataAdapter SQL ADO.NET? 2. DataReader.? 3. SqlDataAdapter OleDataAdapter.,? 4. DataAdapter Connection.,? 5..NET Label, DataGrid, TextBox DataSet XML.,? 6.? 1. 2. 3. 4. 5. 6. Exercises 1. DataAccess TabPage1 ShowCheckedItems.., CheckedListBox 372
. GetItemChecked. Visual Studio.NET (SDK ). 2. DataAccess TabPage2 ComboBox. ComboBox. Northwind Customers TabPage2 ComboBox2 DataGrid CustomerID ComboBox2 CustomerID DataGrid., Form1 DataView dv. Public Class Form1 Inherits System.Windows.Forms.Form Private dv As DataView 10 Chapter public class Form1 : System.Windows.Forms.Form { private DataView dv; DataView Form1, Form1. Load_Customers. BindWithDataSet_ Click.,., Pubs Authors Northwind Customers. Load_Customers. Northwind Connection. Customers SQL Select SqlDataAdapter. SqlDataAdapter Fill, DataSet DataTable Customers. 373
ComboBox2 DataSet. ComboBox2 ValueMember CustomerID. ComboBox2 DisplayMember CustomerID. ComboBox2 SelectedIndexChanged, DataView RowFilter DataView, DataView DataGrid. SelectedIndexChanged. Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged ' DataView RowFilter. dv.rowfilter = "CustomerID Like '" & ComboBox2.Text & "% " ' DataGrid DataView. DataGrid1.DataSource = dv End Sub private void combobox2_selectedindexchanged(object sender, System.EventArgs e) { // DataView RowFilter. dv.rowfilter = "CustomerID Like '" + combobox2.text + "% "; } // DataGrid DataView. datagrid1.datasource = dv;, TabControl1_TabIndexChanged., DataGrid DataSet DataView.. 374
dv = New DataView(ds.Tables(0), "", "", DataViewRowState.OriginalRows) ' DataSet ' DataGrid DataSource. DataGrid1.DataSource = dv SDK DataView., DataView DataSet. [ 10.10]. 10 Chapter 375