SQL查询字符串可以在SQL Server Management Studio中使用,但不能在带有SQLCommand.ExecuteReader的VB.net中使用
我有一个使用VB.net代码的ASPX页面,它被设置为运行一系列SQL查询并用结果填充SQLDataReader。每个SQL查询都是从SQL数据库中表中的特定字段中提取的。这段代码完全适用于我运行的每个查询,除了一个查询 涉及的VB代码如下所示SQL查询字符串可以在SQL Server Management Studio中使用,但不能在带有SQLCommand.ExecuteReader的VB.net中使用,sql,sql-server,vb.net,sqldatareader,sqlcommand,Sql,Sql Server,Vb.net,Sqldatareader,Sqlcommand,我有一个使用VB.net代码的ASPX页面,它被设置为运行一系列SQL查询并用结果填充SQLDataReader。每个SQL查询都是从SQL数据库中表中的特定字段中提取的。这段代码完全适用于我运行的每个查询,除了一个查询 涉及的VB代码如下所示 Dim SQL_Template_Query As String Dim cmd3 As SqlCommand = New SqlCommand Dim r3 As SqlDataReader cmd3 = New SqlCommand(SQL
Dim SQL_Template_Query As String
Dim cmd3 As SqlCommand = New SqlCommand
Dim r3 As SqlDataReader
cmd3 = New SqlCommand(SQL_Template_Query)
cmd3.CommandTimeout = 1200 ' Extended to 1200 sec = 20 min
cmd3.CommandType = CommandType.Text
cmd3.Connection = Global_Objects.SQLLocalDB_Connection
Try
r3 = Nothing
r3 = cmd3.ExecuteReader() ' THIS LINE IS WHERE THE ERROR OCCURS
Catch
Session("Main_Error_Code") = 1
Session("Main_Error_Message") = "The SQL statement for the following address is invalid. Please examine the GCDB_Excel_Reports_Templates record, and correct." &
Chr(13) & "ID " & ti.ID & ", " & ti.Sheet_Name & ", " & ti.Cell_Address
' Write to error log
cxlErrorsSheetLog.Cell("A" & cxlErrorsCurRow).Value = Session("Main_Error_Message")
cxlErrors.SaveAs(cxlErrorsPath)
cxlErrors = Nothing
cxlDoc = Nothing
Return
End Try
正如您所看到的,我已经准备好了捕获错误的代码,因此我确切地知道错误发生的位置,但我不确定为什么。错误发生在
SQL\u Template\u Query=“DECLARE@Start\u Year int;SET@Start\u Year=2018;DECLARE@Reporting\u Year int;SET@Reporting\u Year=2021;DECLARE@First\u Year int;SET@First\u Year=(在(@Reporting\u Year-@Start\u Year+1)情况下)因此,@AndrewMorton在使用单个连接的多个SQL查询方面似乎走上了正确的道路。我创建了一个全新的VB.net项目,添加了代码以提取导致问题的特定存储SQL语句,然后运行它。它工作正常。我知道,在原始项目中,我替换了代码要在单独的连接上运行特定的SqlCommand,请在每个循环后处理连接实例 代码现在按预期工作。仍然不清楚为什么这个特定的SQL语句会导致问题,而不是其他语句。但是,无论如何,我将重新编写此项目中的所有代码,以脱离单个连接对象
@在安德烈莫顿的其他评论中提供的链接也是有用的。实际上,我一直在寻找一种独立检查SQL查询的方法,而不必使用底层数据库。
您是否考虑过使用存储过程?是否尝试过“catch Ex”,以便您可以获得异常,并打印出异常的文本(Ex.Mead)。?@Granny我对存储过程的理解是,要创建一个存储过程,需要事先声明参数。上面查询中显示的前两个参数实际上是在代码运行时动态创建的,并且随模板的不同而不同。与SQL查询本身的大部分类似,它们是在运行时从表中提取的。因此ey在代码实际运行之前是未知的。@J.P.Brown顺便说一句,如果Global\u对象。SQLLocalDB\u Connection
是整个过程中使用的一个连接对象,这是使用连接的错误方法。正确的方法是实例化连接,使用它,然后调用.Dispose()
。@J.P.Brown如果您将SQL发布到中并使用“分析”按钮,它会显示一些有用的建议。
DECLARE @Start_Year int;
SET @Start_Year=2018;
DECLARE @Reporting_Year int;
SET @Reporting_Year=2021;
declare @First_Year int;
set @First_Year =
(
case
when (@Reporting_Year-@Start_Year+1)<=5 then @Start_Year
else @Reporting_Year - 4 end
);
drop table if exists #Reporting_Years;
create table #Reporting_Years
(
Reporting_Year int
);
insert into #Reporting_Years
select distinct YEAR(Single_Date)
from Single_Dates where YEAR(Single_Date)
between @Reporting_Year-4 and @Reporting_Year;
drop table if exists #Has_ACT;
create table #Has_ACT (Building_ID int,Start_Date datetime,End_Date datetime);
insert into #Has_ACT
select distinct Building_ID,MIN(Start_Date),MAX(End_Date) from Tracking_Periods
where Utility_Type_ID between 16 and 19
and YEAR(Start_Date)<=@Reporting_Year
and (YEAR(End_Date)>=@First_Year or End_Date is null)
group by Building_ID;
select i1.Reporting_Year,
SUM(case when i2.Asset_Class like 'Office' then GFA else null end) as Office_GFA,
SUM(case when i2.Asset_Class like 'Retail' then GFA else null end) as Retail_GFA,
SUM(case when i2.Asset_Class like 'Industrial' then GFA else null end) as Industrial_GFA
from #Reporting_Years i1
left join
(
select z1.Reporting_Year,x.Building_ID,y.Building,y.Asset_Class,x.Start_Year,x.End_Year,y.Most_Recent_GFA_Year,
case when z1.Reporting_Year<=y.Most_Recent_GFA_Year
then
(select GFA from Annual_GFA where Building_ID=x.Building_ID and Reporting_Year=z1.Reporting_Year)
else
(select GFA from Annual_GFA where Building_ID=x.Building_ID and Reporting_Year=y.Most_Recent_GFA_Year)
end as GFA
from
(
select Building_ID,
case
when YEAR(Start_Date)<@First_Year then @First_Year
else YEAR(Start_Date) end as Start_Year,
case
when End_Date is null then @Reporting_Year
when YEAR(End_Date)>@Reporting_Year then @Reporting_Year
else YEAR(End_Date) end as End_Year from #Has_ACT) x
left join
(
select a.Building_ID,a.Building,a.Asset_Class,MAX(a.Reporting_Year) as Most_Recent_GFA_Year
from View_All_Buildings_Annual_GFA a
inner join View_All_Buildings b on a.Building_ID=b.Building_ID
inner join View_All_Building_Ownership_By_Year c on c.Building_ID=a.Building_ID and c.Reporting_Year=@Reporting_Year
inner join Building_Ownership d on a.Building_ID=d.Building_ID
left join Override_Building_Count e on a.Building_ID=e.Building_ID and e.Report_ID=5
inner join #Has_ACT f on f.Building_ID=a.Building_ID
where b.Asset_Manager like 'Anonymous'
and b.Not_On_Program=0 and b.Exclude_From_Reporting=0 and b.Tenant=0
and d.Year_Removed is null
and (e.Building_Count<>0 or e.Building_Count is null)
group by a.Building_ID,a.Building,a.Asset_Class) y on x.Building_ID=y.Building_ID
cross join #Reporting_Years z1 where z1.Reporting_Year between @First_Year and @Reporting_Year
and z1.Reporting_Year>=x.Start_Year and z1.Reporting_Year<=x.End_Year
and y.Building_ID is not null) i2 on i1.Reporting_Year=i2.Reporting_Year
where i1.Reporting_Year between @Start_Year and @Reporting_Year
group by i1.Reporting_Year
order by i1.Reporting_Year;