newbie ado net questuon

S

Stephanie

I am trying to get started with ado.net, coming from experience with ado. I
have a simple SQL Server stored procedure which inserts a row and returns
the value of the identity column for the inserted row (seel below).

I have a method in a C# project which is simply going to add a record and
return the identity. I am looking throught the documentation to see how to
do this.

You see I have a dataadapter and a dataset. But I cannot see what methods to
use to execute the sp and get back tha value.

Can anyone point me in the right direction?

public String add(DateTime entryDate, DateTime startTime, DateTime
endTime, String shortDescription, String longDescription)
{
SqlConnection cn = new SqlConnection("Data Source=sstowe;Initial
Catalog=Time;User ID=sa;Password=password;");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

cn.Open;
da. <--- WHAT now?


da.Fill(ds);
return <--- I think I used to know how to work


}


CREATE PROCEDURE Entry_Add
@EntryDate datetime,
@StartTime datetime,
@EndTime datetime,
@Short char(50),
@Description varchar(255),
@ID int OUTPUT

AS
BEGIN
SET NOCOUNT ON
INSERT INTO Entry
(
EntryDate,
StartTime,
EndTime,
Short,
Description
)
VALUES
(
@EntryDate,
@StartTime,
@EndTime,
@Short,
@Description

)
SELECT @ID = SCOPE_IDENTITY()
END
GO
 
C

Cor Ligthert [MVP]

Stephanie,
SqlConnection cn = new SqlConnection("Data Source=sstowe;Initial
Catalog=Time;User ID=sa;Password=password;");
SqlDataAdapter da = new SqlDataAdapter();

You have to tell the DataAdapter what connection and commands it has to use

The easiest way to show it(it can much shorter) is to use for that an
SQLCommand
SqlCommand cmd = newSqlCommand(SqlConnection)

Than you have to tell what type of command the command uses
cmd.CommandType=CommandType.StoredProcedure;

Than the name of the storedprocedure
cmd.Command.Text="sp_Tables";

Than you add that command to your Datadapter
da.SelectCommand = cmd;
DataSet ds = new DataSet();

One of the steps the dataadapter fullfils is to open a connection, therefore
is now this enough (you have to do more by instance to get exceptions and
than it is better to do the open the connection yourself)
da.Fill(ds);

All is directly typed in this message so watch typing errors.

I hope this helps,

Cor
 
W

William \(Bill\) Vaughn

You need to do a bit of reading... there are several good books on this
subject including mine. See www.betav.com for details.
The C# version will walk you through all aspects of calling stored
procedures and returning the (perhaps many) resultsets.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
S

Stephanie

Thanks both of you. I always have a hard time finding what I need to know
from Microsoft documentation.

Thanks!

Stephanie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top