當前位置: 首頁>>代碼示例>>C#>>正文


C# SqlCommand.Parameters屬性代碼示例

本文整理匯總了C#中System.Data.SqlClient.SqlCommand.Parameters屬性的典型用法代碼示例。如果您正苦於以下問題:C# SqlCommand.Parameters屬性的具體用法?C# SqlCommand.Parameters怎麽用?C# SqlCommand.Parameters使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在System.Data.SqlClient.SqlCommand的用法示例。


在下文中一共展示了SqlCommand.Parameters屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: UpdateDemographics

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);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Data.SqlClient,代碼行數:30,代碼來源:SqlCommand.Parameters

示例2: Main

//引入命名空間
using System;
using System.Data;
using System.Data.SqlClient;

   class CommandExampleParameters
   {
      static void Main() 
      {
         SqlConnection thisConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");

         SqlCommand nonqueryCommand = thisConnection.CreateCommand();

         try {
            thisConnection.Open();

            nonqueryCommand.CommandText = "CREATE TABLE MyTable (MyName VARCHAR (30), MyNumber integer)";
            Console.WriteLine(nonqueryCommand.CommandText);
            Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());

            nonqueryCommand.CommandText = "INSERT INTO MyTable VALUES (@MyName, @MyNumber)";

            nonqueryCommand.Parameters.Add("@MyName", SqlDbType.VarChar, 30);
            nonqueryCommand.Parameters.Add("@MyNumber", SqlDbType.Int);
        
            nonqueryCommand.Prepare();
        
            string[] names = { "A", "B", "C", "D" } ;
            int i;
            for (i=1; i<=4; i++) {
               nonqueryCommand.Parameters["@MyName"].Value = names[i-1];
               nonqueryCommand.Parameters["@MyNumber"].Value = i;
               Console.WriteLine(nonqueryCommand.CommandText);
               Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());
            }
         } catch (SqlException ex) {
            Console.WriteLine(ex.ToString());
         } finally {  
            thisConnection.Close();
            Console.WriteLine("Connection Closed.");
         }
      }
   }
開發者ID:C#程序員,項目名稱:System.Data.SqlClient,代碼行數:43,代碼來源:SqlCommand.Parameters


注:本文中的System.Data.SqlClient.SqlCommand.Parameters屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。