简体   繁体   English

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

[英]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. 我是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. 我可以使用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

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. 返回Visual Studio,您将需要打开与数据库的连接,然后打开将与数据读取器结合使用的命令。 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): 应该有很多有关如何执行此操作的示例,但是我将为您提供一个非常简短的示例(很抱歉,它是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);
            }
        }
    }
}

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. 如果您无法创建存储过程,则可以在命令文本位中键入整个SQL脚本,但是您必须非常注意回车,空格,文字,引号等。

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... 再次抱歉,我的示例代码是C#,我只是想为您提供一个非常基本的框架,因此至少您知道要输入Google的内容了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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