简体   繁体   English

在C#中自动递增非主键(SQL Server 2014)

[英]Auto increment a non-primary key (SQL Server 2014) in C#

private void btnOk_Click(object sender, EventArgs e)
{
        Helper.openConnection();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = Helper.cn;
        cmd.CommandText = "insert into Characters values(@Name, 'Novice', 1, 40, 10, 0, @STR, @AGI, @VIT, @DEX, @LUK, @INT, 1, 1)";
        cmd.Parameters.AddWithValue("@Name", txtName.Text);
        cmd.Parameters.AddWithValue("@STR", txtStr.Text);
        cmd.Parameters.AddWithValue("@AGI", txtAgi.Text);
        cmd.Parameters.AddWithValue("@VIT", txtVit.Text);
        cmd.Parameters.AddWithValue("@DEX", txtDex.Text);
        cmd.Parameters.AddWithValue("@LUK", txtLuk.Text);
        cmd.Parameters.AddWithValue("@INT", txtInt.Text);
        cmd.ExecuteNonQuery();
        Helper.cn.Close();
        MessageBox.Show("Character successfully created!");
        this.Close();
}

Written above is my code for a simple character creation screen for my project in C#. 上面写的是我为C#项目中的简单字符创建屏幕编写的代码。 I have a problem of doing an auto increment of the CharacterSlot column, which is a non-primary key since in the picture below, CharacterID is my primary key. 我有一个问题,我需要对CharacterSlot列进行自动递增,这是一个非主键,因为在下图中, CharacterID是我的主键。

I also chose CharacterID as primary since we can have multiple characters per account. 我还选择了CharacterID作为主要CharacterID ,因为每个帐户可以有多个字符。 Below are images from my SQL Server database. 以下是我的SQL Server数据库中的图像。

字符表

用户表

EDIT: Per character slot is a clickable picture box that changes the values of the stats written on the character selection screen, the thing is, if the CharacterSlot is always 1, it will just show Helios' stats and not the other characters that I will create (up to 3). 编辑:每个字符插槽是一个可单击的图片框,它会更改写在字符选择屏幕上的统计信息的值,问题是,如果CharacterSlot始终为1,它将仅显示Helios的统计信息,而不会显示其他字符创建(最多3个)。

角色创建和角色选择

How do I work around this? 我该如何解决?

That's what the Sequence object is for. 这就是Sequence对象的用途。

CREATE SEQUENCE Test.CountBy1
    START WITH 1
    INCREMENT BY 1 ;
GO

CREATE TABLE Test.TestTable
     (CounterColumn int PRIMARY KEY,
    Name nvarchar(25) NOT NULL) ; 
GO

INSERT Test.TestTable (CounterColumn,Name)
    VALUES (NEXT VALUE FOR Test.CountBy1, 'Syed') ;
GO

SELECT * FROM Test.TestTable; 
GO

https://msdn.microsoft.com/en-us/library/ff878370.aspx https://msdn.microsoft.com/en-us/library/ff878370.aspx

You can also use NEXT VALUE FOR in the DEFAULT constraint on a column. 您还可以在列的DEFAULT约束中使用NEXT VALUE FOR

So if you want the recount to restart per use, you need to do this. 因此,如果您希望每次使用重新开始计数,则需要执行此操作。 Notice how the command text declares its own parameter for the count that it wants to look up. 请注意,命令文本如何为要查找的计数声明其自己的参数。

CREATE Database Test1
GO
Use Test1
GO
CREATE TABLE TestTable
     (CounterColumn int IDENTITY PRIMARY KEY,
    Name nvarchar(25) NOT NULL,
    SlotNumber INT NOT nULL,
    UserId INT NOT NULL

    )
GO


---- SQL Command Parameters----

Declare @Name nvarchar(25) = 'Tom';
Declare @UserId INT = 1;


----------------------- SQL Command Text------------
Declare @SlotNumber INT;
SELECT @SlotNumber = IsNull(Max(SlotNumber), 0) + 1 FROM TestTable WHERE UserId=@UserId;
INSERT INTO TestTable (Name, SlotNumber, UserId) VALUES (@Name, @SlotNumber, @UserId);

--------Check results -----------

SELECT * FROM TestTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM