private void Form1_Load(object sender, System.EventArgs e) { System.Data.SqlClient.SqlCommand mySqlCommand = sqlConnection1.CreateCommand(); mySqlCommand.CommandText = "SELECT CustomerID, CompanyName, Address " + "FROM Customers " + "WHERE CustomerID = 'ALFKI'"; System.Data.SqlClient.SqlDataAdapter mySqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter(); mySqlDataAdapter.SelectCommand = mySqlCommand; MyDataSet myDataSet = new MyDataSet(); sqlConnection1.Open(); mySqlDataAdapter.Fill(myDataSet, "Customers"); sqlConnection1.Close(); MyDataSet.CustomersDataTable myDataTable = myDataSet.Customers; foreach (MyDataSet.CustomersRow myDataRow in myDataTable.Rows) { listView1.Items.Add(myDataRow.CustomerID); listView1.Items.Add(myDataRow.CompanyName); listView1.Items.Add(myDataRow.Address); } }
private void Form1_Load(object sender, System.EventArgs e) { // populate the DataSet with the CustomerID, CompanyName, // and Address columns from the Customers table sqlConnection1.Open(); sqlDataAdapter1.Fill(myDataSet1, "Customers"); // get the Customers DataTable MyDataSet.CustomersDataTable myDataTable = myDataSet1.Customers; // create a new DataRow in myDataTable using the // NewCustomersRow() method of myDataTable MyDataSet.CustomersRow myDataRow = myDataTable.NewCustomersRow(); // set the CustomerID, CompanyName, and Address // of myDataRow myDataRow.CustomerID = "J5COM"; myDataRow.CompanyName = "J5 Company"; myDataRow.Address = "1 Main Street"; // add the new row to myDataTable using the // AddCustomersRow() method myDataTable.AddCustomersRow(myDataRow); // push the new row to the database using // the Update() method of sqlDataAdapter1 sqlDataAdapter1.Update(myDataTable); // find the row using the FindByCustomerID() // method of myDataTable myDataRow = myDataTable.FindByCustomerID("J5COM"); // modify the CompanyName and Address of myDataRow myDataRow.CompanyName = "Widgets Inc."; myDataRow.Address = "1 Any Street"; // push the modification to the database sqlDataAdapter1.Update(myDataTable); // display the DataRow objects in myDataTable // in the listView1 object foreach (MyDataSet.CustomersRow myDataRow2 in myDataTable.Rows) { listView1.Items.Add(myDataRow2.CustomerID); listView1.Items.Add(myDataRow2.CompanyName); // if the Address is null, set Address to "Unknown" if (myDataRow2.IsAddressNull() == true) { myDataRow2.Address = "Unknown"; } listView1.Items.Add(myDataRow2.Address); } // find and remove the new row using the // FindByCustomerID() and RemoveCustomersRow() methods // of myDataTable myDataRow = myDataTable.FindByCustomerID("J5COM"); myDataTable.RemoveCustomersRow(myDataRow); // push the delete to the database sqlDataAdapter1.Update(myDataTable); sqlConnection1.Close(); }