Run-time Insert/Update in Stored Procedure from C# application

Posted by Asumal_123 under Sql Server on 1/1/2009 | Views : 9496 | Status : [Member] | Replies : 3
Hi Friends, This is Sandeep. I have a problem that i am facing in my job that needs to be solved as soon as possible. I want to create a stored procedure that can accept a table name,its fields & corresponding field's values to insert/update in the same table at run-time from c# application (front-end). Plz send me the code or the method how to do that?




Responses

Posted by: Poster on: 1/1/2009 [Member] Starter

Up
0
Down
First of all, a stored procedure can't accept a Table name, field name as parameter.

So you will need to write your sql statement in the stored procedure and pass the values to the fields as parameter.

You can also refer to this post http://forums.asp.net/t/1086559.aspx to know about insert and update stored procedure.




Asumal_123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhijit Jana on: 1/1/2009 [Member] [MVP] Bronze

Up
0
Down
Read This Article. This will helps you to create the stored procedure
http://www.sommarskog.se/dynamic_sql.html.
After Creating the Stored procedure, its very simple task to call the stored procedure and passing the fields as parameters using ADO.NET
Cheers!!!
Abhijit

Cheers !
Abhijit

Asumal_123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhijit Jana on: 1/1/2009 [Member] [MVP] Bronze

Up
0
Down
This is a Sample Store Procedure , I have written that you can use it a reference.

-- Sample StoreProcedure for Passing Table Name and

-- Filed name as parameter
-- Author : Abhijit Jana
alter procedure [dbo].[MyProcedure]
@FiledName as varchar(50),
@TableName as varchar(50)
as
Begin
DECLARE @StatementString VARCHAR(1000)
DECLARE @ExecutableString NVARCHAR(1000)
set @StatementString = 'select ' + @FiledName + ' from ' + @TableName
select @ExecutableString =CAST(@StatementString as NVarchar(1000))
EXECUTE sp_executesql @ExecutableString
end


And Execute it for test as
exec [MyProcedure] 'Roll', 'Students'


Cheers !!!
Abhijit

Cheers !
Abhijit

Asumal_123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response