Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have data in dtmain datatable.I want to use this dtmain data in a select query as below.Is there any method to fire query on datatable stored in datatable object.

Below is my code.

C#
DataSet ds = ReadDataFile("E:\\ImportData\\" + strName + ".xlsx");
 DataTable dtmain = ds.Tables[0];
Sql = "Select Name,Main.* from (SELECT EmpId, FName from  " + dtmain + ")as Main Left Join Table1 T1 ON T1.EmpId =Main.EmpId";

and fire Sql in sql server and get records.
Posted
Updated 16-Jul-14 1:21am
v4
Comments
swapnil_jadhav 16-Jul-14 7:09am    
Try using linQ
ZurdoDev 16-Jul-14 7:17am    
I'm confused. Do you want to find data that is in your DataTable or do you want to query Sql Server?

if I am not wrong you cannot mix data table with sql query.
 
Share this answer
 
 
Share this answer
 
use this solution i.e. use linq to query on datatable

C#
public DataSet ds;
public DataTable dt, dt1;
ds = new DataSet();

dt = new DataTable();
dt.TableName = "Employee";
dt.Columns.Add("EmployeeID", typeof(Int32));
dt.Columns.Add("Name");

 ds.Tables.Add(dt);

dt1 = new DataTable();
dt1.TableName = "EmployeeAddress";
dt1.Columns.Add("EmployeeID", typeof(Int32));
dt1.Columns.Add("EmployeeAddressID", typeof(Int32));
dt1.Columns.Add("Address");
dt1.Columns.Add("AddressType");
 ds.Tables.Add(dt);
 ds.Tables.Add(dt1);

 //Inner join
 var result = from emp in ds.Tables[0].AsEnumerable()
             join empAdd in ds.Tables[1].AsEnumerable()
                 on (int)emp["EmployeeID"] equals (int)empAdd["EmployeeID"]
             select new
             {
                 EmployeeID = (int)emp["EmployeeID"],
                 Name = (string)emp["Name"],
                 Address = (string)empAdd["Address"],
                 AddressType = (string)empAdd["AddressType"]
             };

foreach (var emp in result)
{
    Console.WriteLine(String.Format("EmployeeID : {0} , Name : {1} , Address : {2} , AddressType : {3}", emp.EmployeeID,emp.Name,emp.Address,emp.AddressType));
}

//Left join
var result = from emp in ds.Tables[0].AsEnumerable()
             join empAdd in ds.Tables[1].AsEnumerable()
                 on (int)emp["EmployeeID"] equals (int)empAdd["EmployeeID"] into AddGrp
             from record in AddGrp.DefaultIfEmpty()
             select new
             {
                 EmployeeID = (int)emp["EmployeeID"],
                 Name = (string)emp["Name"],
                 Address = record == null ? string.Empty : (string)record["Address"],
                 AddressType = record == null ? string.Empty : (string)record["AddressType"]
             };

foreach (var emp in result)
{
    Console.WriteLine(String.Format("EmployeeID : {0} , Name : {1} , Address : {2} , AddressType : {3}", emp.EmployeeID, emp.Name, emp.Address, emp.AddressType));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900