gpt4 book ai didi

c# - SCOPE_IDENTITY 返回错误的 ID

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:05 24 4
gpt4 key购买 nike

我正在尝试返回新创建行的 ID。我试过用 SCOPE_IDENTITY 来做,但它返回 3。我四处搜索,无法确定我做错了什么。

代码如下:

    <asp:SqlDataSource ID="sourceRecruit" runat="server" 
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT * FROM [Players] WHERE ([ID] = @ID)"
UpdateCommand="UPDATE [Players] SET [FirstName] = @FirstName, [LastName]=@LastName, [Height]=@Height, [Weight]=@Weight, [FortyYardDash] = @FortyYardDash, [CombineVerified] = @CombineVerified, [HomeNumber] = @HomeNumber, [CellNumber] = @CellNumber, [Email] = @Email, [Position] = @Position, [Commitment] = @Commitment, [GraduationYear] = @GraduationYear, [LastUpdate] = @LastUpdate, [NetworkUpdate] = @NetworkUpdate, [DecisionDate] = @DecisionDate, [MiscContact] = @MiscContact, [RivalsID] = @RivalsID, [ScoutID] = @ScoutID, [ESPNID] = @ESPNID, [t247SportsID] = @t247SportsID, [PictureID] = @PictureID, [StarRating] = @StarRating, [NationalHot100Rank] = @NationalHot100Rank, [PositionRank] = @PositionRank, [JanuaryEnrollee] = @JanuaryEnrollee, [Recruiter] = @Recruiter, [RecruitedPosition] = @RecruitedPosition, [SummerCamp] = @SummerCamp, [JuniorDay] = @JuniorDay, [SpringGame] = @SpringGame, [UnofficialVisit] = @UnofficialVisit, [FootballGame] = @FootballGame, [OfficialVisitDate] = @OfficialVisitDate, [Status] = @Status, [Updates] = @Updates, [Favorites] = @Favorites, [Visits] = @Visits, [Odds] = @Odds WHERE [ID] = @ID"
InsertCommand="INSERT INTO Players ([FirstName], [LastName], [Height], [Weight], [FortyYardDash], [CombineVerified], [HomeNumber], [CellNumber], [Email], [Position], [Commitment], [GraduationYear], [LastUpdate], [NetworkUpdate], [DecisionDate], [MiscContact], [RivalsID], [ScoutID], [ESPNID], [t247SportsID], [PictureID], [StarRating], [NationalHot100Rank], [PositionRank], [JanuaryEnrollee], [Recruiter], [RecruitedPosition], [SummerCamp], [JuniorDay], [SpringGame], [UnofficialVisit], [FootballGame], [OfficialVisitDate], [Status], [Updates], [Favorites], [Visits], [Odds]) VALUES (@FirstName, @LastName, @Height, @Weight, @FortyYardDash, @CombineVerified, @HomeNumber, @CellNumber, @Email, @Position, @Commitment, @GraduationYear, @LastUpdate, @NetworkUpdate, @DecisionDate, @MiscContact, @RivalsID, @ScoutID, @ESPNID, @t247SportsID, @PictureID, @StarRating, @NationalHot100Rank, @PositionRank, @JanuaryEnrollee, @Recruiter, @RecruitedPosition, @SummerCamp, @JuniorDay, @SpringGame, @UnofficialVisit, @FootballGame, @OfficialVisitDate, @Status, @Updates, @Favorites, @Visits, @Odds); SET @ID=SCOPE_IDENTITY()"
OnInserted="reloadPage">

<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DecisionDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="NetworkUpdate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="OfficialVisitDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="LastUpdate" ConvertEmptyStringToNull="true" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" DefaultValue="Int32" Direction="Output" />
<asp:Parameter Name="DecisionDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="NetworkUpdate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="OfficialVisitDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="LastUpdate" ConvertEmptyStringToNull="true" />
</InsertParameters>
</asp:SqlDataSource>

下面是我的代码:

    protected void reloadPage(object sender, SqlDataSourceStatusEventArgs e)
{

object newid = e.Command.Parameters["@ID"].Value;
lblResults.Text = "Test "+Convert.ToString(newid);
}

正如我所提到的,每次我运行代码时,lblResults 都会显示“3”,但 ID 在 3000 范围内。

最佳答案

需要使用OnInserted事件,并指定参数的输出方向:

<asp:Parameter Direction="Output" Name="ID" Type="Int32" />

代码隐藏:

protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
int id = Convert.ToInt32(e.Command.Parameter["@ID"].Value);
}

我还注意到您设置了 DefaultValue="Int32"。您应该将其更改为 Type="Int32"

如果在应用上述更改后仍有问题,请尝试将 SET @ID = SCOPE_IDENTITY 更改为 SELECT @ID = SCOPE_IDENTITY。应该不会有什么不同,但如果一切都失败了……

关于c# - SCOPE_IDENTITY 返回错误的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7742873/

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