SqlCommand 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示针对SQL Server数据库执行的Transact-SQL语句或存储过程。 此类不能被继承。
对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。
public ref class SqlCommand sealed : System::Data::Common::DbCommand
public ref class SqlCommand sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbCommand
public ref class SqlCommand sealed : System::Data::Common::DbCommand, ICloneable
public sealed class SqlCommand : System.Data.Common.DbCommand
public sealed class SqlCommand : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbCommand
public sealed class SqlCommand : System.Data.Common.DbCommand, ICloneable
type SqlCommand = class
inherit DbCommand
type SqlCommand = class
inherit Component
interface IDbCommand
interface IDisposable
interface ICloneable
type SqlCommand = class
inherit DbCommand
interface ICloneable
Public NotInheritable Class SqlCommand
Inherits DbCommand
Public NotInheritable Class SqlCommand
Inherits Component
Implements ICloneable, IDbCommand, IDisposable
Public NotInheritable Class SqlCommand
Inherits DbCommand
Implements ICloneable
- 继承
- 继承
- 继承
- 实现
示例
以下示例创建一个 SqlConnection、一个 SqlCommand和一个 SqlDataReader。 该示例读取数据,并将其写入控制台。 最后,该示例关闭代码块,然后在SqlDataReader退出代码块时SqlConnection关闭Using该示例。
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(
queryString, connection);
connection.Open();
using(SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}
}
}
}
Public Sub ReadOrderData(ByVal connectionString As String)
Dim queryString As String = _
"SELECT OrderID, CustomerID FROM dbo.Orders;"
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
Try
While reader.Read()
Console.WriteLine(String.Format("{0}, {1}", _
reader(0), reader(1)))
End While
Finally
' Always call Close when done reading.
reader.Close()
End Try
End Using
End Sub
以下示例演示如何创建和执行不同类型的 SqlCommand 对象。
首先,必须执行以下脚本来创建示例数据库:
USE [master]
GO
CREATE DATABASE [MySchool]
GO
USE [MySchool]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CourseExtInfo] @CourseId int
as
select c.CourseID,c.Title,c.Credits,d.Name as DepartmentName
from Course as c left outer join Department as d on c.DepartmentID=d.DepartmentID
where c.CourseID=@CourseId
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[DepartmentInfo] @DepartmentId int,@CourseCount int output
as
select @CourseCount=Count(c.CourseID)
from course as c
where c.DepartmentID=@DepartmentId
select d.DepartmentID,d.Name,d.Budget,d.StartDate,d.Administrator
from Department as d
where d.DepartmentID=@DepartmentId
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[GetDepartmentsOfSpecifiedYear]
@Year int,@BudgetSum money output
AS
BEGIN
SELECT @BudgetSum=SUM([Budget])
FROM [MySchool].[dbo].[Department]
Where YEAR([StartDate])=@Year
SELECT [DepartmentID]
,[Name]
,[Budget]
,[StartDate]
,[Administrator]
FROM [MySchool].[dbo].[Department]
Where YEAR([StartDate])=@Year
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Course]([CourseID] [nvarchar](10) NOT NULL,
[Year] [smallint] NOT NULL,
[Title] [nvarchar](100) NOT NULL,
[Credits] [int] NOT NULL,
[DepartmentID] [int] NOT NULL,
CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED
(
[CourseID] ASC,
[Year] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Department]([DepartmentID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Budget] [money] NOT NULL,
[StartDate] [datetime] NOT NULL,
[Administrator] [int] NULL,
CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED
(
[DepartmentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Person]([PersonID] [int] IDENTITY(1,1) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[HireDate] [datetime] NULL,
[EnrollmentDate] [datetime] NULL,
CONSTRAINT [PK_School.Student] PRIMARY KEY CLUSTERED
(
[PersonID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[StudentGrade]([EnrollmentID] [int] IDENTITY(1,1) NOT NULL,
[CourseID] [nvarchar](10) NOT NULL,
[StudentID] [int] NOT NULL,
[Grade] [decimal](3, 2) NOT NULL,
CONSTRAINT [PK_StudentGrade] PRIMARY KEY CLUSTERED
(
[EnrollmentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[EnglishCourse]
as
select c.CourseID,c.Title,c.Credits,c.DepartmentID
from Course as c join Department as d on c.DepartmentID=d.DepartmentID
where d.Name=N'English'
GO
INSERT [dbo].[Course] ([CourseID], [Year], [Title], [Credits], [DepartmentID]) VALUES (N'C1045', 2012, N'Calculus', 4, 7)
INSERT [dbo].[Course] ([CourseID], [Year], [Title], [Credits], [DepartmentID]) VALUES (N'C1061', 2012, N'Physics', 4, 1)
INSERT [dbo].[Course] ([CourseID], [Year], [Title], [Credits], [DepartmentID]) VALUES (N'C2021', 2012, N'Composition', 3, 2)
INSERT [dbo].[Course] ([CourseID], [Year], [Title], [Credits], [DepartmentID]) VALUES (N'C2042', 2012, N'Literature', 4, 2)
SET IDENTITY_INSERT [dbo].[Department] ON
INSERT [dbo].[Department] ([DepartmentID], [Name], [Budget], [StartDate], [Administrator]) VALUES (1, N'Engineering', 350000.0000, CAST(0x0000999C00000000 AS DateTime), 2)
INSERT [dbo].[Department] ([DepartmentID], [Name], [Budget], [StartDate], [Administrator]) VALUES (2, N'English', 120000.0000, CAST(0x0000999C00000000 AS DateTime), 6)
INSERT [dbo].[Department] ([DepartmentID], [Name], [Budget], [StartDate], [Administrator]) VALUES (4, N'Economics', 200000.0000, CAST(0x0000999C00000000 AS DateTime), 4)
INSERT [dbo].[Department] ([DepartmentID], [Name], [Budget], [StartDate], [Administrator]) VALUES (7, N'Mathematics', 250024.0000, CAST(0x0000999C00000000 AS DateTime), 3)
SET IDENTITY_INSERT [dbo].[Department] OFF
SET IDENTITY_INSERT [dbo].[Person] ON
INSERT [dbo].[Person] ([PersonID], [LastName], [FirstName], [HireDate], [EnrollmentDate]) VALUES (1, N'Hu', N'Nan', NULL, CAST(0x0000A0BF00000000 AS DateTime))
INSERT [dbo].[Person] ([PersonID], [LastName], [FirstName], [HireDate], [EnrollmentDate]) VALUES (2, N'Norman', N'Laura', NULL, CAST(0x0000A0BF00000000 AS DateTime))
INSERT [dbo].[Person] ([PersonID], [LastName], [FirstName], [HireDate], [EnrollmentDate]) VALUES (3, N'Olivotto', N'Nino', NULL, CAST(0x0000A0BF00000000 AS DateTime))
INSERT [dbo].[Person] ([PersonID], [LastName], [FirstName], [HireDate], [EnrollmentDate]) VALUES (4, N'Anand', N'Arturo', NULL, CAST(0x0000A0BF00000000 AS DateTime))
INSERT [dbo].[Person] ([PersonID], [LastName], [FirstName], [HireDate], [EnrollmentDate]) VALUES (5, N'Jai', N'Damien', NULL, CAST(0x0000A0BF00000000 AS DateTime))
INSERT [dbo].[Person] ([PersonID], [LastName], [FirstName], [HireDate], [EnrollmentDate]) VALUES (6, N'Holt', N'Roger', CAST(0x000097F100000000 AS DateTime), NULL)
INSERT [dbo].[Person] ([PersonID], [LastName], [FirstName], [HireDate], [EnrollmentDate]) VALUES (7, N'Martin', N'Randall', CAST(0x00008B1A00000000 AS DateTime), NULL)
SET IDENTITY_INSERT [dbo].[Person] OFF
SET IDENTITY_INSERT [dbo].[StudentGrade] ON
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (1, N'C1045', 1, CAST(3.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (2, N'C1045', 2, CAST(3.00 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (3, N'C1045', 3, CAST(2.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (4, N'C1045', 4, CAST(4.00 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (5, N'C1045', 5, CAST(3.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (6, N'C1061', 1, CAST(4.00 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (7, N'C1061', 3, CAST(3.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (8, N'C1061', 4, CAST(2.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (9, N'C1061', 5, CAST(1.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (10, N'C2021', 1, CAST(2.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (11, N'C2021', 2, CAST(3.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (12, N'C2021', 4, CAST(3.00 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (13, N'C2021', 5, CAST(3.00 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (14, N'C2042', 1, CAST(2.00 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (15, N'C2042', 2, CAST(3.50 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (16, N'C2042', 3, CAST(4.00 AS Decimal(3, 2)))
INSERT [dbo].[StudentGrade] ([EnrollmentID], [CourseID], [StudentID], [Grade]) VALUES (17, N'C2042', 5, CAST(3.00 AS Decimal(3, 2)))
SET IDENTITY_INSERT [dbo].[StudentGrade] OFF
ALTER TABLE [dbo].[Course] WITH CHECK ADD CONSTRAINT [FK_Course_Department] FOREIGN KEY([DepartmentID])
REFERENCES [dbo].[Department] ([DepartmentID])
GO
ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_Department]
GO
ALTER TABLE [dbo].[StudentGrade] WITH CHECK ADD CONSTRAINT [FK_StudentGrade_Student] FOREIGN KEY([StudentID])
REFERENCES [dbo].[Person] ([PersonID])
GO
ALTER TABLE [dbo].[StudentGrade] CHECK CONSTRAINT [FK_StudentGrade_Student]
GO
接下来,编译并执行以下内容:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
class Program {
static class SqlHelper {
// Set the connection, command, and then execute the command with non query.
public static Int32 ExecuteNonQuery(String connectionString, String commandText,
CommandType commandType, params SqlParameter[] parameters) {
using (SqlConnection conn = new SqlConnection(connectionString)) {
using (SqlCommand cmd = new SqlCommand(commandText, conn)) {
// There're three command types: StoredProcedure, Text, TableDirect. The TableDirect
// type is only for OLE DB.
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
// Set the connection, command, and then execute the command and only return one value.
public static Object ExecuteScalar(String connectionString, String commandText,
CommandType commandType, params SqlParameter[] parameters) {
using (SqlConnection conn = new SqlConnection(connectionString)) {
using (SqlCommand cmd = new SqlCommand(commandText, conn)) {
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
return cmd.ExecuteScalar();
}
}
}
// Set the connection, command, and then execute the command with query and return the reader.
public static SqlDataReader ExecuteReader(String connectionString, String commandText,
CommandType commandType, params SqlParameter[] parameters) {
SqlConnection conn = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(commandText, conn)) {
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
// When using CommandBehavior.CloseConnection, the connection will be closed when the
// IDataReader is closed.
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
}
}
static void Main(string[] args) {
String connectionString = "Data Source=(local);Initial Catalog=MySchool;Integrated Security=True;Asynchronous Processing=true;";
CountCourses(connectionString, 2012);
Console.WriteLine();
Console.WriteLine("Following result is the departments that started from 2007:");
GetDepartments(connectionString, 2007);
Console.WriteLine();
Console.WriteLine("Add the credits when the credits of course is lower than 4.");
AddCredits(connectionString, 4);
Console.WriteLine();
Console.WriteLine("Please press any key to exit...");
Console.ReadKey();
}
static void CountCourses(String connectionString, Int32 year) {
String commandText = "Select Count([CourseID]) FROM [MySchool].[dbo].[Course] Where Year=@Year";
SqlParameter parameterYear = new SqlParameter("@Year", SqlDbType.Int);
parameterYear.Value = year;
Object oValue = SqlHelper.ExecuteScalar(connectionString, commandText, CommandType.Text, parameterYear);
Int32 count;
if (Int32.TryParse(oValue.ToString(), out count))
Console.WriteLine("There {0} {1} course{2} in {3}.", count > 1 ? "are" : "is", count, count > 1 ? "s" : null, year);
}
// Display the Departments that start from the specified year.
static void GetDepartments(String connectionString, Int32 year) {
String commandText = "dbo.GetDepartmentsOfSpecifiedYear";
// Specify the year of StartDate
SqlParameter parameterYear = new SqlParameter("@Year", SqlDbType.Int);
parameterYear.Value = year;
// When the direction of parameter is set as Output, you can get the value after
// executing the command.
SqlParameter parameterBudget = new SqlParameter("@BudgetSum", SqlDbType.Money);
parameterBudget.Direction = ParameterDirection.Output;
using (SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, commandText,
CommandType.StoredProcedure, parameterYear, parameterBudget)) {
Console.WriteLine("{0,-20}{1,-20}{2,-20}{3,-20}", "Name", "Budget", "StartDate",
"Administrator");
while (reader.Read()) {
Console.WriteLine("{0,-20}{1,-20:C}{2,-20:d}{3,-20}", reader["Name"],
reader["Budget"], reader["StartDate"], reader["Administrator"]);
}
}
Console.WriteLine("{0,-20}{1,-20:C}", "Sum:", parameterBudget.Value);
}
// If credits of course is lower than the certain value, the method will add the credits.
static void AddCredits(String connectionString, Int32 creditsLow) {
String commandText = "Update [MySchool].[dbo].[Course] Set Credits=Credits+1 Where Credits<@Credits";
SqlParameter parameterCredits = new SqlParameter("@Credits", creditsLow);
Int32 rows = SqlHelper.ExecuteNonQuery(connectionString, commandText, CommandType.Text, parameterCredits);
Console.WriteLine("{0} row{1} {2} updated.", rows, rows > 1 ? "s" : null, rows > 1 ? "are" : "is");
}
}
注解
创建实例 SqlCommand 时,读/写属性将设置为其初始值。 有关这些值的列表,请参阅 SqlCommand 构造函数。
SqlCommand 在 SQL Server 数据库中执行命令的功能如下:
| Item | Description |
|---|---|
| BeginExecuteNonQuery | 启动由此 SqlCommand描述的 Transact-SQL 语句或存储过程的异步执行,通常执行 INSERT、DELETE、UPDATE 和 SET 语句等命令。 每次调用都必须与调用配对,该调用BeginExecuteNonQueryEndExecuteNonQuery完成操作通常位于单独的线程上。 |
| BeginExecuteReader | 启动由此描述 SqlCommand 的 Transact-SQL 语句或存储过程的异步执行,并从服务器检索一个或多个结果集。 每次调用都必须与调用配对,该调用BeginExecuteReaderEndExecuteReader完成操作通常位于单独的线程上。 |
| BeginExecuteXmlReader | 启动由此描述 SqlCommand的 Transact-SQL 语句或存储过程的异步执行。 每次调用都必须与调用配对,该调用BeginExecuteXmlReaderEndExecuteXmlReader完成操作,通常在单独的线程上,并返回一个XmlReader对象。 |
| ExecuteReader | 执行返回行的命令。 为了提高性能, ExecuteReader 请使用 Transact-SQL sp_executesql 系统存储过程调用命令。 因此, ExecuteReader 如果用于执行命令(如 Transact-SQL SET 语句),则可能没有所需的效果。 |
| ExecuteNonQuery | 执行 Transact-SQL INSERT、DELETE、UPDATE 和 SET 语句等命令。 |
| ExecuteScalar | 从数据库检索单个值(例如聚合值)。 |
| ExecuteXmlReader | 将对象 CommandText 发送到 Connection 并生成对象 XmlReader 。 |
可以重置 CommandText 属性并重复使用 SqlCommand 该对象。 但是,必须先关闭该命令 SqlDataReader ,然后才能执行新的或以前的命令。
如果由执行 a 的方法生成 a SqlExceptionSqlCommand, SqlConnection 则当严重性级别为 19 或更少时,将保持打开状态。 当严重级别为 20 或更大时,服务器通常会关闭 。SqlConnection 但是,用户可以重新打开连接并继续。
注释
.NET Framework Data Provider for SQL Server 不支持无名称(也称为序号)参数。
构造函数
| 名称 | 说明 |
|---|---|
| SqlCommand() |
初始化 SqlCommand 类的新实例。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting) |
使用指定的命令文本、连接、事务和加密设置初始化类的新实例 SqlCommand 。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| SqlCommand(String, SqlConnection, SqlTransaction) |
使用查询文本、查询文本和 < 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| SqlCommand(String, SqlConnection) |
使用查询的文本和一个 SqlCommandSqlConnection. 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| SqlCommand(String) |
使用查询的文本初始化类的新实例 SqlCommand 。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
属性
| 名称 | 说明 |
|---|---|
| CanRaiseEvents |
获取一个值,该值指示组件是否可以引发事件。 (继承自 Component) |
| ColumnEncryptionSetting |
获取或设置此命令的列加密设置。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| CommandText |
获取或设置在数据源中执行的 Transact-SQL 语句、表名或存储过程。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| CommandTimeout |
获取或设置在终止尝试执行命令并生成错误之前(以秒为单位)的等待时间。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| CommandType |
获取或设置一个值,该值指示如何 CommandText 解释属性。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Connection |
获取或设置 SqlConnection 此实例 SqlCommand所使用的 。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Container |
IContainer获取包含 .Component (继承自 Component) |
| DbConnection |
获取或设置 DbConnection 由此 DbCommand使用的 。 (继承自 DbCommand) |
| DbParameterCollection |
获取 对象的集合 DbParameter 。 (继承自 DbCommand) |
| DbTransaction |
获取或设置 DbTransaction 此 DbCommand 对象在其中执行的对象。 (继承自 DbCommand) |
| DesignMode |
获取一个值,该值指示当前是否 Component 处于设计模式。 (继承自 Component) |
| DesignTimeVisible |
获取或设置一个值,该值指示命令对象是否应在 Windows 窗体设计器控件中可见。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Events |
获取附加到此 Component对象的事件处理程序的列表。 (继承自 Component) |
| Notification |
获取或设置一个值,该值指定 SqlNotificationRequest 绑定到此命令的对象。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| NotificationAutoEnlist |
获取或设置一个值,该值指示应用程序是否应从公共 SqlDependency 对象自动接收查询通知。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Parameters |
对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Site | (继承自 Component) |
| Transaction |
获取或设置 SqlTransaction 执行范围 SqlCommand 。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| UpdatedRowSource |
获取或设置命令结果应用于 DataRowUpdate 方法 DbDataAdapter的用法。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
方法
| 名称 | 说明 |
|---|---|
| BeginExecuteNonQuery() |
启动由此描述 SqlCommand的 Transact-SQL 语句或存储过程的异步执行。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| BeginExecuteNonQuery(AsyncCallback, Object) |
根据回调过程和状态信息,启动由此 SqlCommand描述的 Transact-SQL 语句或存储过程的异步执行。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| BeginExecuteReader() |
启动由此描述 SqlCommand的 Transact-SQL 语句或存储过程的异步执行,并从服务器检索一个或多个结果集。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| BeginExecuteReader(AsyncCallback, Object, CommandBehavior) |
使用其中一个SqlCommand值并从服务器检索一个或多个结果集(给定回调过程和状态信息)启动由此 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| BeginExecuteReader(AsyncCallback, Object) |
在给定回调过程和状态信息的情况下,启动对此描述 SqlCommand 的 Transact-SQL 语句或存储过程的异步执行,并从服务器检索一个或多个结果集。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| BeginExecuteReader(CommandBehavior) |
使用其中一个SqlCommand值启动 Transact-SQL 语句或存储过程CommandBehavior的异步执行。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| BeginExecuteXmlReader() |
启动由此描述 SqlCommand 的 Transact-SQL 语句或存储过程的异步执行,并将结果作为对象 XmlReader 返回。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| BeginExecuteXmlReader(AsyncCallback, Object) |
启动由此描述 SqlCommand 的 Transact-SQL 语句或存储过程的异步执行,并使用回调过程将结果作为 XmlReader 对象返回。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Cancel() |
尝试取消执行 SqlCommand。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Clone() |
创建一个新 SqlCommand 对象,该对象是当前实例的副本。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| CreateDbParameter() |
创建对象的新实例 DbParameter 。 (继承自 DbCommand) |
| CreateObjRef(Type) |
创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。 (继承自 MarshalByRefObject) |
| CreateParameter() |
创建对象的新实例 SqlParameter 。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Dispose() |
执行与释放、释放或重置非托管资源关联的应用程序定义任务。 (继承自 DbCommand) |
| Dispose() |
释放该 Component命令使用的所有资源。 (继承自 Component) |
| Dispose(Boolean) |
释放由托管资源使用 DbCommand 的非托管资源,并选择性地释放托管资源。 (继承自 DbCommand) |
| Dispose(Boolean) |
释放由托管资源使用 Component 的非托管资源,并选择性地释放托管资源。 (继承自 Component) |
| EndExecuteNonQuery(IAsyncResult) |
完成 Transact-SQL 语句的异步执行。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| EndExecuteReader(IAsyncResult) |
完成 Transact-SQL 语句的异步执行,并返回请求 SqlDataReader的语句。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| EndExecuteXmlReader(IAsyncResult) |
完成 Transact-SQL 语句的异步执行,以 XML 形式返回请求的数据。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| ExecuteDbDataReader(CommandBehavior) |
针对其连接执行命令,返回 DbDataReader 可用于访问结果的命令。 (继承自 DbCommand) |
| ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken) |
提供程序应实现此方法,以便为 ExecuteReader 重载提供非默认实现。 默认实现调用同步 ExecuteReader() 方法并返回已完成的任务,从而阻止调用线程。 如果传递已取消的取消令牌,则默认实现将返回已取消的任务。 ExecuteReader 引发的异常将通过返回的任务异常属性进行通信。 此方法接受一个取消令牌,该令牌可用于请求提前取消操作。 实现可能会忽略此请求。 (继承自 DbCommand) |
| ExecuteNonQuery() |
针对连接执行 Transact-SQL 语句,并返回受影响的行数。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteNonQueryAsync() |
一个异步版本 ExecuteNonQuery(),它对其连接对象执行命令,返回受影响的行数。 使用 ExecuteNonQueryAsync(CancellationToken) CancellationToken.None 调用。 (继承自 DbCommand) |
| ExecuteNonQueryAsync(CancellationToken) |
异步版本,该版本 ExecuteNonQuery()针对连接执行 Transact-SQL 语句,并返回受影响的行数。 取消令牌可用于请求在命令超时之前放弃操作。 异常将通过返回的任务对象报告。
|
| ExecuteReader() |
将 CommandText 它发送到 Connection 并生成一个 SqlDataReader。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteReader(CommandBehavior) |
将值CommandText发送到Connection其中一个值,并生成一个SqlDataReaderCommandBehavior值。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteReaderAsync() |
的异步版本ExecuteReader(),用于将它发送到CommandTextConnection并生成一个 SqlDataReader。 异常将通过返回的任务对象报告。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteReaderAsync(CancellationToken) |
的异步版本ExecuteReader(),用于将它发送到CommandTextConnection并生成一个 SqlDataReader。
|
| ExecuteReaderAsync(CommandBehavior, CancellationToken) |
一个异步版本,该版本
|
| ExecuteReaderAsync(CommandBehavior) |
一个异步版本
|
| ExecuteScalar() |
执行查询,并返回查询返回的结果集中第一行的第一列。 忽略其他列或行。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteScalarAsync() |
一个异步版本,该版本 ExecuteScalar()执行命令并返回第一个返回的结果集中第一行的第一列。 将忽略所有其他列、行和结果集。 使用 ExecuteScalarAsync(CancellationToken) CancellationToken.None 调用。 (继承自 DbCommand) |
| ExecuteScalarAsync(CancellationToken) |
异步版本,该版本 ExecuteScalar()异步执行查询,并返回查询返回的结果集中第一行的第一列。 忽略其他列或行。 取消令牌可用于请求在命令超时之前放弃操作。 异常将通过返回的任务对象报告。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteXmlReader() |
将对象 CommandText 发送到 Connection 并生成对象 XmlReader 。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteXmlReaderAsync() |
一个异步版本ExecuteXmlReader(),用于将对象发送到CommandTextConnection并生成对象XmlReader。 异常将通过返回的任务对象报告。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ExecuteXmlReaderAsync(CancellationToken) |
一个异步版本ExecuteXmlReader(),用于将对象发送到CommandTextConnection并生成对象XmlReader。
|
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetLifetimeService() |
已过时.
检索控制此实例的生存期策略的当前生存期服务对象。 (继承自 MarshalByRefObject) |
| GetService(Type) |
返回一个对象,该对象表示服务由 Component 或其 Container提供的服务。 (继承自 Component) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| InitializeLifetimeService() |
已过时.
获取生存期服务对象来控制此实例的生存期策略。 (继承自 MarshalByRefObject) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。 (继承自 MarshalByRefObject) |
| Prepare() |
在 SQL Server 实例上创建命令的已准备版本。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ResetCommandTimeout() |
将 CommandTimeout 属性重置为其默认值。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
| ToString() |
返回包含 String 的名称 Component(如果有)。 不应重写此方法。 (继承自 Component) |
事件
| 名称 | 说明 |
|---|---|
| Disposed |
当组件通过对方法的调用 Dispose() 释放时发生。 (继承自 Component) |
| StatementCompleted |
执行 Transact-SQL 语句完成时发生。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
显式接口实现
| 名称 | 说明 |
|---|---|
| ICloneable.Clone() |
创建一个新 SqlCommand 对象,该对象是当前实例的副本。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| IDbCommand.Connection |
获取或设置 IDbConnection 此实例 IDbCommand所使用的 。 (继承自 DbCommand) |
| IDbCommand.CreateParameter() |
创建对象的新实例 SqlParameter 。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| IDbCommand.CreateParameter() |
创建对象 IDbDataParameter 的新实例。 (继承自 DbCommand) |
| IDbCommand.ExecuteReader() |
发送到 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| IDbCommand.ExecuteReader() |
CommandText针对Connection并生成一个 IDataReader。 (继承自 DbCommand) |
| IDbCommand.ExecuteReader(CommandBehavior) |
将值CommandText发送到Connection其中一个值,并生成一个SqlDataReaderCommandBehavior值。 对于新开发,请使用Microsoft。Data.SqlClient。 此页面记录 System.Data.SqlClient。 |
| IDbCommand.ExecuteReader(CommandBehavior) |
对CommandText值Connection执行并生成一个IDataReaderCommandBehavior值。 (继承自 DbCommand) |
| IDbCommand.Parameters | (继承自 DbCommand) |
| IDbCommand.Transaction |
获取或设置 DbTransaction 此 DbCommand 对象在其中执行的对象。 (继承自 DbCommand) |