SqlParameter.Value : SqlParameter « System.Data.SqlClient « C# / C Sharp by API






SqlParameter.Value

 

using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
    public static void Main()
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
            con.Open();
            
            string employeeID =  "5";
            string title = "Cleaner";
            
            using (SqlCommand com = con.CreateCommand())
            {
                com.CommandType = CommandType.Text;
                com.CommandText = "UPDATE Employee SET Title = @title" +
                    " WHERE Id = @Employeeid";
    
                // Create a SqlParameter object for the title parameter.
                SqlParameter p1 = com.CreateParameter();
                p1.ParameterName = "@title";
                p1.SqlDbType = SqlDbType.VarChar;
                p1.Value = title;
                com.Parameters.Add(p1);
    
                // Use a shorthand syntax to add the id parameter.
                com.Parameters.Add("@Employeeid", SqlDbType.Int).Value = employeeID;
    
                // Execute the command and process the result.
                int result = com.ExecuteNonQuery();
    
                if (result == 1)
                {
                    Console.WriteLine("Employee {0} title updated to {1}.",
                        employeeID, title);
                }
                else
                {
                    Console.WriteLine("Employee {0} title not updated.",
                        employeeID);
                }
            }
        }
    }
}

   
  








Related examples in the same category

1.new SqlParameter
2.SqlParameter.ParameterName
3.SqlParameter.SourceVersion