本文介绍了如何通过变量的SqlDataSource的SelectCommand中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从code传递变量背后的SqlDataSource的SelectCommand中?
我不想使用内置的参数类型(如ControlParemeter,QueryStringParameter等)
我需要通过一个vraiable?
下面的示例不起作用
< ASP:SqlDataSource的ID =SqlDataSource1=服务器
的ConnectionString =&其中;%$的ConnectionStrings:itematConnectionString%>中
的SelectCommand =SELECT items.name,items.id从项目INNER JOIN users_items ON items.id = users_items.id WHERE(users_items.user_id = @userId)ORDER BY users_items.date降序>
< SelectParameters>
< ASP:参数默认值=<%=用户id%>中NAME =用户id的DbType =GUID/>
< / SelectParameters>
解决方案
您总是可以像这样做的背后,code:
SqlDataSource1.SelectParameters.Add(@用户id,用户id);
修改
试试这个来代替,删除SelectCommand属性和SelectParameters:
< ASP:SqlDataSource的ID =SqlDataSource1=服务器
的ConnectionString =<%$的ConnectionStrings:itematConnectionString%>>
然后在code背后做到这一点:
SqlDataSource1.SelectParameters.Add(用户id,userId.ToString());
SqlDataSource1.SelectCommand =SELECT items.name,items.id从项目INNER JOIN users_items ON items.id = users_items.id WHERE(users_items.user_id = @userId)ORDER BY users_items.date降序
这工作对我来说,这也适用:
< ASP:SqlDataSource的ID =SqlDataSource1=服务器
的ConnectionString =&其中;%$的ConnectionStrings:itematConnectionString%>中
的SelectCommand =SELECT items.name,items.id从项目INNER JOIN users_items ON items.id = users_items.id WHERE(users_items.user_id = @userId)ORDER BY users_items.date降序>< / ASP:SqlDataSource的>
SqlDataSource1.SelectParameters.Add(用户id,DbType.Guid,userId.ToString());
I want to pass variable from the code behind to SelectCommand of SqlDataSource?
I dont want to use built-in parameter types (like ControlParemeter, QueryStringParameter, etc)
I need to pass a vraiable?
the following example does not work
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:itematConnectionString %>"
SelectCommand = "SELECT items.name, items.id FROM items INNER JOIN users_items ON items.id = users_items.id WHERE (users_items.user_id = @userId) ORDER BY users_items.date DESC">
<SelectParameters>
<asp:Parameter DefaultValue="<%= userId %>" Name="userId" DbType="Guid" />
</SelectParameters>
解决方案
You could always do it like this in the code behind:
SqlDataSource1.SelectParameters.Add("@userId", userId);
EDIT
Try this instead, remove the SelectCommand property and SelectParameters:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:itematConnectionString %>">
Then in the code behind do this:
SqlDataSource1.SelectParameters.Add("userId", userId.ToString());
SqlDataSource1.SelectCommand = "SELECT items.name, items.id FROM items INNER JOIN users_items ON items.id = users_items.id WHERE (users_items.user_id = @userId) ORDER BY users_items.date DESC"
This worked for me, this also works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:itematConnectionString %>"
SelectCommand = "SELECT items.name, items.id FROM items INNER JOIN users_items ON items.id = users_items.id WHERE (users_items.user_id = @userId) ORDER BY users_items.date DESC"></asp:SqlDataSource>
SqlDataSource1.SelectParameters.Add("userid", DbType.Guid, userId.ToString());
这篇关于如何通过变量的SqlDataSource的SelectCommand中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!