[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

How to insert a record through LINQ.

Table name t
field empid, empname
How to insert the record through LINQ.
0
searchsanjaysharma
Asked:
searchsanjaysharma
2 Solutions
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Hi, here's some samples for you. There's lots of info out there.
0
 
GlobaLevelCommented:
this more specifically:
public static void InsertOrUpdateCustomer(string customerId, string 

            companyName, string contactName, string contactTitle, string address, 

            string city, string region, string postalCode, string country, string 

            phone, string 

            fax)

        {

 

            NorthWindDataClassesDataContext dc = new 

            NorthWindDataClassesDataContext();

 

            var matchedCustomer = (from c in dc.GetTable<Customer>()

                                   where c.CustomerID == customerId

                                   select c).SingleOrDefault();

 

            if(matchedCustomer == null)

            {

                try

                {

                    // create new customer record since customer ID

                    // does not exist

                    Table<Customer> customers = Accessor.GetCustomerTable();

                    Customer cust = new Customer();

 

                    cust.CustomerID = customerId;

                    cust.CompanyName = companyName;

                    cust.ContactName = contactName;

                    cust.ContactTitle = contactTitle;

                    cust.Address = address;

                    cust.City = city;

                    cust.Region = region;

                    cust.PostalCode = postalCode;

                    cust.Country = country;

                    cust.Phone = phone;

                    cust.Fax = fax;

 

                    customers.InsertOnSubmit(cust);

                    customers.Context.SubmitChanges();

                }

                catch (Exception ex)

                {

                    throw ex;

                }  

            }

            else

            {

                try

                {

                    matchedCustomer.CompanyName = companyName;

                    matchedCustomer.ContactName = contactName;

                    matchedCustomer.ContactTitle = contactTitle;

                    matchedCustomer.Address = address;

                    matchedCustomer.City = city;

                    matchedCustomer.Region = region;

                    matchedCustomer.PostalCode = postalCode;

                    matchedCustomer.Country = country;

                    matchedCustomer.Phone = phone;

                    matchedCustomer.Fax = fax;

 

                    dc.SubmitChanges();

                }

                catch (Exception ex)

                {

                    throw ex;

                }

            }

 

        }

Open in new window

0
 
searchsanjaysharmaAuthor Commented:
ok
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now