none
Insert into database through stored proc in LINQ to SQL???? RRS feed

  • Question

  • Hi,
           How can i insert the data into database through stored procedure in LINQ to SQL????


      

    Wednesday, July 22, 2009 12:51 PM

Answers

  • Hi,

    I just write an article that can help you.
    I explain how to use the created stored procedure using the dbml designer which will generate the Insert method for you.
    Insert using Stored Procedure and Linq to Sql [http://nadege.deroussen.net/?p=287]

    Hope it helps. Tell me if it is not clear.

    Nadege DEROUSSEN http://nadege.deroussen.net
    • Marked as answer by abhi0410 Thursday, July 23, 2009 12:09 PM
    Wednesday, July 22, 2009 7:30 PM
  • Great.

    If I resuse the example in my article, you have to do the following :
    If you want to use an output parameter, add it to your stored procedure like this :

    CREATE PROCEDURE dbo.InsertCustomer
    @id nchar(5),
    ...
    @leretour nchar(10) OUTPUT
    AS

    and update your method in dbml designer. You will see a new reference parameter. Just use it like this :

    string retour = string.Empty;
    int customerid = context.InsertCustomer("NAD12", "My Company", "Nadege", "Owner", "My Adress", "My City", "FR", "124567", "FRANCE", "1234567890", "9876543210", ref retour);
    Console.WriteLine("Adding user to database");
    Console.WriteLine(string.Format("Retour = {0}", retour));

    Is this what you need ?


    Nadege DEROUSSEN http://nadege.deroussen.net
    • Marked as answer by abhi0410 Thursday, July 23, 2009 12:08 PM
    Thursday, July 23, 2009 6:42 AM

All replies

  • Hello,

    I think you could find the solution in this article : http://weblogs.asp.net/scottgu/archive/2007/08/23/linq-to-sql-part-7-updating-our-database-using-stored-procedures.aspx

    Search paragraph "Step 3: Doing Order Inserts with a SPROC" and you'll find the solution.

    Hope it helps
    Nadege DEROUSSEN http://nadege.deroussen.net
    Wednesday, July 22, 2009 2:36 PM
  • Hi Nadege,
             i did check that post...but there Scott didn't describe how to write code for inserting through stored proc. He jus showed the images but didn't write the code to insert...

    So could you help me with the code???

    Thanks..
    Wednesday, July 22, 2009 3:40 PM
  • Hi,

    I just write an article that can help you.
    I explain how to use the created stored procedure using the dbml designer which will generate the Insert method for you.
    Insert using Stored Procedure and Linq to Sql [http://nadege.deroussen.net/?p=287]

    Hope it helps. Tell me if it is not clear.

    Nadege DEROUSSEN http://nadege.deroussen.net
    • Marked as answer by abhi0410 Thursday, July 23, 2009 12:09 PM
    Wednesday, July 22, 2009 7:30 PM
  • Hi
        Thanks for the help....i was basically searching for this part.


    NorthwindDataContext context = new NorthwindDataContext();
    context.InsertCustomer(”NAD09″, “My Company”, “Nadege”, “Owner”, “My Adress”, “My City”, “FR”, “124567″, “FRANCE”, “1234567890″, “9876543210″);
    context.SubmitChanges();


    N suppose i have a output parameter in stored proc then how would i handle that???i mean to say that after writing
    context.SubmitChanges()   what should i write to get the output parameter and store it in variable?????

    Thanks.


    Thursday, July 23, 2009 5:24 AM
  • Great.

    If I resuse the example in my article, you have to do the following :
    If you want to use an output parameter, add it to your stored procedure like this :

    CREATE PROCEDURE dbo.InsertCustomer
    @id nchar(5),
    ...
    @leretour nchar(10) OUTPUT
    AS

    and update your method in dbml designer. You will see a new reference parameter. Just use it like this :

    string retour = string.Empty;
    int customerid = context.InsertCustomer("NAD12", "My Company", "Nadege", "Owner", "My Adress", "My City", "FR", "124567", "FRANCE", "1234567890", "9876543210", ref retour);
    Console.WriteLine("Adding user to database");
    Console.WriteLine(string.Format("Retour = {0}", retour));

    Is this what you need ?


    Nadege DEROUSSEN http://nadege.deroussen.net
    • Marked as answer by abhi0410 Thursday, July 23, 2009 12:08 PM
    Thursday, July 23, 2009 6:42 AM
  • Hi
        it should be ref retour  or ref leretour   in context.InsertCustomer() ???

    because i see the in stored proc area u have defined leretour....i hope i am understanding in correct direction...



    Thursday, July 23, 2009 9:11 AM
  • In context.insertCustomer(), it should be retour, the name of your variable in your code. No matter the name in the stored proc.
    Hi
        it should be ref retour  or ref leretour   in context.InsertCustomer() ???

    because i see the in stored proc area u have defined leretour....i hope i am understanding in correct direction...




    Nadege DEROUSSEN
    My blog : http://nadege.deroussen.net
    (Was my post helpful? If so, please indicate)
    Thursday, July 23, 2009 12:04 PM
  •  okies ,,,Thanks....i understood why wrote like that...i got little confused there...:-)  thanks.
    Thursday, July 23, 2009 12:08 PM
  • No problem.
    Can you just indicate if my post was helpful, if it was ?

    Thanks.
    Nadege DEROUSSEN
    My blog : http://nadege.deroussen.net
    (Was my post helpful? If so, please indicate)
    Thursday, July 23, 2009 12:45 PM
  • I already did it on msdn,,,,
     or
    Do you mean to say about your article ????
    Thursday, July 23, 2009 12:50 PM
  • Just on msdn, it's ok. I have not seen it.
    Thanks.
    Nadege DEROUSSEN
    My blog : http://nadege.deroussen.net
    (Was my post helpful? If so, please indicate)
    Thursday, July 23, 2009 1:43 PM