- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
s INTRODUCING DATASETS AND DATA ADAPTERS in VB.NET
CHAPTER 8 s INTRODUCING DATASETS AND DATA ADAPTERS Generating DataMatrix In Visual Basic .NET Using Barcode generator for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications. www.OnBarcode.comData Matrix ECC200 Reader In Visual Basic .NET Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comcmd.Parameters.Add( _ "@country", _ SqlDbType.NVarChar, _ 15, _ "country") Linear Barcode Generation In VB.NET Using Barcode maker for VS .NET Control to generate, create Linear 1D Barcode image in .NET framework applications. www.OnBarcode.comGenerating QR Code JIS X 0510 In Visual Basic .NET Using Barcode drawer for Visual Studio .NET Control to generate, create QR Code 2d barcode image in VS .NET applications. www.OnBarcode.comFinally, you set the data adapter s InsertCommand property with the command to insert into the Employees table so it will be the SQL the data adapter executes when you call its Update method. You then called Update on the data adapter to propagate the change to the database. Here you added only one row, but since the SQL is parameterized, the data adapter looked for all new rows in the employees data table and submitted inserts for all of them to the database. Encode UCC - 12 In VB.NET Using Barcode maker for .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications. www.OnBarcode.comPDF-417 2d Barcode Encoder In VB.NET Using Barcode creator for .NET Control to generate, create PDF 417 image in .NET framework applications. www.OnBarcode.com' Update database da.InsertCommand = cmd da.Update(ds, "employees") Printing 2D Barcode In Visual Basic .NET Using Barcode encoder for Visual Studio .NET Control to generate, create 2D Barcode image in Visual Studio .NET applications. www.OnBarcode.comCreating 2 Of 5 Interleaved In VB.NET Using Barcode generation for Visual Studio .NET Control to generate, create ITF image in VS .NET applications. www.OnBarcode.comFigure 8-8 shows the new row, and if you if you check with Database Explorer or the SSMSE, you ll see the row has been propagated to the database. Roy Beatty is now in the Employees table. DataMatrix Generator In Objective-C Using Barcode generation for iPhone Control to generate, create DataMatrix image in iPhone applications. www.OnBarcode.comData Matrix Creation In C# Using Barcode creation for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET applications. www.OnBarcode.comDeleteCommand Property
Paint Code 128 Code Set B In None Using Barcode maker for Microsoft Word Control to generate, create Code 128 Code Set A image in Word applications. www.OnBarcode.comGS1 - 13 Generation In None Using Barcode generation for Software Control to generate, create EAN 13 image in Software applications. www.OnBarcode.comYou use the DeleteCommand property to execute SQL DELETE statements.
Draw Barcode In Objective-C Using Barcode drawer for iPhone Control to generate, create Barcode image in iPhone applications. www.OnBarcode.comData Matrix Printer In None Using Barcode encoder for Microsoft Excel Control to generate, create Data Matrix ECC200 image in Excel applications. www.OnBarcode.comTry It Out: Propagating New Dataset Rows to a Data Source Let s again modify Listing 8-4 to delete a row from the database. 1. Add a new Console Application project named PersistDeletes to your 08 solution. 2. Replace the code in Module1.vb with the code in Listing 8-7. Barcode Printer In Java Using Barcode generator for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comBarcode Decoder In Java Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications. www.OnBarcode.comListing 8-7. PersistDeletes: Module1.vb
Create GS1 DataBar Truncated In Java Using Barcode creator for Java Control to generate, create DataBar image in Java applications. www.OnBarcode.comMake QR Code In Java Using Barcode drawer for Java Control to generate, create QR Code image in Java applications. www.OnBarcode.comImports System Imports System.Data Imports System.Data.SqlClient Module Module1
Barcode Drawer In None Using Barcode maker for Software Control to generate, create Barcode image in Software applications. www.OnBarcode.comGS1 128 Generation In Visual Studio .NET Using Barcode encoder for Reporting Service Control to generate, create UCC - 12 image in Reporting Service applications. www.OnBarcode.comCHAPTER 8 s INTRODUCING DATASETS AND DATA ADAPTERS
Sub Main() ' Set up connection string Dim connString As String = _ "server = .\sqlexpress;" _ & "integrated security = true;" _ & "database = northwind" ' Set up query Dim qry As String = _ "select " _ & " * " _ & "from " _ & " employees " _ & "where " _ & " country = 'UK' " ' Set up DML Dim del As String = _ "delete from employees " _ & "where " _ & " employeeid = @employeeid " ' Create connection Dim conn As SqlConnection = New SqlConnection(connString) Try ' Create data adapter Dim da As SqlDataAdapter = New SqlDataAdapter(qry, conn) ' Create and fill dataset Dim ds As DataSet = New DataSet() da.Fill(ds, "employees") ' Get the data table reference Dim dt As DataTable = ds.Tables("employees") ' Delete Employees ' ' Create command Dim cmd As SqlCommand = New SqlCommand(del, conn) CHAPTER 8 s INTRODUCING DATASETS AND DATA ADAPTERS
' ' Map parameters ' cmd.Parameters.Add( _ "@employeeid", _ SqlDbType.Int, _ 4, _ "employeeid") ' ' ' Select employee to delete Dim filt As String = _ "firstname = 'Roy' " _ & "and " _ & "lastname = 'Beatty' " ' ' Delete employee from data table For Each row As DataRow In dt.Select(filt) row.Delete() Next ' ' Update database da.DeleteCommand = cmd da.Update(ds, "employees") ' Display data table For Each row As DataRow In dt.Rows Console.WriteLine( _ "{0} {1} {2}", _ row("firstname").ToString().PadRight(15), _ row("lastname").ToString().PadLeft(25), _ row("city") _ ) Next Catch e As Exception ' Display error Console.WriteLine("Error: " & e.ToString) Finally ' Close connection conn.Close() End Try End Sub End Module CHAPTER 8 s INTRODUCING DATASETS AND DATA ADAPTERS
3. Make this the startup project, and run it with Ctrl+F5. You should see the output in Figure 8-9. Figure 8-9. Deleting a row
How It Works You added a DELETE statement (and changed the name of the original query string variable from sql to del in order to clearly distinguish it from this statement): ' Set up DML Dim del As String = _ "delete from employees " _ & "where " _ & " employeeid = @employeeid " You inserted the delete code ahead of the display, after creating a command and mapping a parameter: ' Delete Employees ' ' Create command Dim cmd As SqlCommand = New SqlCommand(del, conn) ' ' Map parameters ' cmd.Parameters.Add( _ "@employeeid", _ SqlDbType.Int, _ 4, _ "employeeid") You selected the row to delete and deleted it. Actually, you selected all rows for employees named Roy Beatty, since you don t know (or care about) their employee IDs. Although you expect only one row to be selected, you use a loop to delete all the rows. (If you ran the PropagateInserts program multiple times, you d have more than one row that matches this selection criteria.)
|
|