Uploading Data from DataSet to two different tables in SQL2005 using LINQ to DataSet and LINQ to SQL.

Posted on 2009-04-24
Last Modified: 2013-11-11
Uploading Data from DataSet to two different tables in SQL2005 using LINQ to DataSet and LINQ to SQL.

I have Excel file (Excel2007 file -C:\ExcelTest.xlsx)
And two tables in SQL Server 2005 database
I use VS2008/C#
I need to upload this file to dataset (It was done, and you can see the code sample).Everything is OK.
Finally I need to use LINQ to Dataset, make some parsing, for example use case statements, or pull same data from this dastaset and upload this data to two different existing tables in the same database.
I have some restrictions, I can not use SSIS, CLR store procs and so on for this uploading. I can use only LINQ and I can not use Linq Excel providers.
I am not familiar with LINQ,please give me a code sample, how to take data from DataSet, make some query and upload this data to two sql server tables.
I know I can use LINQ to DataSet and LINQ to SQL.
Thank You,
J_Kogan 4/24/2009

This is sample of my code It is working OK, but it is only dataset. 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.OleDb;

using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)



    protected void btnImportExcelToGrid_Click(object sender, EventArgs e)



        string fileName = FileUpload1.PostedFile.FileName;


        string strConn = @"Data Source=" + fileName + ";Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";


        //You must use the $ after the object

        //you reference in the spreadsheet

        //sheet#3 I need sheet#3 only NPS ENL ACC WITH QUALITY GOALS -name of the sheet

        DataSet ds3 = new DataSet();

        OleDbDataAdapter da3 = new OleDbDataAdapter



        //GreedView is only for the show

        GridView3.DataSource = ds3.Tables[0].DefaultView;








Open in new window

Question by:J_Kogan
    1 Comment
    LVL 21

    Accepted Solution

    now you need to cycle through the data, pulling out what you need and pushing it into your linq objects.  FIrst I use Linq to make generic objects of each row, then I run through those rows and insert them into linq-to-sql objects:
    var rows = from p in ds3.Tables[0].AsEnumerable()
      select new 
        name = p["Name"],
        address = p["Address"]
        something_else = p["column name"]
    //now you have a collection of objects that each represents a row, so push it into L2S now
    foreach(var v in rows)
      some_item i = new some_item();
      i.address = v.address;
      i.other = v.something_else;
    //this is assuming 'db' is your DataContext and you have an object named 'some_item'

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now