2015-06-11 43 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

大聲笑謝謝:)很高興幫助。 –