Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I want to pass an object to a store procedure in SQL-Server 2008. In store procedure I want to take data from my object and then insert that data to a table. Is it possible in SQL-Server? how? Here my object is custom class object.
please help me.
Posted
Updated 29-Sep-13 18:08pm
v2
Comments
Tanumay99 27-Sep-13 1:44am    
Please help me If any one knows.

Refer - Passing an object collection as a parameter into SQL Server stored procedure[^].
Quote:
Assuming SQL Server 2008+, you can do this using a table-valued parameter. First in SQL Server create a table type:
SQL
CREATE TYPE dbo.HobbiesTVP AS TABLE
(
  HobbyID INT PRIMARY KEY,
  HobbyName NVARCHAR(50),
  HobbyTypeID INT
);

Then your stored procedure would say:
SQL
@Hobbies dbo.HobbiesTVP READONLY

In C# (sorry I don't know vb.net equivalent) it would be as follows (but if you just have one UserID, this doesn't need to be part of the collection, does it?):

C#
// as Steve pointed out, you may need to have your hobbies in a DataTable.

DataTable HobbyDataTable = new DataTable();
HobbyDataTable.Columns.Add(new DataColumn("HobbyID"));
HobbyDataTable.Columns.Add(new DataColumn("HobbyName"));
HobbyDataTable.Columns.Add(new DataColumn("HobbyTypeID"));

// loop through objHobbyCollection and add the values to the DataTable,
// or just populate this DataTable in the first place

using (connObject)
{
    SqlCommand cmd = new SqlCommand("dbo.User_Update", connObject);
    cmd.CommandType = CommandType.StoredProcedure;
    // other params, e.g. @UserID
    SqlParameter tvparam = cmd.Parameters.AddWithValue("@Hobbies", HobbyDataTable);
    tvparam.SqlDbType = SqlDbType.Structured;
    // ...presumably ExecuteNonQuery()
}

Also look at - Table Value Parameters in SQL Server 2008 and .NET (C#)[^]
 
Share this answer
 
v2
Comments
Tanumay99 30-Sep-13 0:07am    
I want to pass my custom class object, Not Data Table Object. Please help me.
Sorry, I guess you can't do that. Because SQL will not understand your custom objects.

So, please format your object as a DataTable and do as suggested in the answer.
Yes, it's possible - just use a parameter (or more than one)

SQL stored procedure parameters[^]
 
Share this answer
 
Comments
Tanumay99 27-Sep-13 2:13am    
actually I want to pass object, with its multiple property. Not a single value or multiple value as parameter in store procedure. please try to under stand my question.
ALTER PROC usp_Getdetails
(
       @Parameter       INT,
       @Id		INT,
       @BookName	VARCHAR(100),
       @Price		INT,
       @CreatedBy       INT, 
       @LastUpDatedBy   INT, 
       @return		INT	= NULL OUTPUT,
       @errorId		INT	= NULL OUTPUT,
       @errorMessage	VARCHAR(1000) = NULL OUTPUT  
  )    
  AS
  SET NOCOUNT ON 
  BEGIN
  BEGIN TRY
  BEGIN TRAN
  
  IF NOT EXISTS (SELECT 1 FROM Getdetails where Id = @Id)
  BEGIN
  
  IF @Parameter =1
  
  BEGIN
  
  INSERT Getdetails
  (
   BookName,    Price,      ActiveYN,   
   DeletedYN,   CreatedOn,  CreatedBy,  LastUpDatedOn
  )
  VALUES
  (  
  @BookName,	@Price,      1,
  0,            GETDATE(),   @CreatedBy,  GETDATE()
  )
  
  END   
   
  END   
   
  ELSE
     
  BEGIN
        
  IF @Parameter =2
        
     BEGIN
     
     UPDATE Getdetails
     
     SET DeletedYN = 1,
            LastUpDatedOn = GETDATE()
            WHERE Id=@Id
         
    END
    
    ELSE
   
    IF @Parameter =3
   
    BEGIN
    UPDATE Getdetails
    SET
          BookName	=	@BookName,
          Price		=	@Price,
          ActiveYN	=       1,
          DeletedYN	=	0,
          LastUpDatedBy	=	@LastUpDatedBy
          
          WHERE Id=@Id
       END
       
   --   SELECT @errorMessage = 'Record Already Exists'
        
        END
        COMMIT TRAN
        END TRY
        BEGIN CATCH
        ROLLBACK TRAN
        
        SELECT @return              = -1,
                     @errorId       =  ERROR_NUMBER(),
                     @errorMessage  =  ERROR_MESSAGE()
    RETURN -1
    END CATCH
               --SELECT @errorMessage = 'Record Inserted Successfully'
       
        
     END
     SET NOCOUNT OFF
 
Share this answer
 
Comments
Tanumay99 28-Sep-13 1:56am    
Your answer shows only a store procedure. but my question is how we pass an object, not data.
And how we store data from the object in store procedure. please try to under stand my question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900