如何获取最后一个最大记录ID,增量ID,然后将记录添加到表c#应用程序

本文关键字:ID 记录 添加 然后 应用程序 增量 何获取 最后一个 获取 | 更新日期: 2023-09-27 18:16:20

下面的代码将一条新记录输入到表中。然而,由于某种原因,它正在拾取3126,并且该ID的记录已经存在。每次我运行这个函数,它都加1,但是id已经有了一个对应的值,直到3198。我该如何改变,我该做什么改变,这样它就能得到记录中已经存在的最大值,并将ID增加1,然后添加记录。

SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString);
try
{
    conn.Open();
    SqlCommand sqlCMD = new SqlCommand("InsertNewDeal", conn);
    sqlCMD.CommandType = System.Data.CommandType.StoredProcedure;
    sqlCMD.Parameters.Add("@Title", System.Data.SqlDbType.VarChar, 100);
    sqlCMD.Parameters.Add("@Office", System.Data.SqlDbType.Char, 3);
    sqlCMD.Parameters.Add("@EntryBy", System.Data.SqlDbType.VarChar, 50);
    sqlCMD.Parameters.Add("@CSR1", System.Data.SqlDbType.VarChar, 50);
    sqlCMD.Parameters.Add("@id", System.Data.SqlDbType.Int);
    sqlCMD.Parameters["@id"].Direction = System.Data.ParameterDirection.Output;

    sqlCMD.Parameters["@Title"].Value = txtName.Text;
    sqlCMD.Parameters["@Office"].Value = cmbOffice.SelectedValue;
    sqlCMD.Parameters["@CSR1"].Value = cmbCSR.SelectedValue;
    sqlCMD.Parameters["@EntryBy"].Value = LisaDatabaseManager.CurrentCSR.Username;
    CSR user = CSR.LoadCurrentUser();
    sqlCMD.ExecuteNonQuery();
    this.newProductionID = (int)sqlCMD.Parameters["@id"].Value;

    SqlCommand cmd = new SqlCommand("UPDATE Productions SET CountryCode = 'CAN', ProvinceCode = '" + user.GetProvinceCode() + "' WHERE ID = " + newProductionID, conn);
    cmd.ExecuteNonQuery();
    AddOptionalFields(newProductionID);
    LisaDatabaseManager.DealsTable.Fill(LisaDatabaseManager.DSGlobal.LoadDeals);
    DialogResult = DialogResult.OK;
    Close();
}
catch (Exception ex)
{
    ExceptionHandler.LogException("New deal", ex, LogSource.CHECK);
}
finally
{
    conn.Close();
}
}
}

InsertNewdeal存储过程如下:

ALTER PROCEDURE [dbo].[InsertNewDeal]
    @Title varchar(100),
    @EntryBy varchar(50),
    @Office char(3),
    @CSR1 varchar(50),
    @id int OUTPUT
AS
BEGIN
    SET NOCOUNT ON;
    declare @CamProb int;
    IF @PSOffice = 'VAN'
        set @CamProb = 1;
    else
        set @CamProb = NULL;
    INSERT INTO Productions(Title, EntryDate, EntryBy, Camera, Light, Grip, Generator, Expendables,IsDead, Office, CSR1, Indie,ProbCam)
        VALUES (@Title, CURRENT_TIMESTAMP, @EntryBy, 0,0,0,0,0,0, @Office, @CSR1,0,@CamProb);
    SELECT @id = Scope_Identity();
    INSERT INTO PostMortems(ProductionID,Indie) VALUES (@id,0);
    INSERT INTO Deals(Production, Date, SubRentals,InsuranceOnFile,DealOnFile, ChargeDVan, Charge5Tonne, ChargeDVan_out, Charge5Tonne_out, ChargeBulbReplacement) VALUES(@id,CURRENT_TIMESTAMP, 'Cost+10%',0,0, 75.00, 100.00, 150.00, 200.00, 1);
    DECLARE ItemCursor Cursor FOR
        select item from dealitems where id in (46048, 46052, 46036, 1, 46054, 15, 46056, 46057, 46058)
        order by
        case
            when id = 46048 then 1
            when id = 46052 then 2
            when id = 46036 then 3
            when id = 1 then 4
            when id = 46054 then 5
            when id = 15 then 6
            when id = 46056 then 7
            when id = 46057 then 8
            else 9
        end
    DECLARE @Description varchar(100)
    DECLARE @count int
    Set @count = 0
    OPEN ItemCursor
    FETCH NEXT FROM ItemCursor INTO @Description
    WHILE @@FETCH_STATUS = 0
    BEGIN
        INSERT INTO DealMemoItems(Deal, Item, Ord)
        VALUES(@id, @Description, @count)
        SET @count = (@count + 1)
        FETCH NEXT FROM ItemCursor INTO @Description
    END
    CLOSE ItemCursor
    DEALLOCATE ItemCursor
END

如何获取最后一个最大记录ID,增量ID,然后将记录添加到表c#应用程序

(SELECT TOP 1 id FROM <yourtable> ORDER BY id DESC) + 1

从表中获取最后一个最高的ID,并将其加1。您可以将其作为值直接放入SQL插入语句中,如下所示:

INSERT INTO <YOURTABLE> (id, name)
values (
     (SELECT TOP 1 id FROM <yourtable> ORDER BY id DESC) + 1,
     'Goober fish'
);

您是否可以将存储过程更改为:

SELECT SCOPE_IDENTITY()

返回最后插入记录的id。您不能依赖于id等于增加1的最大id,因为id可能会被重复使用。

注意,这是一个SQL Server解决方案,但通常在其他数据库引擎中也有等效的