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

Execute Scalar で 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();
于 2013-04-09T05:07:23.827 に答える
0

あなたの質問は、DB に新しく挿入されたレコードを取得する方法だと思いますか? その場合は、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

出力パラメータと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

こんにちはスティーブ データ型として uniqueidentifier を使用してテーブルにもう 1 つの列を挿入し、一意に識別可能な分離コードの列に 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 に答える
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 に答える