zrabzdn
zrabzdn

Reputation: 995

Call stored procedure from c#

I created a test application with Thread and call stored procedure. If I call stored procedure from console application stored procedure executes, but does not insert values into test table. I'm using a DbContext. If I call procedure in SQL Server Management Studio then value adds to test table. Where is error?

My console application for thread call stored procedure:

 static void Main(string[] args)
            {
             ModelContext context = new ModelContext();
             Thread t = new Thread(() => ThreadTestProcedure(context));
             t.Start();
    }

Thread call method:

 private static void ThreadTestProcedure(ModelContext сontext)
            {
                try
                {                
                        context.RunTestProcedure(1000, "Test")
                }
                catch (Exception ex)
                {

                }

                Console.WriteLine();
                Console.ReadKey();
            }

My stored procedure for insert values to test table:

CREATE PROCEDURE TestProcedure
   @Int1 INT,
   @SrtingValue nvarchar(250)
AS        
   IF NOT EXISTS (SELECT * FROM sys.objects 
                   WHERE object_id = OBJECT_ID(N'[dbo].[ASSIGNMENTS_DELETED]') 
                   AND type in (N'U'))
   BEGIN
      CREATE TABLE ASSIGNMENTS_DELETED
             (ID INT PRIMARY KEY (ID) identity(1,1),
              Assigment int, 
              StringValue nvarchar(250)
             )   
   END 

   WHILE 1=1 
   BEGIN
      BEGIN TRY
      BEGIN TRANSACTION
          insert into ASSIGNMENTS_DELETED
             select @Int1, @SrtingValue            

          COMMIT TRANSACTION
     END TRY
     BEGIN CATCH
          IF XACT_STATE() <> 0 
              ROLLBACK TRANSACTION
          RAISERROR ('it broke', 16, 1)
     END CATCH
 END

Upvotes: 0

Views: 253

Answers (2)

Ajay
Ajay

Reputation: 6590

Remove WHILE 1=1 from your stored procedure. It goes in infinite loop.

Upvotes: 2

Leigh Ciechanowski
Leigh Ciechanowski

Reputation: 1317

Your stored procedure has an infinite loop WHILE 1=1 ??? remove that.

Upvotes: 3

Related Questions