sql >> Base de Datos >  >> RDS >> Sqlserver

¿Cuál es el propósito de usar CommandType.Tabledirect?

Tipo de comando contiene nombres que especifican cómo se interpreta una cadena de comando.

  1. CommandType.Text para un comando de texto SQL. (Predeterminado).
  2. CommandType.StoredProcedure para el nombre de un procedimiento almacenado.
  3. CommandType.TableDirect para el nombre de una tabla .

Todas las filas y columnas de la tabla nombrada se devolverán cuando llame a uno de los métodos Execute.

NOTA:TableDirect solo es compatible con el proveedor de datos de .NET Framework para OLE DB . El acceso a varias tablas no es compatible cuando CommandType está configurado en TableDirect .

Ejemplo de ejemplo de cómo se ha utilizado:

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();

Insertar/Actualizar

        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));
        }