+92 332 4229 857 99ProjectIdeas@Gmail.com

How to update data in table (LINQ To SQL):

How to update data in table (LINQ To SQL)
The following code snippet shows how you can delete data row from the table and also in database. For example there is a table in your database with the name Customer, which contains the following columns in it:
ID, Name, Address

This is the connection string which let you connect to the server:
               Data Source = "Your Server Name"
               Initial Catalog = "The name of your database"
               Integrated Security = "true" for getting your credentials automatic to your current login account in windows, in this way you don't have to use user id and password while establishing connection with SQL server.

public static string conString = @"Data Source =  MOAVIA_OSAMA-PC\SQLEXPRESS;Initial Catalog = MyFirst ; Integrated Security = true";
    The following is the class generated when you add "Linq to SQL classes" to project, which takes an argument of connection string:

DataClasses1DataContext dc = new DataClasses1DataContext(conString);

Now, you want to update row of data in table, first you have to search the data which you want to update, so the following code search the Customer against the ID = 2:

  Customer query =
         (from Customer c in dc.Customers
          where c.ID == 2
          select c).Single();

Now, you want to update the Name and Address of the searched Customer so you should do the following:

  query.Name = "Osama";
  query.Address = "Islamabad";

The following statement saves in database table:



Miroslav said...

where can i find
DataClasses1DataContext Class

Admin said...

when you Add "Linq To SQL class" from the item menu there is mentioned below,
you can also change it according to your desire