Visual Studio 2010数据网格中带有临时表的Sql查询

Visual Studio 2010数据网格中带有临时表的Sql查询,sql,visual-studio-2010,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

如果这个问题已经得到回答,我很抱歉,我读了其他几篇帖子,没有看到对我问题的明确答案。我是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

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

CREATE PROCEDURE dbo.MyData
AS
SET NOCOUNT ON;
BEGIN
    <insert your SQL here>
END;
创建过程dbo.MyData
作为
不计数;
开始
结束;
回到VisualStudio,您将需要打开到数据库的连接,然后打开一个将与数据读取器一起使用的命令。应该有很多这样做的例子,但我会给你一个非常简短的示例(抱歉,它是C#,我不接触VB):

使用(var con=new SqlConnection(“Server=WHATEVERSERVER;Database=WHATEVERDBS;Trusted_Connection=True;”)
{
con.Open();
使用(var com=con.CreateCommand())
{
com.CommantText=“EXECUTE.dbo.MyData;”;
使用(var dr=com.ExecuteReader())
{
while(dr.Read())
{
var row=新字符串[7];
行[0]=dr[“rmn”].ToString();
行[1]=dr[“缺少”].ToString();
等
MyDataGrid.Rows.Add(row);
}
}
}
}
最后,您可以从数据网格中提取行数,以获得要在窗口中显示的数据行数

如果无法创建存储过程,则可以将整个SQL脚本键入命令文本位,但必须非常小心使用回车、空格、文字、引号等


再次,很抱歉,我的示例代码是C#,我只是想给你一个非常基本的框架,所以至少你知道去谷歌输入什么…

你需要以任何方式编辑数据吗(提示你可能无法编辑)?您是否计划使用实体框架,如果没有,那么如何将数据从SQL Server获取到datagrid中?就我个人而言,我只是将其包装在一个存储过程中,并将其强制放入一个只读数据网格中,但这可能不是您想要的。您使用哪种语言(如C#、VB)?最后,你是否坚持VS2010?谢谢你的回复。数据将是只读的,我只能使用VS2010。我使用的是VB windows窗体应用程序。我没有计划使用实体框架。程序的最终结果将只是每次运行查询并插入新表时计算行数。。。我需要那些临时表来获得我需要的输出,但不知道如何在VSI中复制它今天将尝试一下,谢谢Richard。
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);
            }
        }
    }
}