SqlCommand 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| SqlCommand() |
初始化 SqlCommand 类的新实例。 |
| SqlCommand(String) |
使用查询的文本初始化类的新实例 SqlCommand 。 |
| SqlCommand(String, SqlConnection) |
使用查询的文本和一个 SqlCommandSqlConnection. |
| SqlCommand(String, SqlConnection, SqlTransaction) |
使用查询文本、查询文本和 < |
| SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting) |
使用指定的命令文本、连接、事务和加密设置初始化类的新实例 SqlCommand 。 |
SqlCommand()
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
初始化 SqlCommand 类的新实例。
public:
SqlCommand();
public SqlCommand();
Public Sub New ()
示例
以下示例创建并 SqlCommand 设置 CommandTimeout 属性。
using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;
public class Form1 : Form
{
protected DataSet DataSet1;
protected DataGrid dataGrid1;
public void CreateSqlCommand()
{
SqlCommand command = new SqlCommand();
command.CommandTimeout = 15;
command.CommandType = CommandType.Text;
}
}
注解
基构造函数将所有字段初始化为其默认值。 下表显示了实例 SqlCommand的初始属性值。
| 属性 | 初始值 |
|---|---|
| CommandText | 空字符串 (“”) |
| CommandTimeout | 30 |
| CommandType | Text |
| Connection | null |
可以通过对属性的单独调用来更改这些属性的值。
适用于
SqlCommand(String)
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
使用查询的文本初始化类的新实例 SqlCommand 。
public:
SqlCommand(System::String ^ cmdText);
public SqlCommand(string cmdText);
new Microsoft.Data.SqlClient.SqlCommand : string -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String)
参数
- cmdText
- String
查询的文本。
示例
以下示例创建一个 SqlCommand传入命令文本。
using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;
public class Form1 : Form
{
protected DataSet DataSet1;
protected DataGrid dataGrid1;
public void CreateCommand()
{
string queryString = "SELECT * FROM Categories ORDER BY CategoryID";
SqlCommand command = new SqlCommand(queryString);
command.CommandTimeout = 15;
command.CommandType = CommandType.Text;
}
}
注解
创建实例 SqlCommand 时,以下读/写属性设置为初始值。
| 属性 | 初始值 |
|---|---|
| CommandText | cmdText |
| CommandTimeout | 30 |
| CommandType | Text |
| Connection | null |
可以通过对属性的单独调用来更改这些属性的值。
适用于
SqlCommand(String, SqlConnection)
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
使用查询的文本和一个 SqlCommandSqlConnection.
public:
SqlCommand(System::String ^ cmdText, Microsoft::Data::SqlClient::SqlConnection ^ connection);
public SqlCommand(string cmdText, Microsoft.Data.SqlClient.SqlConnection connection);
new Microsoft.Data.SqlClient.SqlCommand : string * Microsoft.Data.SqlClient.SqlConnection -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection)
参数
- cmdText
- String
查询的文本。
- connection
- SqlConnection
一个SqlConnection,表示与SQL Server实例的连接。
示例
以下示例创建 SqlCommand 并设置其一些属性。
using System;
using System.Data;
using Microsoft.Data.SqlClient;
namespace SqlCommandCS
{
class Program
{
static void Main()
{
string str = "Data Source=(local);Initial Catalog=Northwind;"
+ "Integrated Security=SSPI";
string qs = "SELECT OrderID, CustomerID FROM dbo.Orders;";
CreateCommand(qs, str);
}
private static void CreateCommand(string queryString, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
}
}
}
}
}
注解
下表显示了实例 SqlCommand的初始属性值。
| 属性 | 初始值 |
|---|---|
| CommandText | cmdText |
| CommandTimeout | 30 |
| CommandType | Text |
| Connection |
SqlConnection这是参数的值的新connection值。 |
可以通过设置相关属性来更改其中任何参数的值。
适用于
SqlCommand(String, SqlConnection, SqlTransaction)
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
使用查询文本、查询文本和 <
public:
SqlCommand(System::String ^ cmdText, Microsoft::Data::SqlClient::SqlConnection ^ connection, Microsoft::Data::SqlClient::SqlTransaction ^ transaction);
public SqlCommand(string cmdText, Microsoft.Data.SqlClient.SqlConnection connection, Microsoft.Data.SqlClient.SqlTransaction transaction);
new Microsoft.Data.SqlClient.SqlCommand : string * Microsoft.Data.SqlClient.SqlConnection * Microsoft.Data.SqlClient.SqlTransaction -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction)
参数
- cmdText
- String
查询的文本。
- connection
- SqlConnection
一个SqlConnection,表示与SQL Server实例的连接。
- transaction
- SqlTransaction
SqlTransaction在其中SqlCommand执行。
注解
下表显示了实例 SqlCommand的初始属性值。
| 属性 | 初始值 |
|---|---|
| CommandText | cmdText |
| CommandTimeout | 30 |
| CommandType | Text |
| Connection |
SqlConnection这是参数的值的新connection值。 |
可以通过设置相关属性来更改其中任何参数的值。
适用于
SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
- Source:
- SqlCommand.cs
使用指定的命令文本、连接、事务和加密设置初始化类的新实例 SqlCommand 。
public:
SqlCommand(System::String ^ cmdText, Microsoft::Data::SqlClient::SqlConnection ^ connection, Microsoft::Data::SqlClient::SqlTransaction ^ transaction, Microsoft::Data::SqlClient::SqlCommandColumnEncryptionSetting columnEncryptionSetting);
public SqlCommand(string cmdText, Microsoft.Data.SqlClient.SqlConnection connection, Microsoft.Data.SqlClient.SqlTransaction transaction, Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionSetting);
new Microsoft.Data.SqlClient.SqlCommand : string * Microsoft.Data.SqlClient.SqlConnection * Microsoft.Data.SqlClient.SqlTransaction * Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting -> Microsoft.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction, columnEncryptionSetting As SqlCommandColumnEncryptionSetting)
参数
- cmdText
- String
查询的文本。
- connection
- SqlConnection
一个SqlConnection,表示与SQL Server实例的连接。
- transaction
- SqlTransaction
SqlTransaction在其中SqlCommand执行。
- columnEncryptionSetting
- SqlCommandColumnEncryptionSetting
加密设置。 有关详细信息,请参阅 Always Encrypted。