Stored Proc and Primary Key

Results 1 to 2 of 2

Thread: Stored Proc and Primary Key

  1. #1
    Join Date
    Dec 1969

    Default Stored Proc and Primary Key

    I am using a stored procedure to insert a record in SQL Server. The primary key is created at the time of the insert. Can I return the key if the insert succeeds? The initial insert creates a record in an author table. Once that is done I want to redirect the user to a page where they can start inserting rows for the books by that author. I need the primary key for the new author row to create the publications row. I am using C#. Code for the insert: <BR><BR>SqlConnection objConn = null; <BR>SqlCommand objCmd = null; <BR>strDBConn = ConfigurationSettings.AppSettings["ConnectionString"]; <BR>objConn = new SqlConnection( strDBConn ); <BR><BR>objCmd = new SqlCommand( ); <BR>objCmd.Connection = objConn; <BR>DataSet objDataSet = new DataSet( ); <BR>SqlDataAdapter objAdptr = new SqlDataAdapter( ); <BR>objCmd.CommandText = "p_insertAuthor"; <BR>objCmd.CommandType = CommandType.StoredProcedure; <BR>SqlParameter objParam; <BR>objParam = objCmd.Parameters.Add("@name",sAuthor ); <BR>objParam = objCmd.Parameters.Add("@desc", sDesc); <BR>objParam = objCmd.Parameters.Add("@country", sCountry); <BR>if( objConn.State != ConnectionState.Open){ <BR>objConn.Open( ); <BR>} <BR>objAdptr.SelectCommand = objCmd; <BR>SqlDataReader objReader; <BR>try{ <BR>objReader = objCmd.ExecuteReader(); <BR>objReader.Close( ); <BR>objConn.Close( ); <BR><BR>} <BR>catch (SqlException objError) <BR>{ <BR>objConn.Close( ); <BR>Response.Write(objError.Message); <BR>}

  2. #2
    Join Date
    Dec 1969

    Default FAQID=79 (eop)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts