2015-06-11 57 views
1

在我的代码后面我有一个函数getEmail(),返回一个电子邮件地址,在前端我有一个sql数据源selectcommand根据电子邮件地址获取信息。截至目前,电子邮件地址是硬编码的,我想知道如何将getEmail()放入select命令。使用VB函数作为SqlDataSource SelectParameter值

VB

Public Function getEmail() As String 
    Dim x As PublicProfile 
    x= UserProfileContext.Current.UserInfo 
    Return x.LoginName.ToString() 
    End Function 

前端

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="select Count(ID)nums from revs where Email='[email protected]' and Source='PUB' and Status='a'"></asp:SqlDataSource> 

我试图把<%= getEmail()%>而不是电子邮件,但没有运气

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="select Count(ID)nums from revs where Email='<% =getEmail() %>' and Source='PUB' and Status='a'"></asp:SqlDataSource> 
+0

容易,但冗长,所以我链接MSDN文章:https://msdn.microsoft.com/en-us/library/z72eefad%28v=vs.140%29.aspx? F = 255&MSPPError = -2147217396。简而言之,使用SQL参数并在页面加载时填充它们。 –

+0

现在即时阅读,但我不知道如何使用我的功能来改变选择参数 – bigdowg

+0

当你说你“没有运气”,发生了什么?空白,错误,蝙蝠飞出屏幕:)? –

回答

1

要参数化你的查询,修改你的SqlDataSource标记为foll OWS添加一个“@Email” SelectParameter:

<asp:SqlDataSource> 
     ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
    SelectCommand="select Count(ID)nums from revs where [email protected] and 
        Source='PUB' and Status='a'"> 
</asp:SqlDataSource> 

在您的代码隐藏/的Page_Load(),尝试添加下述(您的结合之前)放置一个值转换成在标记,这是所限定的SelectParameter(在转由你的函数提供):

SqlDataSource1.SelectParameters("Email").DefaultValue = getEmail() 
+1

美丽的我的朋友,堆栈需要更多像你 – bigdowg

+0

大声笑谢谢:)很高兴帮助。 –

相关问题