Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
3__linq_hands_on_lab.docx
Скачиваний:
1
Добавлен:
01.05.2025
Размер:
171 Кб
Скачать
☆

Task 6 – Modifying Database Data

In this task, you move beyond data retrieval and see how to manipulate the data. The four basic data operations are Create, Retrieve, Update, and Delete, collectively referred to as CRUD. You see how LINQ to SQL makes CRUD operations simple and intuitive. This task shows how to use the create and update operations.

  1. Create a new method that modifies the database data as well as a call from Main:

static void ModifyData()

{

var db = new NorthwindDataContext();

Console.WriteLine("Number Created Before: {0}",

db.Customers.Where( c => c.CustomerID == "ADVCA" ).Count());

var newCustomer = new Customer

{

CompanyName = "AdventureWorks Cafe",

CustomerID = "ADVCA"

};

db.Customers.InsertOnSubmit(newCustomer);

db.SubmitChanges();

Console.WriteLine("Number Created After: {0}",

db.Customers.Where( c => c.CustomerID == "ADVCA" ).Count());

}

static void Main(string[] args)

{

ModifyData();

}

  1. Press Ctrl+F5 to build and run the application. Notice that the two lines written to the screen are different after the database is updated. Now press any key to terminate the application.

Notice that after the Add method is called, the changes are then submitted to the database using the SubmitChanges method. Note that once the customer has been inserted, it cannot be inserted again due to the primary key uniqueness constraint. Therefore this program can only be run once.

  1. Now update and modify data in the database. Change ModifyData to the following code that updates the contact name for the first customer retrieved.

static void ModifyData()

{

var db = new NorthwindDataContext();

Console.WriteLine("Number Updated Before: {0}",

db.Customers.Where( c => c.ContactName == "New Contact" ).Count());

var existingCustomer = db.Customers.First();

existingCustomer.ContactName = "New Contact";

db.SubmitChanges();

Console.WriteLine("Number Updated After: {0}",

db.Customers.Where( c => c.ContactName == "New Contact" ).Count());

}

  1. Now press Ctrl+F5 to build and run the application. Notice again the number of contacts with the name “New Contact” changes. Press any key to terminate the application.

Task 7 – Calling Stored Procedures

Recall that when using the designer, you added a stored procedure along with the tables. In this task you will call tha stored procedure.

  1. Create a new method that prints the results of a call to the Ten Most Expensive Products stored procedure that was added to the NorthwindDataContext in the designer:

static void InvokeSproc()

{

var db = new NorthwindDataContext();

foreach (var r in db.Ten_Most_Expensive_Products())

Console.WriteLine(r.TenMostExpensiveProducts + "\t" + r.UnitPrice);

}

  1. Now call this method from the Main method:

static void Main(string[] args)

{

InvokeSproc();

}

  1. Press Ctrl+F5 to build and run the application. After viewing the results, press any key to terminate the application.

When the database cannot be accessed through dynamic SQL statements, you can use C# 3.0 and LINQ to run stored procedures to access the data.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]