简体   繁体   中英

Sql Query with Temp tables in Visual Studio 2010 data grid

I apologize up front if this has been answered, i read several other posts and didn't see a clear answer to my question. I am a beginner at VS2010. Basically i have the below query and i want it to display in the data grid view when i run my program.

I can use VS2010 to join two actual tables but as you can see below the temp tables are very important.

 IF OBJECT_ID('tempdb..#tempbatch') IS NOT NULL DROP TABLE #tempbatch
      IF OBJECT_ID('tempdb..#tempbatch2') IS NOT NULL DROP TABLE #tempbatch2
      IF OBJECT_ID('tempdb..#tempbatch1') IS NOT NULL DROP TABLE #tempbatch1
      IF OBJECT_ID('tempdb..#tempbatch3') IS NOT NULL DROP TABLE #tempbatch3
      IF OBJECT_ID('tempdb..#tempbatch4') IS NOT NULL DROP TABLE #tempbatch4
      IF OBJECT_ID('tempdb..#tempbatch5') IS NOT NULL DROP TABLE #tempbatch5
      IF OBJECT_ID('tempdb..#tempbatch6') IS NOT NULL DROP TABLE #tempbatch6
      IF OBJECT_ID('tempdb..#tempbatch7') IS NOT NULL DROP TABLE #tempbatch7
      IF OBJECT_ID('tempdb..#tempbatch8') IS NOT NULL DROP TABLE #tempbatch8
      IF OBJECT_ID('tempdb..#tempbatch9') IS NOT NULL DROP TABLE #tempbatch9
      IF OBJECT_ID('tempdb..#tempbatch10') IS NOT NULL DROP TABLE #tempbatch10

      create table #tempbatch (rowid bigint primary key identity(1,1), shipmentno varchar(64))
  insert into #tempbatch select * from @unitnames


select distinct b.dcsID, a.BoxType,  b.BoxNO, b.shipmentno, b.PaletteWithinShipment into #tempbatch1 from #tempbatch c
join dva..Boxmapping as a
on c.shipmentno = a.shipmentno 
join dva..Boxmapping as b
on a.boxno = b.BoxNO
--where b.shipmentno = '@rmn'
group by b.dcsID, a.BoxType, b.BoxNO, b.shipmentno, b.PaletteWithinShipment
order by b.PaletteWithinShipment, b.BoxNO

--select dcsid,boxtype,boxno,shipmentno from #tempbatch1 

select distinct a.dcsid,b.dcsid as manifestDCS,b.rmn into #tempbatch3 from #tempbatch1 a

left outer join dva..manifestDCS b
on a.dcsid = b.dcsid 

select distinct b.dcsid,a.rmn into #tempbatch5 from #tempbatch3 a
left outer join dva..manifestdcs b
on a.rmn = b.rmn


 select b.dcsid as deliverexDCSID,a.dcsid,a.rmn,pbatch  into #tempbatch4 from #tempbatch5 a
 left outer join #tempbatch1 b
  on a.dcsid = b.dcsid 
  join dva..document c
  on a.dcsid = c.dcsid 

  where a.dcsid not in (select dcsid from dva..document where ftpstime is null) and a.dcsid not in (select dcsid from dva..boxmapping)   

 delete from #tempbatch4 where deliverexdcsid is not null 


  ----- ******************************** START OF SECOND QUERY *********************************-------------


  select * into #tempbatch6 from dva..Boxmapping 

select distinct c.rmn,c.dcsid,b.dcsid as BoxDCSID,a.pbatch into #tempbatch8 from #tempbatch4 a
left outer join dva..manifestDCS b
on a.dcsid = b.dcsid 
left outer join dva..manifestdcs c
on b.rmn = c.rmn   

 select distinct  c.rmn,c.dcsid as Missing,a.dcsid,d.BoxNO,d.boxtype,e.palette,e.PaletteWithinShipment,e.shipmentno into #tempbatch9 from #tempbatch8 a
 left outer join #tempbatch4 c
 on a.rmn = c.rmn 
 left outer join dva..boxmapping d
 on b.dcsid = d.dcsid 
 left outer join dva..boxmapping e
 on d.boxno = e.boxno 

 delete from #tempbatch9 where dcsID is null
 delete from #tempbatch9 where boxno is null  
 delete from #tempbatch9 where palette is null 


 select distinct rmn,missing,boxno,boxtype,PaletteWithinShipment,shipmentno from #tempbatch9

 order by rmn,PaletteWithinShipment

Wrap the whole of your query in a stored procedure, ie

CREATE PROCEDURE dbo.MyData
AS
SET NOCOUNT ON;
BEGIN
    <insert your SQL here>
END;

Back in Visual Studio you will need to open a connection to your database, then open a command that you will use in conjunction with a data reader. There should be plenty of examples of how to do this, but I will give you a very short sample (sorry it's in C#, I don't touch VB):

using (var con = new SqlConnection("Server=WHATEVERSERVER;Database=WHATEVERDBS;Trusted_Connection=True;"))
{
    con.Open();
    using (var com = con.CreateCommand())
    {
        com.CommantText = "EXECUTE <MyDatabase>.dbo.MyData;";
        using (var dr = com.ExecuteReader())
        {
            while (dr.Read())
            {
                var row = new string[7];
                row[0] = dr["rmn"].ToString();
                row[1] = dr["missing"].ToString();
                etc...
                MyDataGrid.Rows.Add(row);
            }
        }
    }
}

Finally, you can just pull back a row count from your data grid, to get the number of rows of data that you want to display in a window.

If you can't create a stored procedure then you could type the entire SQL script into the command text bit, but you would have to be very careful with the carriage returns, spacing, literals, quotes, etc.

Again, apologies that my example code is in C#, I was just wanting to give you a very basic framework, so at the very least you know what to go and type into Google...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM