SqlCommand.CommandType 属性

定义

获取或设置一个值,该值指示解释 CommandText 属性的方式。

public:
 virtual property System::Data::CommandType CommandType { System::Data::CommandType get(); void set(System::Data::CommandType value); };
public override System.Data.CommandType CommandType { get; set; }
member this.CommandType : System.Data.CommandType with get, set
Public Overrides Property CommandType As CommandType

属性值

CommandType 值之一。 默认值是Text .

示例

以下示例创建 SqlCommand 并设置其一些属性。

// <Snippet1>
using System;
using System.Xml;
using System.Data;
using Microsoft.Data.SqlClient;
using System.Data.Common;
using System.Windows.Forms;

public class Form1 : Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;


    public void CreateSqlCommand()
    {
        SqlCommand command = new SqlCommand();
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text;
    }
    // </Snippet1>
}

注解

CommandType 属性设置为 StoredProcedure时,应将 CommandText 属性设置为存储过程的名称。 当您调用 Execute 方法之一时,该命令将执行此存储过程。

Microsoft .NET Framework Data Provider for SQL Server 不支持使用问号 () 占位符将参数传递给 SQL 语句或名为 CommandTypeText存储过程。。 在这种情况下,必须使用命名参数。 例如:

SELECT * FROM Customers WHERE CustomerID = @CustomerID

有关详细信息,请参阅 配置参数

适用于