我正在尝试返回新创建的行的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范围内。
答案 0 :(得分:2)
您需要使用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
。不应该有所作为,但如果一切都失败了......
答案 1 :(得分:0)
你试过吗
SELECT @ID = SCOPE_IDENTITY()
而不是
SET @ID = SCOPE_IDENTITY()
这就是我所拥有并且完美无缺的。