[英]ASP.NET Code in GridView
I'm converting a classic ASP site to ASP.NET (C#) and there are several stored procedures that can be called from one page. 我正在将一个经典的ASP站点转换为ASP.NET(C#),并且可以从一个页面调用几个存储过程。 Currently in the classic ASP the values such as the stored procedure name and other parameters are passed in the query string.
目前在经典ASP中,诸如存储过程名称和其他参数之类的值在查询字符串中传递。 I'm trying to determine a way of doing this in ASP.NET now as I cannot seem to use variables as the stored procedure name.
我正试图在ASP.NET中确定一种方法,因为我似乎无法使用变量作为存储过程名称。 What I tried to do was the following:
我试图做的是以下内容:
<% string sp = Request.QueryString["sp"]; %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="<%= sp%>" SelectCommandType="StoredProcedure">
The second part of this is that of the stored procedures, there can be a different number of parameters needed. 第二部分是存储过程,可能需要不同数量的参数。 In my classic ASP I was passing the parameters over via query string but first checking to see if they were blank and if so, not to attempt to pass them as a parameter to the stored procedure.
在我的经典ASP中,我通过查询字符串传递参数,但首先检查它们是否为空白,如果是,则不要尝试将它们作为参数传递给存储过程。 Here is how I was doing it in classic ASP:
以下是我在经典ASP中的表现:
If param1 <> "" then
cmd.Parameters(i) = param1
i=i+1
End If
If param2 <> "" then
cmd.Parameters(i) = param2
i=i+1
End If
If param3 <> "" then
cmd.Parameters(i) = param3
i=i+1
End If
If param4 <> "" then
cmd.Parameters(i) = param4
End If
I don't see a way to accomplish this in .NET either since I can't place code within the GridView. 我没有看到在.NET中实现此目的的方法,因为我无法在GridView中放置代码。 How can do accomplish both of these pieces?
怎么能完成这两件作品?
Edit : I've solved the first half of my problem by doing the following in my Page_Load
event as follows: 编辑 :我通过在
Page_Load
事件中执行以下操作解决了问题的前半部分,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
sp = Request.QueryString["sp"];
SqlDataSource1.SelectCommand = sp;
}
Now the only remaining issue is how to only pass parameters with values assigned. 现在唯一剩下的问题是如何仅传递具有指定值的参数。
Problem solved. 问题解决了。 In the code behind:
在后面的代码中:
protected void Page_Load(object sender, EventArgs e)
{
sp = Request.QueryString["sp"];
param1 = Request.QueryString["param1"];
param2 = Request.QueryString["param2"];
param3 = Request.QueryString["param3"];
SqlDataSource1.SelectCommand = sp;
if (param1 != null)
{
SqlDataSource1.SelectParameters.Add("param1", param1);
}
if (param2 != null)
{
SqlDataSource1.SelectParameters.Add("param2", param2);
}
if (param3 != null)
{
SqlDataSource1.SelectParameters.Add("param3", param3);
}
}
In the aspx 在aspx中
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
I think this is not exactly what you want but can help: 我认为这不是你想要的,但可以提供帮助:
<asp:SqlDataSource ID="DataSource1" runat="server" SelectCommand="Your SQL"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
<SelectParameters>
<asp:QueryStringParameter Name="sp" QueryStringField="sp"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
In this case the SQL you provide will end up with the query string sp
as a parameter. 在这种情况下,您提供的SQL将以查询字符串
sp
作为参数结束。
Maybe you can do something like this : 也许你可以这样做此 :
EXEC (@sp);
I couldn't test it so take with a grain of salt. 我无法测试它,所以带上一粒盐。 But i think you can elaborate more beyond this.
但我认为除此之外你还可以详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.