Modifica di dati con le stored procedure (ADO.NET)

Le stored procedure possono accettare dati come parametri di input e possono restituire dati come parametri di output, set di risultati o valori restituiti. Nell'esempio seguente vengono illustrati l'invio e la ricezione di parametri di input, parametri di output e valori restituiti in ADO.NET. Viene inserito un nuovo record in una tabella in cui la colonna della chiave primaria è una colonna Identity di un database SQL Server.

NotaNota

Se si utilizzano stored procedure SQL Server per modificare o eliminare dati tramite SqlDataAdapter, assicurarsi di non utilizzare SET NOCOUNT ON nella definizione della stored procedure.Con tale comando il totale restituito delle righe interessate è pari a zero e tale situazione viene interpretata da DataAdapter come un conflitto di concorrenza.In questo caso verrà generata un'eccezione DBConcurrencyException.

Esempio

Nell'esempio viene utilizzata la stored procedure seguente per inserire una nuova categoria nella tabella Categories di Northwind. La stored procedure accetta il valore nella colonna CategoryName come parametro di input e utilizza la funzione SCOPE_IDENTITY () per recuperare il nuovo valore nel campo Identity, CategoryID, e lo restituisce come parametro di output. Nell'istruzione RETURN viene utilizzata la funzione @@ROWCOUNT per restituire il numero delle righe inserite.

CREATE PROCEDURE dbo.InsertCategory
  @CategoryName nvarchar(15),
  @Identity int OUT
AS
INSERT INTO Categories (CategoryName) VALUES(@CategoryName)
SET @Identity = SCOPE_IDENTITY()
RETURN @@ROWCOUNT

Nell'esempio di codice seguente viene utilizzata la stored procedure InsertCategory sopra indicata come origine per InsertCommand di SqlDataAdapter. Il parametro di output @Identity e il valore restituito verranno riflessi in DataSet dopo l'inserimento del record nel database quando viene chiamato il metodo Update di SqlDataAdapter. Nel codice viene inoltre recuperato il valore restituito:

NotaNota

Quando si utilizza OleDbDataAdapter, è necessario specificare i parametri con ParameterDirection impostato su ReturnValue prima degli altri parametri.

Private Sub ReturnIdentity(ByVal connectionString As String)
    Using connection As SqlConnection = New SqlConnection( _
       connectionString)

        ' Create a SqlDataAdapter based on a SELECT query.
        Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
           "SELECT CategoryID, CategoryName FROM dbo.Categories", _
           connection)

        ' Create a SqlCommand to execute the stored procedure. 
        adapter.InsertCommand = New SqlCommand("dbo.InsertCategory", _
           connection)
        adapter.InsertCommand.CommandType = CommandType.StoredProcedure

        ' Create a parameter for the ReturnValue.
        Dim parameter As SqlParameter = _
           adapter.InsertCommand.Parameters.Add( _
          "@RowCount", SqlDbType.Int)
        parameter.Direction = ParameterDirection.ReturnValue

        ' Create an input parameter for the CategoryName.
        ' You do not need to specify direction for input parameters.
        adapter.InsertCommand.Parameters.Add( _
          "@CategoryName", SqlDbType.NChar, 15, "CategoryName")

        ' Create an output parameter for the new identity value.
        parameter = adapter.InsertCommand.Parameters.Add( _
          "@Identity", SqlDbType.Int, 0, "CategoryID")
        parameter.Direction = ParameterDirection.Output

        ' Create a DataTable and fill it.
        Dim categories As DataTable = New DataTable
        adapter.Fill(categories)

        ' Add a new row.
        Dim newRow As DataRow = categories.NewRow()
        newRow("CategoryName") = "New Category"
        categories.Rows.Add(newRow)

        ' Update the database.
        adapter.Update(categories)

        ' Retrieve the ReturnValue.
        Dim rowCount As Int32 = _
           CInt(adapter.InsertCommand.Parameters("@RowCount").Value)

        Console.WriteLine("ReturnValue: {0}", rowCount.ToString())
        Console.WriteLine("All Rows:")
        Dim row As DataRow
        For Each row In categories.Rows
            Console.WriteLine("  {0}: {1}", row(0), row(1))
        Next
    End Using
End Sub
private static void ReturnIdentity(string connectionString)
{
    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        // Create a SqlDataAdapter based on a SELECT query.
        SqlDataAdapter adapter = new SqlDataAdapter(
          "SELECT CategoryID, CategoryName FROM dbo.Categories",
          connection);

        // Create a SqlCommand to execute the stored procedure.
        adapter.InsertCommand = new SqlCommand("InsertCategory", connection);
        adapter.InsertCommand.CommandType = CommandType.StoredProcedure;

        // Create a parameter for the ReturnValue.
        SqlParameter parameter = adapter.InsertCommand.Parameters.Add(
          "@RowCount", SqlDbType.Int);
        parameter.Direction = ParameterDirection.ReturnValue;

        // Create an input parameter for the CategoryName.
        // You do not need to specify direction for input parameters.
        adapter.InsertCommand.Parameters.Add(
          "@CategoryName", SqlDbType.NChar, 15, "CategoryName");

        // Create an output parameter for the new identity value.
        parameter = adapter.InsertCommand.Parameters.Add(
          "@Identity", SqlDbType.Int, 0, "CategoryID");
        parameter.Direction = ParameterDirection.Output;

        // Create a DataTable and fill it.
        DataTable categories = new DataTable();
        adapter.Fill(categories);

        // Add a new row.
        DataRow categoryRow = categories.NewRow();
        categoryRow["CategoryName"] = "New Beverages";
        categories.Rows.Add(categoryRow);

        // Update the database.
        adapter.Update(categories);

        // Retrieve the ReturnValue.
        Int32 rowCount =
            (Int32)adapter.InsertCommand.Parameters["@RowCount"].Value;

        Console.WriteLine("ReturnValue: {0}", rowCount.ToString());
        Console.WriteLine("All Rows:");
        foreach (DataRow row in categories.Rows)
        {
            {
                Console.WriteLine("  {0}: {1}", row[0], row[1]);
            }
        }
    }
}

Vedere anche

Concetti

Esecuzione di un comando (ADO.NET)

Altre risorse

Recupero e modifica di dati in ADO.NET

DataAdapter e DataReader (ADO.NET)