CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
2008-01-15 16:52
2061 查看
Listing 1 – Simple WebForm used for the Test
<form id="Form1" method="post" runat="server"> <p>First Name<asp:TextBox id="FirstName" runat="server"></asp:TextBox></p> <p>Last Name<asp:TextBox id="LastName" runat="server"></asp:TextBox></p> <p><asp:Button id="btnAddRecord" runat="server" Text="Add"></asp:Button></p> <p><asp:Label id="lblLastRecordAdded" runat="server"></asp:Label></p> </form>
We will create a database named DummyDB and a table with the name Employees with Employeeid, FirstName and LastName as column names under the database. The required SQL Script is given below for referenceListing 2 - Table Creation Script CREATE TABLE Employees (Employeeid int IDENTITY (1, 1) NOT NULL, FirstName nvarchar (50), LastName nvarchar (50))Now that we have the table in place, we will see how to get the Id of newly added record through the code. We will use the SCOPE_IDENTITY() function for our requirement. After inserting the new record we simply return the value, as shown below.Listing 3 – Use SCOPE_IDENTITY() -- INSERT the new record INSERT INTO Employees(FirstName, LastName) VALUES(@FirstName, @LastName) -- Now return the EmployeeId of the newly inserted record SELECT SCOPE_IDENTITY()The ExecuteScalar() method can be used in ASP.NET since it returns the first column of the first row in the result set returned by the query.Listing 4 – Use ExecuteScalar()Below is the code in VB.NET and C#. |
Code |
string strconnection, strsqlinsert;SqlConnection conn;SqlCommand cmd;string Employeeid;private void btnAddRecord_Click(object sender, System.EventArgs e){strconnection = "server=localhost;uid=sa;password=;database=DummyDB";strsqlinsert = "Insert into Employees ( ";strsqlinsert += "FirstName ,LastName";strsqlinsert += ")";strsqlinsert += " values (";strsqlinsert += "@FirstName,@LastName";strsqlinsert += ")";strsqlinsert += "; SELECT SCOPE_IDENTITY() ; ";conn = new SqlConnection(strconnection);cmd = new SqlCommand(strsqlinsert, conn);cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = FirstName.Text;cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = LastName.Text;cmd.Connection.Open();Employeeid = cmd.ExecuteScalar().ToString();cmd.Connection.Close();lblLastRecordAdded.Text = Employeeid;} Figure 1NOTEAlthough SCOPE_IDENTITY and @@IDENTITY return the last identity value generated for any table in the current session, SCOPE_IDENTITY returns value only in current scope whereas @@IDENTITY returns value across all scopes.IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope. |
相关文章推荐
- How to get the Values of Selected Row from a Gridview using ASP.NET
- How to get the Current Controller Name, Action, or ID in ASP.NET MVC
- how to get geometry type of layer using IMapServer3 and IMapLayerInfo? (C#)
- How to Get First and Last Day of a Week in SQL Server
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
- how to get geometry type of layer using IMapServer3 and IMapLayerInfo? (C#)
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- How to configure the Virtual Path of ASP.NET Development Server to be “Root”
- How to Get First and Last Day of a Month in SQL Server
- How to Determine the Version of Oracle XML Publisher for Oracle E-Business Suite 11i and Release 12 (Doc ID 362496.1)
- how to config the tftpd-hpa server in the ubuntu using for remote tftp client '-p' and '-r' command
- How to find out the Sql Server version and service pack
- how to get all controller attribute action and parameter in asp.net mvc
- How To Turn Up Logging on the Siebel Web Server Extension in Siebel Versions 7.x and 8.x? [ID 477185
- How-to find the SQL that using lots of temp tablespace in Oracle
- How to change the network IP addresses of SQL Server failover cluster instances-[From MS]
- Qt How to get the width and height of a widget
- zhuanzai: AJAX: How to create a "Processing" modal window using UpdateProgress and ModalPopup ASP.net AJAX controls
- How To Download The Latest Updates And Patches Using 11.2.0.2 OUI [ID 1295074.1]