SqlCommand Konstruktoren

Definition

Überlädt

Name Beschreibung
SqlCommand()

Initialisiert eine neue Instanz der SqlCommand-Klasse.

SqlCommand(String)

Initialisiert eine neue Instanz der SqlCommand Klasse mit dem Text der Abfrage.

SqlCommand(String, SqlConnection)

Initialisiert eine neue Instanz der SqlCommand Klasse mit dem Text der Abfrage und einer SqlConnection.

SqlCommand(String, SqlConnection, SqlTransaction)

Initialisiert eine neue Instanz der SqlCommand Klasse mit dem Text der Abfrage, a SqlConnectionund der SqlTransaction.

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Initialisiert eine neue Instanz der SqlCommand Klasse mit angegebenem Befehlstext, Verbindung, Transaktion und Verschlüsselungseinstellung.

SqlCommand()

Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs

Initialisiert eine neue Instanz der SqlCommand-Klasse.

public:
 SqlCommand();
public SqlCommand();
Public Sub New ()

Beispiele

Im folgenden Beispiel wird eine SqlCommand Eigenschaft erstellt und festgelegt 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;
    }
}

Hinweise

Der Basiskonstruktor initialisiert alle Felder mit ihren Standardwerten. In der folgenden Tabelle sind die anfänglichen Eigenschaftswerte für eine Instanz von SqlCommandaufgeführt.

EigenschaftenAusgangswert
CommandText leere Zeichenfolge ("")
CommandTimeout 30
CommandType Text
Connection null

Sie können den Wert für eine dieser Eigenschaften durch einen separaten Aufruf der Eigenschaft ändern.

Gilt für:

SqlCommand(String)

Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs

Initialisiert eine neue Instanz der SqlCommand Klasse mit dem Text der Abfrage.

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)

Parameter

cmdText
String

Der Text der Abfrage.

Beispiele

Im folgenden Beispiel wird ein SqlCommandBefehlstext erstellt, der übergeben wird.

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;
    }
}

Hinweise

Wenn eine Instanz erstellt SqlCommand wird, werden die folgenden Lese-/Schreibeigenschaften auf Anfangswerte festgelegt.

EigenschaftenAusgangswert
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection null

Sie können den Wert für eine dieser Eigenschaften durch einen separaten Aufruf der Eigenschaft ändern.

Gilt für:

SqlCommand(String, SqlConnection)

Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs

Initialisiert eine neue Instanz der SqlCommand Klasse mit dem Text der Abfrage und einer SqlConnection.

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)

Parameter

cmdText
String

Der Text der Abfrage.

connection
SqlConnection

Eine SqlConnection, die die Verbindung mit einer Instanz von SQL Server darstellt.

Beispiele

Im folgenden Beispiel wird ein SqlCommand Teil seiner Eigenschaften erstellt und festgelegt.

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]));
                }
            }
        }
    }
}

Hinweise

In der folgenden Tabelle sind die anfänglichen Eigenschaftswerte für eine Instanz von SqlCommandaufgeführt.

EigenschaftenAusgangswert
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection Ein neues SqlConnection , das der Wert für den connection Parameter ist.

Sie können den Wert für jeden dieser Parameter ändern, indem Sie die zugehörige Eigenschaft festlegen.

Gilt für:

SqlCommand(String, SqlConnection, SqlTransaction)

Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs

Initialisiert eine neue Instanz der SqlCommand Klasse mit dem Text der Abfrage, a SqlConnectionund der SqlTransaction.

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)

Parameter

cmdText
String

Der Text der Abfrage.

connection
SqlConnection

Eine SqlConnection, die die Verbindung mit einer Instanz von SQL Server darstellt.

transaction
SqlTransaction

Der SqlTransaction , in dem die SqlCommand Ausführung ausgeführt wird.

Hinweise

In der folgenden Tabelle sind die anfänglichen Eigenschaftswerte für eine Instanz von SqlCommandaufgeführt.

EigenschaftenAusgangswert
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection Ein neues SqlConnection , das der Wert für den connection Parameter ist.

Sie können den Wert für jeden dieser Parameter ändern, indem Sie die zugehörige Eigenschaft festlegen.

Gilt für:

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs
Quelle:
SqlCommand.cs

Initialisiert eine neue Instanz der SqlCommand Klasse mit angegebenem Befehlstext, Verbindung, Transaktion und Verschlüsselungseinstellung.

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)

Parameter

cmdText
String

Der Text der Abfrage.

connection
SqlConnection

Eine SqlConnection, die die Verbindung mit einer Instanz von SQL Server darstellt.

transaction
SqlTransaction

Der SqlTransaction , in dem die SqlCommand Ausführung ausgeführt wird.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

Die Verschlüsselungseinstellung. Weitere Informationen finden Sie unter Always Encrypted.

Gilt für: