共用方式為


SqlCommand 類別

定義

代表一個 Transact-SQL 語句或儲存程序,需對 SQL Server 資料庫執行。 無法繼承這個類別。

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
繼承
SqlCommand
繼承
繼承
實作

範例

以下範例可產生一個SqlConnection、一個SqlCommand、一個。SqlDataReader 範例會讀取資料,並將其寫入主控台。 最後,範例會關閉 ,SqlDataReader然後在退出Using程式碼區塊時關閉 。SqlConnection

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 資料庫執行指令的方法:

項目 說明
BeginExecuteNonQuery 啟動非同步執行由此 SqlCommand描述的 Transact-SQL 語句或儲存程序,通常執行如 INSERT、DELETE、UPDATE 和 SET 等指令。 每次呼叫 必須 BeginExecuteNonQuery 與 呼叫 EndExecuteNonQuery配對,該呼叫完成操作,通常在獨立執行緒上完成。
BeginExecuteReader 啟動非同步執行由此 SqlCommand 描述的 Transact-SQL 語句或儲存程序,並從伺服器擷取一個或多個結果集。 每次呼叫 必須 BeginExecuteReader 與 呼叫 EndExecuteReader配對,該呼叫完成操作,通常在獨立執行緒上完成。
BeginExecuteXmlReader 啟動非同步執行由此 SqlCommand描述的 Transact-SQL 語句或儲存程序。 每次呼叫 必須 BeginExecuteXmlReader 與 的 EndExecuteXmlReader呼叫配對,該呼叫完成操作,通常在獨立執行緒上完成,並回傳一個 XmlReader 物件。
ExecuteReader 執行回傳列的指令。 為了提升效能,會 ExecuteReader 使用 Transact-SQL sp_executesql 系統儲存程序來呼叫指令。 因此,如果用來 ExecuteReader 執行像 Transact-SQL SET 語句這類指令,可能不會有你想要的效果。
ExecuteNonQuery 執行 INSERT、DELETE、UPDATE 和 SET Transact-SQL 等指令。
ExecuteScalar 從資料庫中取得單一值(例如彙總值)。
ExecuteXmlReader CommandText 傳送到 並 Connection 建立一個 XmlReader 物件。

你可以重置屬性 CommandText 並重複使用該 SqlCommand 物件。 不過,你必須先關閉 , SqlDataReader 才能執行新的或之前的指令。

若 a SqlException 是由執行 的方法 SqlCommand產生,則當嚴重程度等級低於 19 時,該 SqlConnection 保持開啟狀態。 當嚴重程度等級達到 20 或以上時,伺服器通常會關閉 SqlConnection。 不過,使用者可以重新開啟連線並繼續使用。

備註

無名(又稱序數)參數不被 .NET Framework 資料提供者支援。

建構函式

名稱 Description
SqlCommand()

初始化 SqlCommand 類別的新執行個體。

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

初始化一個新的類別實例 SqlCommand ,並指定命令文字、連線、交易及加密設定。

SqlCommand(String, SqlConnection, SqlTransaction)

初始化一個新的類別實例 SqlCommand ,包含查詢文本 a SqlConnection、 和 SqlTransaction

SqlCommand(String, SqlConnection)

初始化一個新的類別實例 SqlCommand ,包含查詢文本與 SqlConnection

SqlCommand(String)

初始化一個新的類別實例 SqlCommand ,並使用查詢的文字。

屬性

名稱 Description
CanRaiseEvents

會得到一個值,表示該元件是否能引發事件。

(繼承來源 Component)
ColumnEncryptionSetting

取得或設定此指令的欄位加密設定。

CommandText

取得或設定在資料來源執行的 Transact-SQL 語句、資料表名稱或儲存程序。

CommandTimeout

在終止執行指令並產生錯誤前,取得或設定等待時間(以秒為單位)。

CommandType

取得或設定一個值,指示該 CommandText 性質的解釋方式。

Connection

取得或設定 SqlConnection 此實例 SqlCommand所使用的。

Container

得到 IContainer 包含 Component的 。

(繼承來源 Component)
DbConnection

得到或設定 由 DbConnection 所使用 DbCommand

(繼承來源 DbCommand)
DbParameterCollection

取得物品集合 DbParameter

(繼承來源 DbCommand)
DbTransaction

取得或設定該DbCommand物件執行的內部。DbTransaction

(繼承來源 DbCommand)
DesignMode

會得到一個值,表示目前 Component 是否處於設計模式。

(繼承來源 Component)
DesignTimeVisible

取得或設定一個值,指示指令物件是否應該在 Windows 表單設計器控制項中顯示。

Events

會取得與此 Component連結的事件處理程序清單。

(繼承來源 Component)
Notification

取得或設定一個值,指定綁定到此指令的物件。SqlNotificationRequest

NotificationAutoEnlist

取得或設定一個值,指示應用程式是否應該自動接收來自共同 SqlDependency 物件的查詢通知。

Parameters

取得 SqlParameterCollection

Site

取得或設定 ISiteComponent

(繼承來源 Component)
Transaction

取得或設定 SqlTransaction 執行的內部 SqlCommand

UpdatedRowSource

Update 方法DbDataAdapter使用時,取得或設定指令結果如何套用DataRow於 。

方法

名稱 Description
BeginExecuteNonQuery()

啟動非同步執行由此 SqlCommand描述的 Transact-SQL 語句或儲存程序。

BeginExecuteNonQuery(AsyncCallback, Object)

在回調程序與狀態資訊下,啟動由此 SqlCommand描述的 Transact-SQL 語句或儲存程序的非同步執行。

BeginExecuteReader()

啟動非同步執行由此 SqlCommand描述的 Transact-SQL 語句或儲存程序,並從伺服器取得一個或多個結果集。

BeginExecuteReader(AsyncCallback, Object, CommandBehavior)

啟動非同步執行由此 SqlCommand描述的 Transact-SQL 語句或儲存程序,使用其中一個 CommandBehavior 值,並從伺服器取得一個或多個結果集,前提是有回調程序與狀態資訊。

BeginExecuteReader(AsyncCallback, Object)

啟動由此 SqlCommand 描述的 Transact-SQL 語句或儲存程序的非同步執行,並從伺服器取得一個或多個結果集,給定回調程序與狀態資訊。

BeginExecuteReader(CommandBehavior)

啟動非同步執行由此 SqlCommand 描述的 Transact-SQL 語句或儲存程序,並使用其中一個 CommandBehavior 值。

BeginExecuteXmlReader()

啟動非同步執行由此 SqlCommand 描述的 Transact-SQL 語句或儲存程序,並將結果以 XmlReader 物件形式回傳。

BeginExecuteXmlReader(AsyncCallback, Object)

啟動非同步執行由此 SqlCommand 描述的 Transact-SQL 語句或儲存程序,並以回調程序回傳結果 XmlReader 為物件。

Cancel()

嘗試取消執行一個 SqlCommand

Clone()

建立一個新的 SqlCommand 物件,該物件是目前實例的複製品。

CreateDbParameter()

建立一個新的物件實例 DbParameter

(繼承來源 DbCommand)
CreateObjRef(Type)

建立一個物件,包含產生代理伺服器所需的所有相關資訊,用於與遠端物件通訊。

(繼承來源 MarshalByRefObject)
CreateParameter()

建立一個新的物件實例 SqlParameter

Dispose()

執行與釋放、釋放或重置未管理資源相關的應用程式定義任務。

(繼承來源 DbCommand)
Dispose()

釋放所有由 Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 未管理的資源, DbCommand 並可選擇性地釋放受管理資源。

(繼承來源 DbCommand)
Dispose(Boolean)

釋放 未管理的資源, Component 並可選擇性地釋放受管理資源。

(繼承來源 Component)
EndExecuteNonQuery(IAsyncResult)

完成非同步執行 Transact-SQL 語句。

EndExecuteReader(IAsyncResult)

完成非同步執行 Transact-SQL 語句,回傳請求 SqlDataReader的 。

EndExecuteXmlReader(IAsyncResult)

完成非同步執行 Transact-SQL 語句,將請求的資料以 XML 格式回傳。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
ExecuteDbDataReader(CommandBehavior)

對其連線執行指令,回傳可用來存取結果的 a DbDataReader

(繼承來源 DbCommand)
ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)

提供者應實作此方法,以提供非預設的超載實 ExecuteReader 作。

預設實作會呼叫同步 ExecuteReader() 方法並回傳已完成的任務,阻擋呼叫執行緒。 預設實作若傳遞已取消的取消標記,會回傳已取消的任務。 ExecuteReader 拋出的異常會透過回傳的任務異常屬性來傳達。

此方法接受一個取消標記,可用來請求提前取消操作。 實施單位可能會忽略此請求。

(繼承來源 DbCommand)
ExecuteNonQuery()

對連線執行 Transact-SQL 陳述式,並回傳受影響的列數。

ExecuteNonQueryAsync()

非同步版本 ExecuteNonQuery()的 ,該指令會對其連接物件執行指令,並回傳受影響的列數。

使用 CancellationToken.None 來調用 ExecuteNonQueryAsync(CancellationToken)

(繼承來源 DbCommand)
ExecuteNonQueryAsync(CancellationToken)

非同步版本 ExecuteNonQuery()的 ,會對連線執行 Transact-SQL 陳述式,並回傳受影響的列數。 取消標記可用來請求在指令逾時前放棄操作。 例外將透過回傳的任務物件回報。

ExecuteReader()

CommandText 傳送到 並 Connection 建立 SqlDataReader

ExecuteReader(CommandBehavior)

CommandText 傳送到 Connection,並利用其中一個CommandBehavior值來建立 。SqlDataReader

ExecuteReaderAsync()

一個非同步版本的 ExecuteReader(),將 傳送 CommandText 到 並 ConnectionSqlDataReader構 。 例外將透過回傳的任務物件回報。

ExecuteReaderAsync(CancellationToken)

一個非同步版本的 ExecuteReader(),將 傳送 CommandText 到 並 ConnectionSqlDataReader構 。

取消標記可用來請求在指令逾時前放棄操作。 例外將透過回傳的任務物件回報。

ExecuteReaderAsync(CommandBehavior, CancellationToken)

一個非同步版本 ExecuteReader(CommandBehavior)的 ,將 傳送 CommandTextConnection,並建立 SqlDataReader

取消標記可用來請求在指令逾時前放棄操作。 例外將透過回傳的任務物件回報。

ExecuteReaderAsync(CommandBehavior)

一個非同步版本 ExecuteReader(CommandBehavior)的 ,將 傳送 CommandTextConnection,並建立 SqlDataReader。 例外將透過回傳的任務物件回報。

ExecuteScalar()

執行查詢,並回傳查詢回傳結果集第一列的第一欄。 額外的欄位或列則被忽略。

ExecuteScalarAsync()

一個非同步版本的 ExecuteScalar(),執行指令並回傳第一個回傳結果集第一列的第一欄。 其他欄位、列和結果集則被忽略。

使用 CancellationToken.None 來調用 ExecuteScalarAsync(CancellationToken)

(繼承來源 DbCommand)
ExecuteScalarAsync(CancellationToken)

非同步版本 ExecuteScalar()的 ,會非同步執行查詢,並回傳查詢回傳結果集第一列的第一欄。 額外的欄位或列則被忽略。

取消標記可用來請求在指令逾時前放棄操作。 例外將透過回傳的任務物件回報。

ExecuteXmlReader()

CommandText 傳送到 並 Connection 建立一個 XmlReader 物件。

ExecuteXmlReaderAsync()

一個非同步版本的 ExecuteXmlReader(),將 傳送 CommandText 到 並 Connection 建立一個 XmlReader 物件。

例外將透過回傳的任務物件回報。

ExecuteXmlReaderAsync(CancellationToken)

一個非同步版本的 ExecuteXmlReader(),將 傳送 CommandText 到 並 Connection 建立一個 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 實例上建立已準備好的指令版本。

ResetCommandTimeout()

將屬性 CommandTimeout 重置為預設值。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
ToString()

回傳 String 包含 的名稱 Component(若有的話)。 此方法不應被覆蓋。

(繼承來源 Component)

事件

名稱 Description
Disposed

當元件被呼叫方法 Dispose() 時會發生。

(繼承來源 Component)
StatementCompleted

當執行 Transact-SQL 語句完成時發生。

明確介面實作

名稱 Description
ICloneable.Clone()

建立一個新的 SqlCommand 物件,該物件是目前實例的複製品。

IDbCommand.Connection

取得或設定 IDbConnection 此實例 IDbCommand所使用的。

(繼承來源 DbCommand)
IDbCommand.CreateParameter()

建立一個新的物件實例 SqlParameter

IDbCommand.CreateParameter()

建立一個新的物件實例 IDbDataParameter

(繼承來源 DbCommand)
IDbCommand.ExecuteReader()

CommandText 傳送到 Connection,並構建 SqlDataReader

IDbCommand.ExecuteReader()

執行 對 CommandTextConnection 構建 IDataReader

(繼承來源 DbCommand)
IDbCommand.ExecuteReader(CommandBehavior)

CommandText 傳送到 Connection,並利用其中一個CommandBehavior值來建立 。SqlDataReader

IDbCommand.ExecuteReader(CommandBehavior)

對 執行 ,CommandTextConnection並利用其中一個CommandBehavior值建立 IDataReader

(繼承來源 DbCommand)
IDbCommand.Parameters

取得 IDataParameterCollection

(繼承來源 DbCommand)
IDbCommand.Transaction

取得或設定該DbCommand物件執行的內部。DbTransaction

(繼承來源 DbCommand)

適用於

另請參閱