2013-01-07 24 views
25

Probabilmente io uso le query dirette per recuperare i record da una tabella e anche la stored procedure alcune volte. Cito il tipo di comando come CommandType.StoredProcedure durante l'utilizzo di SP. Inoltre sto vedendo un'altra opzione denominata CommandType.Tabledirect, cercato in qualche altro posto, ma non è chiaro a riguardo. Qualcuno potrebbe aiutarmi a farsi un'idea al riguardo? Per favore, dammi alcuni codici di esempio.Qual è lo scopo dell'uso di CommandType.Tabledirect

risposta

33

CommandType contiene nomi che specificano come viene interpretata una stringa di comando.

  1. CommandType.Text per un comando di testo SQL. (Predefinito)
  2. CommandType.StoredProcedure per il nome di una stored procedure.
  3. CommandType.TableDirect per il nome di un tavolo.

Tutte le righe e le colonne della tabella denominata verranno restituite quando si chiama uno dei metodi Execute.

NOTA: TableDirect è supportato solo dal provider di dati .NET Framework per OLE DB . L'accesso a più tabelle non è supportato quando CommandType è impostato su TableDirect.

esempio campione come viene stato utilizzato:

OleDbConnection myOleDbConnection =new OleDbConnection("provider=sqloledb;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI"); 
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand(); 

myOleDbCommand.CommandType = CommandType.TableDirect; 

myOleDbCommand.CommandText = "Employee"; 

myOleDbConnection.Open(); 

OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader(); 

for (int count = 1; count <= 2; count++) 
{ 
    myOleDbDataReader.Read(); 
    Console.WriteLine("myOleDbDataReader[\" ID\"] = " + 
    myOleDbDataReader["ID"]); 
    Console.WriteLine("myOleDbDataReader[\" FirstName\"] = " + 
    myOleDbDataReader["FirstName"]); 
    Console.WriteLine("myOleDbDataReader[\" LastName\"] = " + 
    myOleDbDataReader["LastName"]); 
} 
myOleDbDataReader.Close(); 
myOleDbConnection.Close(); 

Inserisci/Aggiorna

 try 
     { 
      using (SqlCeCommand command = conn.CreateCommand()) 
      { 
       command.CommandText = "Holdings"; 
       command.CommandType = CommandType.TableDirect; 
       using (SqlCeResultSet rs = command.ExecuteResultSet(ResultSetOptions.Updatable | ResultSetOptions.Scrollable)) 
       { 
        SqlCeUpdatableRecord record = rs.CreateRecord(); 
        foreach (var r in _commitBatch) 
        { 
         int index=0; 
         record.SetValue(index++, r.TryGetValueOrDefault("IdentifierFromImportSource",string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("SecurityID", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("SecurityName", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("SecurityType", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("AllocationAmount", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("Position", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("AnnualFeePercent", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("MarginAmount", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("Price", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("MorningstarSecId", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("MorningstarSecType", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("UserID", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("MorningstarPrice", string.Empty)); 
         record.SetValue(index++, string.Empty); 
         record.SetValue(index++, r.TryGetValueOrDefault("AnnualFeeFrequency", string.Empty)); 
         record.SetValue(index++, r.TryGetValueOrDefault("TrackingMethod", "1")); 
         rs.Insert(record); 
        } 
       } 

      } 

     } 
     catch (Exception e) 
     { 
      NotifyError(this, new ImportErrorEventArgs(e.Message + e.StackTrace, ErrorLevel.Application)); 
     } 
+0

E 'possibile, per eseguire l'aggiornamento/inserimento/cancellazione azioni tramite TableDirect.if sì, si può anche posta codice di esso. –

+0

Ho appena aggiornato la mia risposta .. @ faheemkhan –

+0

Grazie, lo esaminerò. –