question

palum avatar image
palum asked

C# Code to execute a parameterized stored procedure

SqlConnection MyConnection=(SqlConnection)Dts.Connections["ADO Source"].AcquireConnection(null); SqlCommand Storproc = new SqlCommand(); Storproc.Connection = MyConnection; Storproc.CommandType = CommandType.StoredProcedure; Storproc.CommandText = "StudentProcedure"; SqlParameter Parameter1 = new SqlParameter("@Name", SqlDbType.VarChar); SqlParameter Parameter2 = new SqlParameter("@ID", SqlDbType.VarChar); Storproc.Parameters.Add(Parameter1); Storproc.Parameters.Add(Parameter2); MyConnection.Open(); Storproc.ExecuteNonQuery(); MyConnection.Close(); I am trying to execute a stored procedure which accepts 2 parameters from SSIS Script task. While execute the above code it giving an error the stored procedure 'StudentProcedure' has no parameter named Parameter2. Please help me how to pass a parameters in C#. Thanks
ssisstored-proceduresc#paramaters
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Pavel Pawlowski avatar image Pavel Pawlowski commented ·
Only a question related to the stored proc execution? Why you are executing the stored proc inside SSIS using the Script Task or Script Component? Much easier you can use **Execute SQL Task** (Control Flow) or **OLE DB Command** (Data Flow). In your case you mentioned Script Task, so you are in Control Flow, so Much Easier you would have to execute the stored proc using the Execute SQL Task.
1 Like 1 ·
mjharper avatar image mjharper commented ·
I think you need to assign a value to each of those parameters, before adding it - something like: Parameter1.Value = "myName"; I'm not sure that's the problem though as I don't think it quite fits with the error message you're getting.
0 Likes 0 ·

1 Answer

·
Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
I would pass the parameters with AddWithValue instead of declaring them and adding them. Like this: SqlConnection MyConnection=(SqlConnection)Dts.Connections["ADO Source"].AcquireConnection(null); SqlCommand Storproc = new SqlCommand(); Storproc.Connection = MyConnection; Storproc.CommandType = CommandType.StoredProcedure; Storproc.CommandText = "StudentProcedure"; Storproc.Parameters.AddWithValue("@Name","SomeValue"); Storproc.Parameters.AddWithValue("@ID","SomeOtherValue"); MyConnection.Open(); Storproc.ExecuteNonQuery(); MyConnection.Close(); In your code you declare both parameters as VarChar. Is the parameter \@ID really a VarChar?
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

palum avatar image palum commented ·
parameter @ID is VarChar but it is a System.Object
0 Likes 0 ·
palum avatar image palum commented ·
When i change the @ID to a variable to type String it executes. when i change the @ID to a System.Object variable in SSIS, I am getting the folling error: System.InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.