从sql server获取最后生成的标识 [英] Get the last generated identity from sql server

查看:85
本文介绍了从sql server获取最后生成的标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个网络基地停车系统



i希望从sql server数据库中检索新插入或最新生成的身份。

:填写并将新插入的数据保存到表格后




i想要从浏览器显示该标识值。



我怎么想这样做。



更新/插入新数据到数据库没有问题,



顺便说一句,我使用存储过程来添加数据。



这是点击按钮时的代码


i want to develop a web base parking system

i want to retrieve the newly inserted or latest generated identity from sql server database.

after filling up and saving the new inserted data to the table

i want to display that identity value from the browser.

how may i suppose to do that.

updating/ inserting new data to database has no problem,

btw, i use stored procedure for adding data.

this is the code upon clicking button

protected void Button1_Click(object sender, EventArgs e)
    {
        string cs = "data source=.; database=palistuhan; integrated security=SSPI";
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("addpark", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@plate", TextBox1.Text);
            cmd.Parameters.AddWithValue("@timein", TextBox1.Text);
            cmd.Parameters.AddWithValue("@timeout", 0);
            cmd.Parameters.AddWithValue("@cost", 0);
            cmd.Parameters.AddWithValue("@date", 0);
            SqlParameter outputParameter = new SqlParameter();
            outputParameter.ParameterName = "@ticket";
            outputParameter.SqlDbType = System.Data.SqlDbType.Int;
            outputParameter.Direction = System.Data.ParameterDirection.Output;
            cmd.Parameters.Add(outputParameter);

            con.Open();
            cmd.ExecuteNonQuery();

        }

    }





i尝试其他代码来自其他论坛。但似乎它返回空值。

请帮助我。谢谢





删除了SHOUTING,添加了代码块 - OriginalGriff [/ edit]





这是我的罢工程序



USE [palistuhan]

GO

/ ******对象:StoredProcedure [dbo]。[addpark]脚本日期:04/12/2015 12:55:24 ****** /

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER程序[dbo]。[addpark ]

@ticket int out,

@plate nchar(10),

@timein float,

@超时浮动,

@date nchar(10),

@cost浮动

as

开始

选择@ticket = scope_identity()

插入停车值(@ plate,@ timein,@ timeout,@ date,@ cost)

return @机票

结束



i tried other code from other forum. but it seems as it returns null value.
please help me. thanks


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]


and this is my strored procedure

USE [palistuhan]
GO
/****** Object: StoredProcedure [dbo].[addpark] Script Date: 04/12/2015 12:55:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[addpark]
@ticket int out,
@plate nchar(10),
@timein float,
@timeout float,
@date nchar(10),
@cost float
as
begin
Select @ticket = scope_identity()
insert into parking values (@plate, @timein, @timeout, @date,@cost)
return @ticket
end

推荐答案

首先查看你的SP,看看它究竟是做什么的。专注于输出参数。



如果一切正常(你可以在SSMS中测试它来检查)那么你需要开始查看你使用的代码返回值 - 您没有显示...
Start by looking at your SP and see exactly what it does. Concentrate on the output parameter.

If that's all ok (and you can test it in SSMS to check) then you need to start looking at the code where you use the return value - which you don't show...


查看六个不同的以获取最后一个身份 - 6种不同的方法来获取SQL中的当前身份值 [ ^ ]。
Check out six different to get the last identity - 6 Different Ways To Get The Current Identity Value in SQL[^].


签出以获得最后一个身份 @@ identity
Check out to get the last identity @@identity


这篇关于从sql server获取最后生成的标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆