+ All Categories
Home > Documents > Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will...

Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will...

Date post: 03-Aug-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
8
30.04.2018 1
Transcript
Page 1: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

1

DATA UPDATING AND DELETING WITH

VISUAL C#.NET

Ayşe Şadiye Özen

2013514054

GENERAL INFORMATION

In 7th chapter from Ying Bai Practical Database

book, we will learn about update and delete

data in the database.

There are 2 method for this application.

1. Table Adapter Manager

2. With runtime objects(ExecuteNonQuery())

TABLE ADAPTER MANAGER

TableAdapter is utilized to perform a data query and all data tables are embedded into the DataSet as a data - catching unit, the Command object is embedded into the different data query methods of the TableAdapter, such as

SelectCommand,

InsertCommand,

UpdateCommand,

DeleteCommand, and is executed based on the associated query type.

TABLEADAPTER.INSERT

Adds new records into a database allowing you

to pass in individual column values as method

parameters.

TABLEADAPTER.UPDATE

Updates existing records in a database. The Update method takes original and new column values as method parameters. The original values are used to locate the original record, and the new values are used to update that record. The TableAdapter.Update method is also used to reconcile changes in a data set back to the database by taking a DataSet, DataTable, DataRow, or array of DataRows asmethod parameters.

TABLEADAPTER.DELETE

Deletes existing records from the database

based on the original column values passed in

as method parameters.

Page 2: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

2

Let’s take an example about Table Adapter.

Page 3: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

3

Page 4: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

4

4 Textbox

4 Button

WITH RUN TIME OBJECTS

In this part, we will use SQL and LINQ and

discuss how to update and delete data in the

databases.

SQL

We known, SQL is a standardized programming

language used for managing relational

databases and performing various operations

on the data in them.

We can update and delete operations with

ExecuteNonQuery() method.

EXECUTENONQUERY()

The ExecuteNonQuery() is one of the most frequently used method in SqlCommand Object and is used for executing statements that do not return result sets (ie. statements like insert data, update data etc.) .

Command.ExecuteNonQuery(): The ExecuteNonQuery() performs Data Definition tasks as well as Data Manipulation tasks also. The Data Definition tasks like creating Stored Procedures,Views etc. perform by the

ExecuteNonQuery() . Also Data Manipulation tasks like Insert, Update, Delete etc. also perform by the ExecuteNonQuery() of SqlCommand Object.

Page 5: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

5

Firstly, we will create a database at SQL Server.

4 Button

1 DataGridView

6 Label

6 Textbox

Page 6: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

6

LINQ

We known, LINQ is a Microsoft programming

model and methodology that essentially adds

formal query capabilities into Microsoft.

We can apply update and delete operations.

UPDATING EXISTING DATA

1. Retrieve the object to be changed from the DataContext

Use LINQ to retrieve the category you want to update from your DataContext.

LINQ's Single method provides an easy way to do this by taking a lambda expression that returns a single object. In this case, the category (c) whose Name == "Programming Practices":

BookCatalog bookCatalog = new BookCatalog( ); Category category = bookCatalog.Categories.Single( c => c.Name == "Programming Practices" );

2. Update the object

category.Name = "Technical Practices";

3. Submit changes to the DataContext

bookCatalog.SubmitChanges( );

DELETING DATA

Similarly, you can remove records from the database by removing them from the appropriate Table collection in your DataContext.

1. Retrieve object to be changed from the DataContext

Retrieve the object you want to delete from your DataContext:

BookCatalog bookCatalog = new BookCatalog( ); Category category = bookCatalog.Categories.Single( c => c.Name == "Java" );

2. Remove the object from the appropriate Table collection in your DataContext

This is done with the DeleteOnSubmit() method on your Table collection:

bookCatalog.Categories.DeleteOnSubmit( category );

3. Submit changes to the DataContext

The data will have been removed locally from your DataContext, but will not be deleted from the database until you call SubmitChanges().

bookCatalog.SubmitChanges( );

Firstly, we will create a new sql database.

6 Label

4 Button

6 Textbox

1 DataGridView

Page 7: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

7

And then, we should add LINQ class our

project.

We should catch and drag our table. Then, we should see this.

Page 8: Data Updating and Deleting with Visual C# · 3. Submit changes to the DataContext The data will have been removed locally from your DataContext, but will not be deleted from the database

30.04.2018

8

REFERENCES

https://searchsqlserver.techtarget.com/definition/SQL

https://www.codeproject.com/Articles/46422/A-LINQ-Tutorial-Adding-Updating-Deleting-Data

https://www.youtube.com/watch?v=fur4nsLa5VU

https://www.ahmetcansever.com/sql-server-2/c-telefon-rehberi-linq-to-sql-classes-select-insert-update-delete/

https://docs.microsoft.com/tr-tr/visualstudio/data-tools/fill-datasets-by-using-tableadapters

Practical Database Programming With Visual C#.NET, by Ying Bai


Recommended