Condividi tramite


Metodo SqlCeCommand.Prepare

Crea una versione preparata o compilata del comando sull'origine dati.

Spazio dei nomi  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

Sintassi

'Dichiarazione
Public Overrides Sub Prepare
'Utilizzo
Dim instance As SqlCeCommand

instance.Prepare()
public override void Prepare()
public:
virtual void Prepare() override
abstract Prepare : unit -> unit 
override Prepare : unit -> unit 
public override function Prepare()

Implementa

IDbCommand.Prepare()

Eccezioni

Eccezione Condizione
InvalidOperationException

La proprietà Connection non è impostata.

oppure

La proprietà Connection non è Open.

Osservazioni

Se la proprietà CommandType è impostata su TableDirect, il metodo Prepare non esegue alcuna operazione.

Prima di chiamare il metodo Prepare, specificare il tipo di dati di ciascun parametro nell'istruzione da preparare. Per ciascun parametro che contiene un tipo di dati a lunghezza variabile, occorre impostare la proprietà Size sulla dimensione massima necessaria. Il metodo Prepare restituisce un errore se queste condizioni non vengono soddisfatte.

Se si chiama un metodo Execute dopo aver chiamato il metodo Prepare, tutti i valori di parametro maggiori del valore specificato dalla proprietà Size saranno troncati automaticamente alla dimensione originale specificata del parametro e non verranno restituiti errori di troncamento.

Esempi

Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf;")
conn.Open()

Dim command As SqlCeCommand = conn.CreateCommand()

' Create and prepare a SQL statement
'
command.CommandText = "INSERT INTO Region (RegionID, RegionDescription) VALUES (@id, @desc)"

Dim param As SqlCeParameter = Nothing

' NOTE:
' For optimal performance, make sure you always set the parameter
' type and the maximum size - this is especially important for non-fixed
' types such as NVARCHAR or NTEXT; In case of named parameters, 
' SqlCeParameter instances do not need to be added to the collection
' in the order specified in the query; If however you use ? as parameter
' specifiers, then you do need to add the parameters in the correct order
'
param = New SqlCeParameter("@id", SqlDbType.Int)
command.Parameters.Add(param)

param = New SqlCeParameter("@desc", SqlDbType.NVarChar, 100)
command.Parameters.Add(param)

command.Parameters("@desc").Size = 100

' Calling Prepare after having set the CommandText and parameters
'
command.Prepare()

' Execute the SQL statement
'
command.ExecuteNonQuery()

' Change parameter values and call ExecuteNonQuery again
'
command.Parameters(0).Value = 21
command.Parameters(1).Value = "mySecondRegion"
command.ExecuteNonQuery()
SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf;");
conn.Open();

SqlCeCommand command = conn.CreateCommand();

// Create and prepare a SQL statement
//
command.CommandText = "INSERT INTO Region (RegionID, RegionDescription) VALUES (@id, @desc)";

SqlCeParameter param = null;

// NOTE:
// For optimal performance, make sure you always set the parameter
// type and the maximum size - this is especially important for non-fixed
// types such as NVARCHAR or NTEXT; In case of named parameters, 
// SqlCeParameter instances do not need to be added to the collection
// in the order specified in the query; If however you use ? as parameter
// specifiers, then you do need to add the parameters in the correct order
//
param = new SqlCeParameter("@id", SqlDbType.Int);
command.Parameters.Add(param);

param = new SqlCeParameter("@desc", SqlDbType.NVarChar, 100);
command.Parameters.Add(param);

command.Parameters["@desc"].Size = 100;

// Calling Prepare after having set the CommandText and parameters
//
command.Prepare();

// Execute the SQL statement
//
command.ExecuteNonQuery();

// Change parameter values and call ExecuteNonQuery again
//
command.Parameters[0].Value = 21;
command.Parameters[1].Value = "mySecondRegion";
command.ExecuteNonQuery();

Vedere anche

Riferimento

SqlCeCommand Classe

Spazio dei nomi System.Data.SqlServerCe