对所有受尊敬的成员,您好
目前我在asp.net中使用以下SELECT命令来基于查询字符串值填充Gridview:
SelectCommand="SELECT ID, Date, Train, I_R, Dir_Ind, Detn, Rly, DiV, Loco, Shed, locoClass, loco_type FROM PunctualityMain WHERE Date >= @strdt AND Date <= @enddt AND ([Rly] = @rly OR @rly IS NULL) AND ([shed] = @shed OR @shed IS NULL) Order by Date ASC">
<SelectParameters>
<asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
<asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
<asp:QueryStringParameter Name="rly" QueryStringField="rly" Type="string" />
<asp:QueryStringParameter Name="shed" QueryStringField="shed" Type="string" />
</SelectParameters>
它的工作正常,两个值为null:
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=&shed=
现在我想对Gridview使用相同的删除,插入&amp;更新命令,如下所示:
DeleteCommand="DELETE FROM [PunctualityMain] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [PunctualityMain] ([Date], [Train], [Dir_Ind], [Detn], [Rly], [DiV], [Loco], [Shed], [locoClass], [loco_type]) VALUES (@Date, @Train, @Dir_Ind, @Detn, @Rly, @DiV, @Loco, @Shed, @locoClass, @loco_type)"
SelectCommand="SELECT * FROM [PunctualityMain] WHERE (([Date] >= @strdt) AND ([Date] <= @enddt) AND ([Shed] = @shed OR @shed IS NULL) AND ([Rly] = @rly OR @rly IS NULL)) ORDER BY [Date]"
UpdateCommand="UPDATE [PunctualityMain] SET [Date] = @Date, [Train] = @Train, [Dir_Ind] = @Dir_Ind, [Detn] = @Detn, [Rly] = @Rly, [DiV] = @DiV, [Loco] = @Loco, [Shed] = @Shed, [locoClass] = @locoClass, [loco_type] = @loco_type WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Train" Type="Int32" />
<asp:Parameter Name="Dir_Ind" Type="String" />
<asp:Parameter Name="Detn" Type="Int32" />
<asp:Parameter Name="Rly" Type="String" />
<asp:Parameter Name="DiV" Type="String" />
<asp:Parameter Name="Loco" Type="String" />
<asp:Parameter Name="Shed" Type="String" />
<asp:Parameter Name="locoClass" Type="String" />
<asp:Parameter Name="loco_type" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
<asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
<asp:QueryStringParameter Name="rly" QueryStringField="rly" Type="string" />
<asp:QueryStringParameter Name="shed" QueryStringField="shed" Type="string" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Train" Type="Int32" />
<asp:Parameter Name="Dir_Ind" Type="String" />
<asp:Parameter Name="Detn" Type="Int32" />
<asp:Parameter Name="Rly" Type="String" />
<asp:Parameter Name="DiV" Type="String" />
<asp:Parameter Name="Loco" Type="String" />
<asp:Parameter Name="Shed" Type="String" />
<asp:Parameter Name="locoClass" Type="String" />
<asp:Parameter Name="loco_type" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
为什么上面的SELECT命令不能用于rly&amp;的NULL值棚子?但是,使用所有四个查询字符串值,它就像这样
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=CR&shed=KYN
请建议任何问题的解决方案,以便它也可以使用此字符串(空字符串值)
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=&shed=
答案 0 :(得分:0)
因为从空查询字符串值= / = null
获得空值为了使其正常工作,您应手动设置空值。 我建议你至少有两种方式:
1)在代码隐藏中:
string rly = Request.QueryString["rly"];
if (rly == "")
rly = null;
然后使用它。
2)在SQL命令中:
Rly=NullIf('@rly','')
但是我不确定第二个是否会像这样工作,如果不能,那么你可以尝试解决它:
codebehind string empt = "";
SQL命令Rly=NullIf('@rly','+empt+')
或者你可以采取其他方式,重点是验证它“rly”查询是否为“”,如果是,则将其设置为null,否则不管它。