Getting @@IDENTITY from TableAdapter

前端 未结 7 2169
自闭症患者
自闭症患者 2020-12-19 20:21

I am trying to complete a seemingly simple task that has turned into a several hour adventure: Getting @@Identity from TableAdapter.Insert().

相关标签:
7条回答
  • 2020-12-19 20:50

    One way is to run a select query after the insert command. A good way is to wrap the original command like this:

        public int WrapInsert(Parameters)
        {
            .....
            int RowsAffected = this.Insert(..Parameters..);
            if ( RowsAffected > 0)
            {
                try
                {
                    SqlCommand cm = this.Connection.CreateCommand();
                    cm.CommandText = "SELECT @@IDENTITY";
                    identity = Convert.ToInt32(cm.ExecuteScalar());
                }
                finally
                {
                    ....
                }
            }
            return RowsAffected;
        }
    
    0 讨论(0)
提交回复
热议问题