How to put SET IDENTITY_INSERT dbo.myTable ON statement

前端 未结 2 2138
难免孤独
难免孤独 2020-12-17 17:44

What I need to do is have a SET IDENTITY_INSERT dbo.myTable ON statement, what\'s the syntax of using the above statement in a c# app?

2条回答
  •  清歌不尽
    2020-12-17 18:26

    It's just the same as any other bit of SQL:

    using (var connection = new SqlConnection("Connection String here"))
    {
        connection.Open();
        var query = "SET IDENTITY_INSERT dbo.MyTable ON; INSERT INTO dbo.MyTable (IdentityColumn) VALUES (@identityColumnValue); SET IDENTITY_INSERT dbo.MyTable OFF;";
        using (var command = new SqlCommand(query, connection)
        {
            command.Parameters.AddWithValue("@identityColumnValue", 3);
            command.ExecuteNonQuery();
        }
    }
    

提交回复
热议问题