IDbCommand.Prepare Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee maakt u een voorbereide (of gecompileerde) versie van de opdracht op de gegevensbron.
public:
void Prepare();
public void Prepare();
abstract member Prepare : unit -> unit
Public Sub Prepare ()
Uitzonderingen
Voorbeelden
In het volgende voorbeeld wordt een exemplaar van de afgeleide klasse OleDbCommandgemaakt en wordt de verbinding geopend. In het voorbeeld wordt vervolgens een opgeslagen procedure op de gegevensbron voorbereid door een tekenreeks door te geven die een SQL Select-instructie is en een tekenreeks die moet worden gebruikt om verbinding te maken met de gegevensbron.
private static void OleDbCommandPrepare(string connectionString)
{
using (OleDbConnection connection = new
OleDbConnection(connectionString))
{
connection.Open();
// Create the Command.
OleDbCommand command = new OleDbCommand();
// Set the Connection, CommandText and Parameters.
command.Connection = connection;
command.CommandText =
"INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?)";
command.Parameters.Add("RegionID", OleDbType.Integer, 4);
command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50);
command.Parameters[0].Value = 20;
command.Parameters[1].Value = "First Region";
// Call Prepare and ExecuteNonQuery.
command.Prepare();
command.ExecuteNonQuery();
// Change parameter values and call ExecuteNonQuery.
command.Parameters[0].Value = 21;
command.Parameters[1].Value = "SecondRegion";
command.ExecuteNonQuery();
}
}
Public Sub OleDbCommandPrepare(ByVal connectionString As String)
Using connection As OleDbConnection = New _
OleDbConnection(connectionString)
connection.Open()
' Create the Command.
Dim command As New OleDbCommand()
' Set the Connection, CommandText and Parameters.
command.Connection = connection
command.CommandText = _
"INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?);"
command.Parameters.Add("RegionID", OleDbType.Integer, 4)
command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50)
command.Parameters(0).Value = 20
command.Parameters(1).Value = "First Region"
' Call Prepare and ExecuteNonQuery.
command.Prepare()
command.ExecuteNonQuery()
' Change parameter values and call ExecuteNonQuery.
command.Parameters(0).Value = 21
command.Parameters(1).Value = "Second Region"
command.ExecuteNonQuery()
End Using
End Sub
Opmerkingen
Als de CommandType eigenschap is ingesteld op TableDirect, Prepare doet u niets. Als CommandType dit is ingesteld StoredProcedure, moet de aanroep Prepare slagen, hoewel dit kan leiden tot een no-op. De server slaat zo nodig automatisch plannen voor hergebruik in de cache op; Daarom hoeft u deze methode niet rechtstreeks aan te roepen in uw clienttoepassing.