【问题标题】:retrieve unique id for just added row检索刚刚添加的行的唯一 ID
【发布时间】:2013-03-31 09:09:07
【问题描述】:

我是 asp.net 的新手。我正在通过 SQL 命令执行一个简单的插入查询。现在我想检索这个新插入的行的唯一 id 并将其存储在一个数组中。我不知道如何解决这个问题。

我的代码

foreach (GridViewRow gvrow in gvuserdata.Rows)
        {

            string strQuery = "insert into vishalc.[Rental Table] (Car_Id_Reference, UserName, CarName,Price,startdate,enddate)" +
            " values(@carid, @username, @carname,@price,@startdate,@enddate)";
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.AddWithValue("@carid", gvrow.Cells[0].Text);
            cmd.Parameters.AddWithValue("@username", gvrow.Cells[1].Text);
            cmd.Parameters.AddWithValue("@carname", gvrow.Cells[2].Text);
            cmd.Parameters.AddWithValue("@price", gvrow.Cells[3].Text);
            cmd.Parameters.AddWithValue("@startdate", gvrow.Cells[4].Text);
            cmd.Parameters.AddWithValue("@enddate", gvrow.Cells[5].Text);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
         //I want to store ID for this row after insert command   
        }

【问题讨论】:

    标签: c# asp.net sql database dataset


    【解决方案1】:

    必须在列中设置mialkey和Identity =“是”。

    然后你可以使用scopidentity

    string strQuery = "insert into vishalc.[Rental Table] (Car_Id_Reference, UserName, CarName,Price,startdate,enddate)" +
                " values(@carid, @username, @carname,@price,@startdate,@enddate); **Select Scope_Identity();**";
                SqlCommand cmd = new SqlCommand(strQuery);
                cmd.Parameters.AddWithValue("@carid", gvrow.Cells[0].Text);
                cmd.Parameters.AddWithValue("@username", gvrow.Cells[1].Text);
                cmd.Parameters.AddWithValue("@carname", gvrow.Cells[2].Text);
                cmd.Parameters.AddWithValue("@price", gvrow.Cells[3].Text);
                cmd.Parameters.AddWithValue("@startdate", gvrow.Cells[4].Text);
                cmd.Parameters.AddWithValue("@enddate", gvrow.Cells[5].Text);
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
             **Guid Id=(Guid)(cmd.ExecuteNonQuery());** 
    

    请参阅下面的行

    guid id =(guid)(cmd.executenonquery())

    你想要guid。所以我被转换为guid。

    【讨论】:

      【解决方案2】:

      将 Select Scope_Identity() 与执行标量一起使用:

      int returnID = 0;
      string strQuery = "insert into vishalc.[Rental Table] (Car_Id_Reference, UserName,  
         CarName,Price,startdate,enddate)" + " values(@carid, @username, 
         @carname,@price,@startdate,@enddate); Select Scope_Identity()";
      ...
      returnID = (int)cmd.ExecuteScalar();
      

      【讨论】:

        【解决方案3】:

        请尝试添加输出参数和SCOPE_IDENTITY输出参数:

        int RetVal = 0;
        
        foreach (GridViewRow gvrow in gvuserdata.Rows)
        {
        
            string strQuery = "insert into vishalc.[Rental Table] (Car_Id_Reference, UserName, CarName,Price,startdate,enddate)" +
            " values(@carid, @username, @carname,@price,@startdate,@enddate);set @out=SCOPE_IDENTITY()";
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.AddWithValue("@carid", gvrow.Cells[0].Text);
            cmd.Parameters.AddWithValue("@username", gvrow.Cells[1].Text);
            cmd.Parameters.AddWithValue("@carname", gvrow.Cells[2].Text);
            cmd.Parameters.AddWithValue("@price", gvrow.Cells[3].Text);
            cmd.Parameters.AddWithValue("@startdate", gvrow.Cells[4].Text);
            cmd.Parameters.AddWithValue("@enddate", gvrow.Cells[5].Text);
        
            SqlParameter outpar = new SqlParameter("@out", SqlDbType.Int);
            outpar.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(outpar);
        
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
        
            if (outpar != null)
                RetVal = Convert.ToInt32(outpar.Value);
        }
        

        【讨论】:

          【解决方案4】:

          嗨,史蒂夫,在您的表中再插入一列,以 uniqueidentifier 作为数据类型,并将 Guid id 添加到代码隐藏中唯一可识别的列中

          创建 GUID

          // This code example demonstrates the Guid.NewGuid() method. 
          
          using System;
          
          class CreateGUID
          {
              public static void Main() 
              {
              Guid g;
          // Create and display the value of two GUIDs.
              g = Guid.NewGuid();
              Console.WriteLine(g);
              Console.WriteLine(Guid.NewGuid());
              }
          }
          
          /*
          This code example produces the following results:
          
          0f8fad5b-d9cb-469f-a165-70867728950e
          7c9e6679-7425-40de-944b-e07fc1f90ae7
          
          */
          

          以上代码仅供您参考,以检查每次生成的唯一 ID 现在您的代码如下所示

          foreach (GridViewRow gvrow in gvuserdata.Rows)
                  {
          Guid uniqueRowId = Guid.NewGuid();
          
          
                      string strQuery = "insert into vishalc.[Rental Table] (uniqueRowId, Car_Id_Reference, UserName, CarName,Price,startdate,enddate)" +
                      " values(@uniqueRowId,@carid, @username, @carname,@price,@startdate,@enddate)";
                      SqlCommand cmd = new SqlCommand(strQuery);    
                    cmd.Parameters.AddWithValue("@uniqueRowId", uniqueRowId);    
                      cmd.Parameters.AddWithValue("@carid", gvrow.Cells[0].Text);
                      cmd.Parameters.AddWithValue("@username", gvrow.Cells[1].Text);
                      cmd.Parameters.AddWithValue("@carname", gvrow.Cells[2].Text);
                      cmd.Parameters.AddWithValue("@price", gvrow.Cells[3].Text);
                      cmd.Parameters.AddWithValue("@startdate",gvrow.Cells[4].Text);
                      cmd.Parameters.AddWithValue("@enddate", gvrow.Cells[5].Text);
                      cmd.CommandType = CommandType.Text;``
                      cmd.Connection = con;
                      cmd.ExecuteNonQuery();
                   //I want to store ID for this row after insert command   
                  }
          

          注意:确保您已更新 TABLE 中的列

          【讨论】:

            【解决方案5】:

            我认为您的问题是如何在数据库中检索新插入的记录,对吗?如果是这种情况,您可以使用 Scope_Identity() 获取您的

            int returnID = 0;
            string strQuery = "insert into vishalc.[Rental Table] (Car_Id_Reference, UserName,     CarName,Price,startdate,enddate)" + 
            " values(@carid, @username, @carname,@price,@startdate,@enddate); Select Scope_Identity()";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            
            SqlDataReader reader = new SqlDataReader();
            reader = cmd.ExecuteReader();
            returnId = reader("Car_Id_Reference")
            

            【讨论】:

              猜你喜欢
              • 2012-05-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-14
              • 1970-01-01
              • 1970-01-01
              • 2011-06-21
              相关资源
              最近更新 更多