检索刚刚添加的行的唯一ID

时间:2013-04-09 04:39:46

标签: c# asp.net sql database dataset

我是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   
        }

5 个答案:

答案 0 :(得分:1)

使用Execute Scalar选择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();

答案 1 :(得分:0)

您好Steve在您的表中使用uniqueidentifier作为数据类型再插入一列,并将Guid id添加到codebehind中可唯一标识的列中

创建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   
        }

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

答案 2 :(得分:0)

我认为您的问题是如何在数据库中检索新插入的记录?如果是这种情况,你可以使用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")

答案 3 :(得分:0)

请尝试添加输出参数和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 :(得分:0)

必须在列中设置主键和Identity =“yes”。

然后你可以使用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。