2

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

5 回答 5

1

将 Select Scope_Identity() 与 Execute Scalar 一起使用:

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();
于 2013-04-09T05:07:23.827 回答
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);
}
于 2013-04-09T05:00:45.293 回答
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 。所以我转换为 guid 。

于 2013-04-09T19:18:17.860 回答
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")
于 2013-04-09T04:54:55.133 回答
0

嗨,史蒂夫,在您的表中再插入一列,使用 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 中的列

于 2013-04-09T04:45:14.473 回答