使用CommandType.Tabledirect的目的是什么?

本文关键字:是什么 使用 Tabledirect CommandType | 更新日期: 2023-09-27 17:51:00

如何使用选项CommandType.TabledirectCommandType.StoredProcedureCommandType.Text不同?

使用CommandType.Tabledirect的目的是什么?

CommandType包含指定如何解释命令字符串的名称。

  1. CommandType.Text用于SQL文本命令。(默认)。
  2. CommandType.StoredProcedure为存储过程的名称。
  3. CommandType.TableDirect用于表名

指定表的所有行和列将在调用Execute方法之一时返回。

注意:TableDirect仅被。net框架数据提供程序支持用于OLE DB。当CommandType设置为TableDirect时,不支持多表访问。

使用示例:

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

插入/更新

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