gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:53:32 64 4
gpt4 key购买 nike

我有一个页面,我试图识别当前用户的 id 键,以便我可以为新表插入附加信息。我已经包含了在 SQL Server Express 中工作的页面的代码,但我认为在绑定(bind)上有一个引用问题来识别 @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: No mapping exists from DbType Object to a known SqlDbType.

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

最佳答案

基于 Managed Data Type Mappings (SQL Server Compact)SQL Server Data Type Mappings 相比Object 似乎不是有效的 DbType 并且无法映射到 SQL Server Compact 中的 SqlDbType(但是在 SqlServer 中有效)。

因为 Userid 是一个 Guid,尝试替换:

<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>

关于c# - ASP .Net 从 SQL Server Compact 数据库获取当前用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17262193/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com