OleDbCommand 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示要对数据源执行的 SQL 语句或存储过程。
public ref class OleDbCommand sealed : System::Data::Common::DbCommand, ICloneable
public ref class OleDbCommand sealed : System::Data::Common::DbCommand, ICloneable, IDisposable
public ref class OleDbCommand sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbCommand
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable, IDisposable
public sealed class OleDbCommand : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbCommand
type OleDbCommand = class
inherit DbCommand
interface IDbCommand
interface IDisposable
interface ICloneable
type OleDbCommand = class
inherit DbCommand
interface ICloneable
interface IDbCommand
interface IDisposable
type OleDbCommand = class
inherit Component
interface ICloneable
interface IDbCommand
interface IDisposable
Public NotInheritable Class OleDbCommand
Inherits DbCommand
Implements ICloneable
Public NotInheritable Class OleDbCommand
Inherits DbCommand
Implements ICloneable, IDisposable
Public NotInheritable Class OleDbCommand
Inherits Component
Implements ICloneable, IDbCommand, IDisposable
- 继承
- 继承
- 继承
- 实现
示例
以下示例使用 OleDbCommand、 OleDbDataAdapter 和 OleDbConnection从 Access 数据库中选择行。 然后返回填充 DataSet 的 。 该示例传递了一个初始化 DataSet的 、一个连接字符串、一个作为 SQL SELECT 语句的查询字符串,以及一个作为源数据库表名称的字符串。
public void ReadMyData(string connectionString)
{
string queryString = "SELECT OrderID, CustomerID FROM Orders";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
}
// always call Close when done reading.
reader.Close();
}
}
Public Sub ReadMyData(ByVal connectionString As String)
Dim queryString As String = "SELECT OrderID, CustomerID FROM Orders"
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(queryString, connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
Console.WriteLine(reader.GetInt32(0).ToString() + ", " _
+ reader.GetString(1))
End While
' always call Close when done reading.
reader.Close()
End Using
End Sub
注解
创建 的 OleDbCommand 实例时,读/写属性将设置为其初始值。 有关这些值的列表,请参阅 OleDbCommand 构造函数。
OleDbCommand 采用以下方法在数据源中执行命令:
项 | 说明 |
---|---|
ExecuteReader | 执行返回行的命令。 ExecuteReader 如果用于执行 SQL SET 语句等命令,则可能没有所需的效果。 |
ExecuteNonQuery | 执行 SQL INSERT、DELETE、UPDATE 和 SET 语句等命令。 |
ExecuteScalar | 检索单个值,例如,从数据库检索聚合值。 |
可以重置 CommandText 属性并重复使用 OleDbCommand 对象。 但是,必须先关闭 , OleDbDataReader 然后才能执行新的或以前的命令。
例如,如果致命OleDbException (,则SQL Server严重性级别为 20 或更高) 由执行 OleDbCommand的方法生成,OleDbConnection连接可能会关闭。 但是,用户可以重新打开连接并继续操作。
构造函数
OleDbCommand() |
初始化 OleDbCommand 类的新实例。 |
OleDbCommand(String) |
使用查询的文本初始化 OleDbCommand 类的新实例。 |
OleDbCommand(String, OleDbConnection) |
使用查询的文本和 OleDbConnection 初始化 OleDbCommand 类的新实例。 |
OleDbCommand(String, OleDbConnection, OleDbTransaction) |
使用查询文本、OleDbConnection 以及 Transaction 初始化 OleDbCommand 类的新实例。 |
属性
CanRaiseEvents |
获取一个指示组件是否可以引发事件的值。 (继承自 Component) |
CommandText |
获取或设置要对数据源执行的 SQL 语句或存储过程。 |
CommandTimeout |
获取或设置在终止尝试执行命令并生成错误之前的等待时间(以秒为单位)。 |
CommandType |
获取或设置一个值,该值指示如何解释 CommandText 属性。 |
Connection |
获取或设置 OleDbCommand 的此实例使用的 OleDbConnection。 |
Container |
获取包含 IContainer 的 Component。 (继承自 Component) |
DbConnection |
获取或设置此 DbConnection 使用的 DbCommand。 (继承自 DbCommand) |
DbParameterCollection |
获取 DbParameter 对象的集合。 (继承自 DbCommand) |
DbTransaction |
获取或设置将在其中执行此 DbCommand 对象的 DbTransaction。 (继承自 DbCommand) |
DesignMode |
获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
DesignTimeVisible |
获取或设置一个值,该值指示命令对象在自定义的 Windows 窗体设计器控件中是否应可见。 |
Events |
获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
Parameters | |
Site | (继承自 Component) |
Transaction |
获取或设置要在其中执行 OleDbTransaction 的 OleDbCommand。 |
UpdatedRowSource |
获取或设置命令结果在由 DataRow 的 |
方法
事件
Disposed |
在通过调用 Dispose() 方法释放组件时发生。 (继承自 Component) |
显式接口实现
ICloneable.Clone() |
有关此成员的说明,请参见 Clone()。 |
IDbCommand.Connection |
获取或设置 IDbCommand 的此实例使用的 IDbConnection。 (继承自 DbCommand) |
IDbCommand.CreateParameter() |
此 API 支持产品基础结构,不能在代码中直接使用。 创建 IDbDataParameter 对象的新实例。 |
IDbCommand.CreateParameter() |
创建 IDbDataParameter 对象的新实例。 (继承自 DbCommand) |
IDbCommand.ExecuteReader() |
有关此成员的说明,请参见 ExecuteReader()。 |
IDbCommand.ExecuteReader(CommandBehavior) |
对 Connection 执行 CommandText,并使用 CommandBehavior 值之一生成 IDataReader。 |
IDbCommand.Parameters | (继承自 DbCommand) |
IDbCommand.Transaction |
获取或设置将在其中执行此 DbCommand 对象的 DbTransaction。 (继承自 DbCommand) |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈