Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取插入到数据库中的最后一个值以生成新值_C#_Sql Server - Fatal编程技术网

C# 如何获取插入到数据库中的最后一个值以生成新值

C# 如何获取插入到数据库中的最后一个值以生成新值,c#,sql-server,C#,Sql Server,使用文本区域,我通过单击按钮将值发送到数据库,但我希望postID自动生成我可以怎么做 protected void btnPost_Click(object sender, EventArgs e) { string PostQuery = "INSERT INTO [Post] (PostID,PostDet,Votes,UserID) VALUES('<auto Genatare post ID>','" + TxtPost.Text + "','12','Us001'

使用文本区域,我通过单击按钮将值发送到数据库,但我希望postID自动生成我可以怎么做

protected void btnPost_Click(object sender, EventArgs e)
{
    string PostQuery = "INSERT INTO [Post] (PostID,PostDet,Votes,UserID) VALUES('<auto Genatare post ID>','" + TxtPost.Text + "','12','Us001')";
    dbClass.ConnectDataBaseToInsert(PostQuery);
    Response.Redirect("~/computing.aspx");
}
protectedvoid btnPost\u单击(对象发送方,事件参数e)
{
string PostQuery=“插入[Post](PostID、PostDet、投票、UserID)值(“”,“+TxtPost.Text+”、'12'、'Us001');
ConnectDataBaseToInsert(PostQuery);
Response.Redirect(“~/computing.aspx”);
}

您应该将PostID列设置为标识(1,1)列(在使用MSSQL.server的情况下)。因此,当您将新行插入数据库时,相应的PostID值将由数据库自动生成。您可能使用的其他RDBMS也同样适用

完成上述更改后,您的代码将更改为以下内容:

protected void btnPost_Click(object sender, EventArgs e)
{
    string PostQuery = "INSERT INTO [Post] (PostDet,Votes,UserID) VALUES("TxtPost.Text + "','12','Us001')";
    dbClass.ConnectDataBaseToInsert(PostQuery);
    Response.Redirect("~/computing.aspx");
}

您可以将
PostID
设为
UNIQUEIDENTIFIER
列,然后传入新生成的GUID()

另外,请使用参数化查询以避免SQL注入。特别是如果输入直接来自WEB用户

为此,请将
ConnectDataBaseToInsert
方法更改为不接受SQL文本,而是使用相应参数准备的
SqlCommand


从您对问题的评论来看:PostID应该类似于
PO0001
。然后,正确执行该操作并尊重并发性的唯一方法是生成一个存储过程,该存储过程接受要插入的值,并生成ID本身

为此,请创建一个包含最后一个帖子ID的新表。然后,使用
更新。。。输出
语句以一次性递增并返回。这是对post ID进行原子更新的唯一方法,这样就不会有两个用户创建相同的ID

示例表可发布

Current
=======
0
选择更新和检索当前帖子ID的示例:

-- We need a temp table, because OUTPUT can not output into a single variable
-- This increments Current by one and outputs the value that was set in one go.
-- This prevents simultaneous calls to get the same ID
DECLARE #postID (ID INT)
UPDATE PostIDTable
OUPUT INSERTED.Current INTO #postID
SET
    Current = Current + 1

-- Get the value from the temp table and convert it into the desired format
DECLARE @pID INT = (SELECT TOP 1 ID FROM #postID)
DECLARE @id NVARCHAR(6) = 'PO' + RIGHT('0000' + CONVERT(NVARCHAR, @pID), 4)

-- Do the actual INSERT
INSERT INTO (PostDet, Votes,UserID) VALUES (@id, ..., ...)

如果PostId是自动生成的,则仅执行以下操作:

protected void btnPost_Click(object sender, EventArgs e)
{
    string PostQuery = "INSERT INTO [Post] (PostDet,Votes,UserID) VALUES('" + TxtPost.Text + "','12','Us001')";
    dbClass.ConnectDataBaseToInsert(PostQuery);
    Response.Redirect("~/computing.aspx");
}

从查询中删除PostId,它将生成下一个Id

最好的方法是为表使用自动递增列Id。 请参阅所有主数据库的结构说明。

试试这个

从tablename order by id desc limit 1中选择*


其中id将是您的主键属性名称。

是否将
PostID
创建为数据库中自动生成的列?这是一个数据库问题。将您的
PostId
字段设置为一个标识(如果使用SQL Server,不确定其他数据库),它将自动为您生成值。请添加一个标记,指定您使用的数据库如果PostId在数据库中自动生成,您尝试保存到该列的任何内容都将被丢弃。我的Post ID类似于“PO0001”这样,当我插入一条记录时,在插入另一条记录之前,我想获取最后一个ID,并在单击按钮时将其设置为“PO002”。如果我是此字段的新成员,那么我如何在中使用此代码以及我之前发布的我编码的当前代码,请帮助我。谢谢1)创建我提到的表。2) 创建一个包含我编写的代码的存储过程。3) 在C#中,使用SQL命令调用存储过程,其中包含要作为参数插入的值(CommandType=StoredProcedure)。如果你对此有困难,请问一个新问题。