SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE Age > @Age", conn); cmd.Parameters.Add(new SqlParameter("@Age", 18));
SqlCommand cmd = new SqlCommand("UpdateCustomer", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@CustomerId", 123)); cmd.Parameters.Add(new SqlParameter("@FirstName", "John")); cmd.Parameters.Add(new SqlParameter("@LastName", "Doe")); cmd.Parameters.Add(new SqlParameter("@Email", "[email protected]"));
SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE Price > @Price", conn); cmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = 100.00m;This code creates a SqlCommand object with a parameterized SQL query to select all products whose price is greater than 100. The Add method is used with an SqlParameter object that specifies the data type as Decimal and a value of 100.00m. In order to use the SqlCommand class, you need to have the System.Data.SqlClient package installed in your project. You can install this package using the NuGet package manager in Visual Studio or by adding it to your project file manually.