ASP.Net从SQL Server Compact数据库获取当前用户ID

本文关键字:获取 用户 ID 数据库 Compact Net SQL Server ASP | 更新日期: 2023-09-27 18:19:30

我有一个页面,我正在尝试识别当前用户的id键,以便插入新表的附加信息。我已经包含了在SQLServerExpress中工作的页面的代码,但我认为在标识@UserID的绑定上存在参考问题。

有什么想法吗?

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" Title="Untitled Page" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data.SqlServerCe" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Xml.Linq" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Security.Permissions" %>
<script language="c#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UserProfileDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        //MembershipUser user = Membership.GetUser();
        //string UserID = user.ProviderUserKey.ToString();
        //e.Command.Parameters["@UserId"].Value = UserID;
        // Get a reference to the currently logged on user
        MembershipUser currentUser = Membership.GetUser();
        // Determine the currently logged on user's UserId value
        Guid currentUserId = (Guid)currentUser.ProviderUserKey;
        // Assign the currently logged on user's UserId to the @UserId parameter
        e.Command.Parameters["@UserId"].Value = currentUserId;
    }
    protected void UserProfile_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        SettingsUpdatedMessage.Visible = true;
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <h2>Update Your Settings</h2>
    <p>
        <asp:Label ID="SettingsUpdatedMessage" runat="server" Text="Your settings have been updated." EnableViewState="false" Visible="false"></asp:Label>
    </p>
    <!-- 
             ProviderName="System.Data.SqlServerCe.4.0" -->
    <asp:DetailsView ID="UserProfile" runat="server" 
        AutoGenerateRows="False" DataKeyNames="UserId" 
        DataSourceID="UserProfileDataSource" DefaultMode="Edit" 
        onitemupdated="UserProfile_ItemUpdated">
        <Fields>
            <asp:BoundField DataField="HomeTown" HeaderText="HomeTown" 
                SortExpression="HomeTown" />
            <asp:BoundField DataField="HomepageUrl" HeaderText="HomepageUrl" 
                SortExpression="HomepageUrl" />
            <asp:BoundField DataField="Signature" HeaderText="Signature" 
                SortExpression="Signature" />
            <asp:CommandField ShowEditButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="UserProfileDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
             ProviderName="<%$ ConnectionStrings:DefaultConnection.ProviderName %>"
        SelectCommand="SELECT [UserId], [HomeTown], [HomepageUrl], [Signature] FROM [UserProfiles] WHERE ([UserId] = @UserId)" 
        onselecting="UserProfileDataSource_Selecting" UpdateCommand="UPDATE UserProfiles SET
    HomeTown = @HomeTown,
    HomepageUrl = @HomepageUrl,
    Signature = @Signature
WHERE UserId = @UserId
">
        <SelectParameters>
            <asp:Parameter Name="UserId" Type="Object" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="HomeTown" />
            <asp:Parameter Name="HomepageUrl" />
            <asp:Parameter Name="Signature" />
            <asp:Parameter Name="UserId" />
        </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>

我一直收到错误:

System.ArgumentException:不存在从DbType对象到已知SqlDbType的映射。

我认为这与UserProfileDataSource_Selecting有关,但我不确定。我找不到可使用的等效SQL Server CE引用。

ASP.Net从SQL Server Compact数据库获取当前用户ID

根据托管数据类型映射(SQL Server Compact),与SQL Server数据类型映射相比,Object似乎不是有效的DbType,并且无法在SQL Server Compact中映射到SqlDbType(但在SQL Server)。

由于UseridGuid,请尝试替换:

<SelectParameters>
   <asp:Parameter Name="UserId" Type="Object" />
</SelectParameters>
<UpdateParameters>
   <asp:Parameter Name="HomeTown" />
   <asp:Parameter Name="HomepageUrl" />
   <asp:Parameter Name="Signature" />
   <asp:Parameter Name="UserId" />
</UpdateParameters>

带有:

<SelectParameters>
   <asp:Parameter Name="UserId" DbType="Guid" />
</SelectParameters>
<UpdateParameters>
   <asp:Parameter Name="HomeTown" />
   <asp:Parameter Name="HomepageUrl" />
   <asp:Parameter Name="Signature" />
   <asp:Parameter Name="UserId" DbType="Guid" />
</UpdateParameters>