在Visual Studio 2010数据网格中使用Temp表进行SQL查询

时间:2016-04-18 15:30:01

标签: sql visual-studio-2010

如果有人回答我,我会在前面道歉,我读了几篇其他帖子,并没有看到我的问题的明确答案。我是VS2010的初学者。基本上我有以下查询,我希望它在我运行我的程序时显示在数据网格视图中。

我可以使用VS2010连接两个实际表,但正如您在下面看到的那样,临时表非常重要。

 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

1 个答案:

答案 0 :(得分:0)

将整个查询包装在存储过程中,即

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

返回Visual Studio,您需要打开与数据库的连接,然后打开一个与数据读取器结合使用的命令。应该有很多如何做到这一点的例子,但是我会给你一个非常简短的样本(对不起它在C#,我不接触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);
            }
        }
    }
}

最后,您可以从数据网格中提取行数,以获取要在窗口中显示的数据行数。

如果您无法创建存储过程,那么可以将整个SQL脚本输入到命令文本位中,但您必须非常小心回车,间距,文字,引号等

再一次,道歉我的示例代码在C#中,我只是想给你一个非常基本的框架,所以至少你知道该怎么做并输入Google ...