使用VB函数作为SqlDataSource SelectParameter值

在我的代码后面我有一个函数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> 

回答:

要参数化你的查询,修改你的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() 

以上是 使用VB函数作为SqlDataSource SelectParameter值 的全部内容, 来源链接: utcz.com/qa/263218.html

回到顶部