Re: SCOPE_IDENTITY() in c# and no stored procedure...

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



ok, I do not understand how to get a value into a stored procedure from my c#
string, int or whatever datatype. It's much easier for me to do it as you
can see in the "VALUES" part of the command with strings. What does the
VALUES look like when you create a stored procedure for insert if you don't
have the values yet.
When you call the stored procedure:
catDA.InsertCommand = new SqlCommand("GetParameterIdentity", strConn);
catDA.InsertCommand.CommandType = CommandType.StoredProcedure;
How do my values from textboxes, calculations and all over the place get in
there?
>From all I've seen, values look like the field names with an @ sign in front
of them.
An example would probably help.
Thanks for any replies.
Trint

"gabe garza" wrote:

>
> > string strSQL5 = "INSERT INTO tblTravelDetail(MemberId, " +
> > " Comments, " +
> > " TravelEventId, " +
> > " OrderId, " +
> > " ItemID, " +
> > " PeriodID, " + //here is where we do the PeriodID lookup calculation
> > " CreatedDateTime, " +
> > " Operator) " +
> > "VALUES ('" + deiinsertString2 + "', " +
> > " '" + deiinsertString3 + "', " +
> > " '" + eiinsertString1 + "', " +
> > " '" + deiinsertString5 + "', " +
> > " '" + deiinsertString6 + "', " +
> > " '" + deiinsertString7 + "', " +
> > " '" + deiinsertString8 + "', " +
> > " '" + insertString7 + "') "
> > cnn5.Execute(strSQL5);
>
> > strSQL5 = "SELECT SCOPE_IDENTITY()"
> > cnn5.Execute(strSQL5);
>
> Using the same cnn5 connection you can retrieve your SCOPE_IDENTITY() from
> your previous INSERT.
> But it must be the same SqlConnection, don't create another SqlConnection,
> use cnn5 on both Execute()'s.
>
> I think someone pointed out that you should really be using a parameterized
> sql query or even better, do this in a stored procedure and have the
> identity return in an OUTPUT Parameter or as a RETURN from your stored
> procedure.
>
>
> "TrintCSD" <TrintCSD@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:C13DFFCD-546D-458C-8FDA-C4B391120085@xxxxxxxxxxxxxxxx
> > ok, I've been on this for a long time...How can I get the most recent
> > identity from this insert into a string?(with using this code as the
> > insert):
> >
> > string strSQL5 = "INSERT INTO tblTravelDetail(MemberId, " +
> > " Comments, " +
> > " TravelEventId, " +
> > " OrderId, " +
> > " ItemID, " +
> > " PeriodID, " + //here is where we do the PeriodID lookup calculation
> > " CreatedDateTime, " +
> > " Operator) " +
> > "VALUES ('" + deiinsertString2 + "', " +
> > " '" + deiinsertString3 + "', " +
> > " '" + eiinsertString1 + "', " +
> > " '" + deiinsertString5 + "', " +
> > " '" + deiinsertString6 + "', " +
> > " '" + deiinsertString7 + "', " +
> > " '" + deiinsertString8 + "', " +
> > " '" + insertString7 + "'); " +
> > "SELECT SCOPE_IDENTITY()";
> >
> > cnn5.Execute(strSQL5, out recordsEffected, 0);
> >
> > Thanks,
> > Trint
> >
> > --
> > Trinity Smith
> > c#/vb.Net Developer
> > EcoQuest, Intl.
>
>
>
.



Relevant Pages

  • RE: SQL stored procedure executing twice
    ... from one parent record to another. ... Dim stDocName As String ... My pass-thru query syntax going to stored procedure ...
    (microsoft.public.access.modulesdaovba)
  • RE: MS Query "Connections"
    ... handle that with my connection string. ... for using the stored procedure route is that it is faster. ... If you truly want to pass in the query string, ...
    (microsoft.public.excel.programming)
  • Re: XML binding advice
    ... Performance - By writing the SQL statement as a stored procedure, ... execution plan will take into account any indexes you have on the tables ... each piece you add to the string. ...
    (microsoft.public.dotnet.xml)
  • Re: TableAdapter specifing IN in the where clause for list of id
    ... And till now, there products which are huge products, they are using AD-HOC ... Since IN cannot accept a delimited string but ... Muhammad replied that I could use a stored procedure to accomplish this ... specify. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Creating A Custom Collection
    ... You'll have to get away from using multidimensional arrays. ... > string StoredProcedureName, string UserName, string TransactionType ... > stored procedure so it can walk this object to create a SqlCommand ... > send the name of the proc and the parameters object to the data layer ...
    (microsoft.public.dotnet.framework)