问题描述
也许我用直接查询检索表中的记录,并存储过程的一些时间。我提到的命令类型CommandType.StoredProcedure同时使用SP。我也看到一个名为CommandType.Tabledirect另一种选择,寻找一些其他的地方,但它并不清楚。可以在任何一个可以帮助我得到关于它的主意?请给我一些示例代码。
Probably I use direct queries to retrieve records from a table and also Stored procedure some times. I mention the command type as CommandType.StoredProcedure while using SP. Also am seeing another option named CommandType.Tabledirect, searched some where else, but not clear about it. Could any one help me to get idea about it? Please give me some sample codes.
推荐答案
的包含指定命令字符串是如何解释的名称。
CommandType contains names that specifies how a command string is interpreted.
-
CommandType.Text
为SQL文本命令。 (默认值。) -
CommandType.StoredProcedure
为存储过程的名称。 -
CommandType.TableDirect
为表的在名称
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.
在调用Execute方法之一指定表的所有行和列将被退回。
All rows and columns of the named table will be returned when you call one of the Execute methods.
请注意: TableDirect 仅由.NET Framework数据提供支持的 OLE DB 。
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.
样品例如它是如何被使用的是不支持多表的访问b
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));
}
这篇关于这是使用CommandType.Tabledirect的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!