3

いくつかの回答を閲覧しましたが、うまく機能していないようです。

プログラムの別の部分で使用できるように、テーブルのIDフィールドを返す必要があります。

    Convert.ToInt32(sqlComm.ExecuteScalar());

しかし、運がなく、同じです

    Convert.ToInt32(sqlComm.Parameters["ID"].Value);

そして、レコードがテーブルに挿入されたとしても、両方とも0を返します。

以下のコードをダンプします、誰かが私が間違っていることを見ることができますか?

    using (SqlConnection sqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand sqlComm = new SqlCommand("up_Insert_Address", sqlConnect))
            {
                sqlComm.CommandType = CommandType.StoredProcedure;
                sqlComm.Parameters.Add("@AddressID", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                sqlComm.Parameters.Add("@AddressLineOne", SqlDbType.NVarChar, 40).Value = address.AddressLineOne;   

                try
                {
                    sqlComm.Connection.Open();
                    return Convert.ToInt32(sqlComm.ExecuteScalar());
                }

                catch (SqlException)
                {
                }

                finally
                {
                    sqlComm.Connection.Close();
                }
            }
        }

およびストアドプロシージャ:

    @AddressID   Bigint OUTPUT,
    @AddressLineOne  NVarChar(40)
    AS
     BEGIN
      BEGIN TRY
      INSERT INTO Address
      (
         AddressLineOne
      )
      VALUES 
      (
         @AddressLineOne
      )

      SET @AddressID = SCOPE_IDENTITY();
    END TRY
    BEGIN CATCH
       DECLARE @Err nvarchar(500)
       SET @Err = ERROR_MESSAGE()
       RAISERROR(@Err, 16, 1)
    END CATCH
   END
4

2 に答える 2

6

使用する必要があります

Convert.ToInt64(sqlComm.Parameters["@AddressID"].Value);

を使用してコマンドを実行した後ExceuteNonQuery。後で参照できるようExecuteScalar に、クエリによって返された結果セットの最初の行の最初の列を返します。パラメータの値を設定するだけで、何も返されませんOUTPUT

また、絶対に飲み込まないでくださいSqlException。コマンドと接続はすでにusingブロックに含まれているため、別のtry / catch/finallyを追加する必要はありません。次のように変更します。

//try
//{
    sqlComm.Connection.Open();
    sqlComm.ExecuteNonQuery();
    return Convert.ToInt64(sqlComm.Parameters["@AddressID"].Value);
    // using Int64 since the SQL type is BigInt
//}

//catch (SqlException)
//{
//}

//finally
//{
//    sqlComm.Connection.Close();
//}
于 2013-01-17T16:52:12.940 に答える
0
var connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

var addressId = 0L; // long value (64bit)

using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("up_Insert_Address", connection))
{
  command.CommandType = CommandType.StoredProcedure;

  command.Parameters.AddWithValue("@AddressID", addressId);    
  command.Parameters.AddWithValue("@AddressLineOne", address.AddressLineOne); 

  command.Parameters["@AddressID"].Direction = ParameterDirection.Output;

  try
  {
    if(connection.State != ConnectionState.Open)
      connection.Open();

    var rowsAffected = command.ExecuteNonQuery();
    addressId = Convert.ToInt64(command.Parameters["@AddressID"].Value);
  }
  catch (SqlException)
  {
    // Handle SQL errors here.
  }
}
于 2013-01-17T17:02:50.203 に答える