创建记录后检索ID [英] Retrieve ID after the record has been created

查看:87
本文介绍了创建记录后检索ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我对开发非常陌生,因此不胜感激.

我现在正在尝试找出如何使用ASP.net中使用c#编译的Web表单添加记录来从我的数据库中检索ID.

场景:创建订单,创建后页面将回发订单号.

亲切的问候,

JD

Good day,

I am very new to development, so would appreciate any help.

I am curently trying to work out how to retrieve an ID from My database after I have added a record using a webform compiled of c# in ASP.net.

scenario : Create an order, and upon creation the page will post back the Order Number.

Kind regards,

JD

推荐答案

可以,使用SCOPE_IDENTITY()
在ADO.NET中,可以一次发送多个查询或调用存储过程.

要快速入门,请尝试:
Yes possible, using SCOPE_IDENTITY()
In ADO.NET, one can send multiple queries in one go or call a stored procedure.

For quick start try:
myCommandForData.CommandText = "INSERT INTO [Country] (CName) VALUES (@CNameValue); SELECT SCOPE_IDENTITY()";
int newCountryID = (int)myCommandForData.ExecuteScalar();



在SP中处理它并调用SP是使用它的更好方法.

我建议您在这里在ADO.NET上阅读:
看看这些:
MSDN:ADO.NET [ MSDN:使用ADO.NET访问数据 [ ^ ]

特定于SP,请转到此处:
MSDN:HOW TO:使用Visual C#.NET在ASP.NET中调用SQL Server存储过程 [ MSDN:HOW TO:使用ADO.NET和Visual C#.NET调用参数化存储过程 [ ADO.NET:使用存储过程 [



Handling it in an SP and calling an SP would be a better way to use it.

I would suggest you to read on ADO.NET here:
Look at these:
MSDN: ADO.NET[^]
MSDN: Accessing Data with ADO.NET[^]

SP specific, go here:
MSDN: HOW TO: Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET[^]
MSDN: HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
ADO.NET : Using Stored Procedures[^]




这取决于您如何访问数据层/数据库:

Hi,

This depends upon how you access the datalayer / your database:

  • If you use dbml / Linq to SQL, the best way would be that you use a Guid as ID and generate the Guid in your code.
  • If you use direct SQL statements, you can get the last inserted identity by executing the TSQL command scope_identity() (see: http://msdn.microsoft.com/de-de/library/ms190315.aspx[^])
  • If you use stored procedures, you can add a return value to the SP which returns you the scope_identity() (see: http://msdn.microsoft.com/de-de/library/ms190315.aspx[^])


This[^] will get you started.


这篇关于创建记录后检索ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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