0

I created a database table both on production and staging server with same columns and constraints.

CREATE TABLE dbo.test
(
    [COLUMN1] [NVARCHAR](255) NOT NULL,
    [COLUMN2] [NVARCHAR](255) NULL,

    PRIMARY KEY CLUSTERED ([COLUMN1] ASC)
                WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                      ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

I have created an ASP.NET MVC 5 project in C#. Using the "Generate Database from Model" feature, I have created a model view controller.

I create a stored procedure in the SQL Server backend which checks server name whether dev or prod and then if dev, copies values in dev to prod (basically sync tables from dev to prod). Same is true for prod.

CREATE PROCEDURE [dbo].[uspSyncTables]
AS
BEGIN TRY
    SET NOCOUNT ON
    DECLARE @truncateEnvironment VARCHAR(500),
            @syncFromEnv VARCHAR(50), @syncToEnv VARCHAR(50),
            @delTable VARCHAR(500), @tablename NVARCHAR(1000)

    SET @tablename='mydb.dbo.test'

    IF(@@SERVERNAME = 'Prod\INSTANCE01')
    BEGIN
        SET @syncFromEnv = '[prod\INSTANCE01]'
        SET @syncToEnv = '[dev\INSTANCE01]'
    END
    ELSE
    BEGIN
        SET @syncFromEnv = '[dev\INSTANCE01]'
        SET @syncToEnv = '[Prod\INSTANCE01]'
    END

    -- Variables 
    DECLARE @sql NVARCHAR(MAX), @message NVARCHAR(4000)

    BEGIN
        SET @delTable = @syncToEnv+'.'+  @tablename
        SET @sql='DELETE FROM '+ @delTable
        PRINT @sql
        EXEC(@SQL)
    END

    SET @sql = 'INSERT INTO ' +@delTable + ' SELECT * FROM ' + @syncFromEnv +'.'+ @tablename
    EXEC(@SQL)
    PRINT 'Rows inserts into : ' + CONVERT(VARCHAR, @@ROWCOUNT)

END TRY
BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT

    SELECT  
        @ErrorMessage = ERROR_MESSAGE(), 
        @ErrorSeverity = ERROR_SEVERITY(), 
        @ErrorState = ERROR_STATE();  

    RAISERROR (@ErrorMessage,  @ErrorSeverity, @ErrorState)
END CATCH

I created a submit button in ASP.NET MVC (Index.html) and in the controller, I am calling this stored procedure.

I am getting an error

System.Data.SqlClient.SqlException: MSDTC on server 'PROD\INSTANCE01' is unavailable

I am thinking this maybe because I call remote table to sync.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2726975
  • 1,285
  • 3
  • 17
  • 26
  • 2
    This is not C# code issue... looks like there is an issue on the database server... Quick googling suggested [this](https://support.microsoft.com/en-sg/help/962313/msdtc-on-server-xxxx-is-unavailable) – Chetan Apr 08 '19 at 01:02
  • I tried this and I still have the same issue. – user2726975 Apr 08 '19 at 19:15
  • Also you can read how to solve it here: https://stackoverflow.com/questions/29414250/msdtc-on-server-server-is-unavailable. Make sure that `Distributed Transaction Coordinator` service is running on DB server. – Tetsuya Yamamoto Apr 09 '19 at 02:42

0 Answers0