SqlCommand.Parameters Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
SqlParameterCollectionZíská .
public:
property System::Data::SqlClient::SqlParameterCollection ^ Parameters { System::Data::SqlClient::SqlParameterCollection ^ get(); };
[System.Data.DataSysDescription("DbCommand_Parameters")]
public System.Data.SqlClient.SqlParameterCollection Parameters { get; }
public System.Data.SqlClient.SqlParameterCollection Parameters { get; }
[<System.Data.DataSysDescription("DbCommand_Parameters")>]
member this.Parameters : System.Data.SqlClient.SqlParameterCollection
member this.Parameters : System.Data.SqlClient.SqlParameterCollection
Public ReadOnly Property Parameters As SqlParameterCollection
Hodnota vlastnosti
Parametry příkazu Transact-SQL nebo uložené procedury. Výchozí hodnota je prázdná kolekce.
- Atributy
Příklady
Následující příklad ukazuje, jak vytvořit SqlCommand a přidat parametry do SqlParameterCollection.
private static void UpdateDemographics(Int32 customerID,
string demoXml, string connectionString)
{
// Update the demographics for a store, which is stored
// in an xml column.
string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
+ "WHERE CustomerID = @ID;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters["@ID"].Value = customerID;
// Use AddWithValue to assign Demographics.
// SQL Server will implicitly convert strings into XML.
command.Parameters.AddWithValue("@demographics", demoXml);
try
{
connection.Open();
Int32 rowsAffected = command.ExecuteNonQuery();
Console.WriteLine("RowsAffected: {0}", rowsAffected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Private Sub UpdateDemographics(ByVal customerID As Integer, _
ByVal demoXml As String, _
ByVal connectionString As String)
' Update the demographics for a store, which is stored
' in an xml column.
Dim commandText As String = _
"UPDATE Sales.Store SET Demographics = @demographics " _
& "WHERE CustomerID = @ID;"
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(commandText, connection)
' Add CustomerID parameter for WHERE clause.
command.Parameters.Add("@ID", SqlDbType.Int)
command.Parameters("@ID").Value = customerID
' Use AddWithValue to assign Demographics.
' SQL Server will implicitly convert strings into XML.
command.Parameters.AddWithValue("@demographics", demoXml)
Try
connection.Open()
Dim rowsAffected As Integer = command.ExecuteNonQuery()
Console.WriteLine("RowsAffected: {0}", rowsAffected)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
Poznámky
Microsoft .NET Framework Zprostředkovatel dat pro SQL Server nepodporuje zástupný symbol otazníku (?) pro předávání parametrů do příkazu SQL nebo uložené procedury volané příkazem CommandType.Text. V tomto případě se musí použít pojmenované parametry. Příklad:
SELECT * FROM Customers WHERE CustomerID = @CustomerID
Note
Pokud parametry v kolekci neodpovídají požadavkům dotazu, které se mají provést, může dojít k chybě.
Další informace naleznete v tématu Konfigurace parametrů a datových typů parametrů.