如何在插入一条记录时,同时取得这条记录的 UserID值  
UserID是标识列。  
需要代码。

解决方案 »

  1.   

    ++++
    SELECT @@IDENTITY 
      

  2.   


    是不是这样?" insert into UserInfo values('{0}','{1}','{2}','{3}','{4}');select @@identity;"  这样没用啊 ········
      

  3.   


    是不是这样?" insert into UserInfo values('{0}','{1}','{2}','{3}','{4}');select @@identity;" 这样没用啊 ········
      

  4.   

    很常见的功能,自己看
    http://frieboy168.blog.163.com/blog/static/3554449120093235646809/
      

  5.   


    你是使用那个方法执行的这个SQL
      

  6.   

    没听明白您说的。你现在不是用电脑啊,   帮我解决下,我就不知道再调用方法后不知道怎么显示UserId了······
      

  7.   


    这么写没有问题啊。  返回值接收一下就是你想要的结果啊。 
    实在不行你用最笨的方法, UserInfo 表倒序取第一行记录。SELECT TOP 1 userID FROM UserInfo ORDER BY userID
    这样总不会错了吧。 
      

  8.   

    SqlConnection conn = new SqlConnection(@"");
                SqlCommand com = conn.CreateCommand();
                com.CommandText = string.Format(@"insert into UserInfo values('{0}','{1}','{2}','{3}','{4}')
                select @@identity as userId",1,2,3,4,5);
                SqlDataReader reader = com.ExecuteReader();
                if (reader != null)
                {
                    using (reader)
                    {
                        while (reader.Read())
                        {
                            int userId = int.Parse(reader["userId"].ToString());
                        }
                    }//using
                }//if