Hi Frineds, this time i want to share ,How to Get ObjectId From the Stored Procrdure in C#.
Here i have Written Both Stored Procedure and CSharp Code . Please look it and if you like this post. just comment it .
Here i have Written Both Stored Procedure and CSharp Code . Please look it and if you like this post. just comment it .
public class DataAccess { private SqlConnection conn; public DataAccess() { string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" // I used for SQLServer2005 .For other Databases check with "http://www.connectionstrings.com" conn = new SQLConnection(connectionString); } // To Get Saved object Id public int Save_Details(string parameter1, string paramter2,....., string parameterN) { int returnObjectId; conn.Open(); SqlCommand cmd = new SqlCommand("SP_Insert", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@parameter1", parameter1); cmd.Parameters.AddWithValue("@parameter2", parameter2); //. //. //. //. cmd.Parameters.AddWithValue("@parameterN", parameterN); cmd.Parameters.Add("@ObjectId", SqlDbType.Int, 0, "Id"); cmd.Parameters["@ObjectId"].Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); returnObjectId = (int)cmd.Parameters["@ObjectId"].Value; conn.Close(); if (returnObjectId > 0) { return returnObjectId;} else {return 0;} } }
--Stored Procedure code SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[SP_Insert] @parameter1 nvarchar(50), @parameter2 nvarchar(50), -- . -- . -- . @parametern nvarchar(50), @Object_Id int OUTPUT AS BEGIN INSERT INTO [dbo].[TableName] ([Parameter1] ,[Parameter2] ,[ParameterN]) VALUES (@parameter1, @parameter2, @parametern) END select @Object_Id =@@Identity
No comments:
Post a Comment