Returning a cursor from a stored procedure

  • Thread starter Thread starter arne
  • Start date Start date
A

arne

How do I capture a resultset from a stored procedure with
ado.net code?

ALTER PROCEDURE dbo.sp_ins_contacts
(
@FName nvarchar(50),
@LName nvarchar(50),
@Initials nvarchar(50),
@IntakeDate smalldatetime,
@SSN nchar(10),
@Advisor nvarchar(50 )
AS
SET NOCOUNT OFF;
INSERT INTO tbl_Contacts(FName, LName, Initials,
IntakeDate, SSN, Advisor) VALUES (@FName, @LName,
@Initials, @IntakeDate, @SSN, @Advisor);
SELECT ContactID, FName, LName, Initials,
IntakeDate, SSN, Advisor FROM tbl_Contacts WHERE
(ContactID = @@IDENTITY)
 
Hi Arne,

The SqlCommand class can help. You can use it together with the
SqlDataAdapter class. Set the SqlCommand's command type to
CommandType.StoredProcedure and bind it to a SqlDataAdapter, and then you
can fill the query result into a dataset. Here is a code snippet for sample:

SqlConnection sqlcnn = new SqlConnection("<Your connection string here.>");

SqlCommand sqlcom = new SqlCommand();

sqlcom.CommandType = CommandType.StoredProcedure;

sqlcom.CommandText = "dbo.sp_ins_contacts";

sqlcom.Connection = sqlcnn;

SqlDataAdapter sda = new SqlDataAdapter(sqlcom);


DataSet ds = new DataSet();

sda.Fill(ds);

Does this answer your question? If there is anything unclear, please reply
to this post.

Kevin Yu
========
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top