2013-04-09 62 views
2

我是新来asp.net。我正在通过SQL命令执行一个简单的插入查询。现在我想检索此新插入行的唯一ID,并将其存储在数组中。我不知道如何解决这个问题。检索唯一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 
     } 

回答

0

喜史蒂夫插入与唯一标识符的数据类型的表多了一个栏,并在代码隐藏添加的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 
     } 

注意:请确保您有更新表中

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") 
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); 
} 
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(); 
0

必须设置的PrimaryKey和身份=“是”在列中。

然后你可以使用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标识=(GUID)(cmd.ExecuteNonQuery())

你想要的GUID。所以我我转换成了guid。