How is the option CommandType.Tabledirect
used unlike CommandType.StoredProcedure
or CommandType.Text
?
TableDirect. The CommandText property should be set to the table name, and all rows and column in the table will be returned. Text. A SQL text command. As you can see from Table 5-25, you can call a stored procedure, use TableDirect, or execute a SQL command.
CommandType can be one of the following values: Text, StoredProcedure, TableDirect. When the value is CommandType. Text, the property CommandText should contain text of a query that must be run on the server. When the value is CommandType. StoredProcedure, CommandText property must be a name of a procedure to execute.
Introduction. A Stored Procedure is a group of SQL statements compiled into a single execution. A Stored Procedure is a prepared SQL code that you can save, so the code can be reused over and over again. Input and output parameters are used in the stored procedure.
The CommandType property sets or returns a CommandTypeEnum value that defines the type of the Command object. Default is adCmdUnknown. If you do not specify the type, ADO will need to contact the provider to determine the type of the command.
CommandType contains names that specifies how a command string is interpreted.
CommandType.Text
for an SQL text command. (Default.) CommandType.StoredProcedure
for the name of a stored procedure.CommandType.TableDirect
for the name of a table.All rows and columns of the named table will be returned when you call one of the Execute methods.
NOTE: TableDirect is only supported by the .NET Framework Data Provider for OLE DB. Multiple table access is not supported when CommandType is set to TableDirect.
Sample example how it is been used:
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();
Insert/Update
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With