Dong Lam Trien

Dong Lam Trien

  • 830
  • 986
  • 147.2k

Why insert new have must "SET IDENTITY_INSERT TableName ON"

Jun 10 2014 11:36 PM
I have a few for example insert new records, when the store must have "SET IDENTITY_INSERT TabeleName ON"; and "SET IDENTITY_INSERT TabeleName OFF"; other examples do not need 2 lines ON and OFF, if I wanted to give my 2 lines to adjust what ? you see the code below:

strSQL = "SET IDENTITY_INSERT TabUsername ON ";
strSQL = strSQL + "INSERT INTO TabUsername(USERNAME";
strSQL = ...
strSQL = strSQL + "SET IDENTITY_INSERT TabUsername OFF";
...
SqlCommand cmd = new SqlCommand(strSQL,cnn);
  try
  {
     cmd.ExecuteNonQuery();
     cnn.Close();
  }
....

Answers (3)