OdbcCommand 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示要对数据源执行的 SQL 语句或存储过程。 此类不能被继承。
public ref class OdbcCommand sealed : System::Data::Common::DbCommand, ICloneable
public ref class OdbcCommand sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbCommand
public sealed class OdbcCommand : System.Data.Common.DbCommand, ICloneable
public sealed class OdbcCommand : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbCommand
type OdbcCommand = class
inherit DbCommand
interface ICloneable
type OdbcCommand = class
inherit Component
interface ICloneable
interface IDbCommand
interface IDisposable
Public NotInheritable Class OdbcCommand
Inherits DbCommand
Implements ICloneable
Public NotInheritable Class OdbcCommand
Inherits Component
Implements ICloneable, IDbCommand, IDisposable
- 继承
- 继承
- 实现
示例
下面的示例使用 ExecuteNonQuery。
public void InsertRow(string connectionString, string insertSQL)
{
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
// The insertSQL string contains a SQL statement that
// inserts a new row in the source table.
OdbcCommand command = new OdbcCommand(insertSQL, connection);
// Open the connection and execute the insert command.
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
Public Sub InsertRow(ByVal connectionString As String, _
ByVal insertSQL As String)
Using connection As New OdbcConnection(connectionString)
' The insertSQL string contains a SQL statement that
' inserts a new row in the source table.
Dim command As New OdbcCommand(insertSQL, connection)
' Open the connection and execute the insert command.
Try
connection.Open()
command.ExecuteNonQuery()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
' The connection is automatically closed when the
' code exits the Using block.
End Using
End Sub
注解
类 OdbcCommand 提供以下方法用于针对数据源执行命令:
项 | 说明 |
---|---|
ExecuteReader | 执行返回行的命令。 |
ExecuteNonQuery | 执行 SQL INSERT、DELETE、UPDATE 和 SET 语句等命令。 |
ExecuteScalar | 从数据库检索单个值,例如聚合值。 |
可以重置 CommandText 属性并重复使用 OdbcCommand 对象。 但是,必须先关闭 , OdbcDataReader 然后才能执行新的或以前的命令。
如果命令的执行导致致命OdbcException的(例如SQL Server严重性级别为 20 或更高),OdbcConnection可能会关闭。 但是,用户可以重新打开连接并继续操作。
构造函数
OdbcCommand() |
初始化 OdbcCommand 类的新实例。 |
OdbcCommand(String) |
使用查询的文本初始化 OdbcCommand 类的新实例。 |
OdbcCommand(String, OdbcConnection) |
用查询文本和 OdbcCommand 对象初始化 OdbcConnection 类的新实例。 |
OdbcCommand(String, OdbcConnection, OdbcTransaction) |
使用查询文本、OdbcConnection 对象以及 Transaction 初始化 OdbcCommand 类的新实例。 |
属性
CanRaiseEvents |
获取一个指示组件是否可以引发事件的值。 (继承自 Component) |
CommandText |
获取或设置要对数据源执行的 SQL 语句或存储过程。 |
CommandTimeout |
获取或设置在终止尝试执行命令并生成错误之前的等待时间(以秒为单位)。 |
CommandType |
获取或设置一个值,该值指示如何解释 CommandText 属性。 |
Connection |
获取或设置 OdbcCommand 的此实例使用的 OdbcConnection。 |
Container |
获取包含 IContainer 的 Component。 (继承自 Component) |
DbConnection |
获取或设置此 DbConnection 使用的 DbCommand。 (继承自 DbCommand) |
DbParameterCollection |
获取 DbParameter 对象的集合。 (继承自 DbCommand) |
DbTransaction |
获取或设置将在其中执行此 DbCommand 对象的 DbTransaction。 (继承自 DbCommand) |
DesignMode |
获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
DesignTimeVisible |
获取或设置一个值,指示该命令对象在自定义界面控件中是否应可见。 |
Events |
获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
Parameters | |
Site | (继承自 Component) |
Transaction |
获取或设置要在其中执行 OdbcTransaction 的 OdbcCommand。 |
UpdatedRowSource |
获取或设置一个值,该值指定 “Update” 方法应如何将命令结果应用于“DataRow”。 |
方法
事件
Disposed |
在通过调用 Dispose() 方法释放组件时发生。 (继承自 Component) |
显式接口实现
ICloneable.Clone() |
有关此成员的说明,请参见 Clone()。 |
IDbCommand.Connection |
获取或设置 IDbCommand 的此实例使用的 IDbConnection。 (继承自 DbCommand) |
IDbCommand.CreateParameter() |
创建 IDbDataParameter 对象的新实例。 |
IDbCommand.CreateParameter() |
创建 IDbDataParameter 对象的新实例。 (继承自 DbCommand) |
IDbCommand.ExecuteReader() |
此 API 支持产品基础结构,不能在代码中直接使用。 对 Connection 执行 CommandText 并生成 IDataReader。 |
IDbCommand.ExecuteReader() |
对 Connection 执行 CommandText 并生成 IDataReader。 (继承自 DbCommand) |
IDbCommand.ExecuteReader(CommandBehavior) |
此 API 支持产品基础结构,不能在代码中直接使用。 针对 Connection 执行 CommandText,并使用指定行为生成 IDataReader。 |
IDbCommand.ExecuteReader(CommandBehavior) |
对 Connection 执行 CommandText,并使用 CommandBehavior 值之一生成 IDataReader。 (继承自 DbCommand) |
IDbCommand.Parameters | (继承自 DbCommand) |
IDbCommand.Transaction |
获取或设置将在其中执行此 DbCommand 对象的 DbTransaction。 (继承自 DbCommand) |
扩展方法
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可处置项返回的任务的等待。 |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈