zoukankan      html  css  js  c++  java
  • Attribute学习:AddCustomerCLS 荣

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

    namespace WebApplication1
    {
     /// <summary>
     /// 属性的案例程序。
     /// </summary>
     public class AddCustomerCLS
     {
      public AddCustomerCLS()
      {
       //
       // TODO: 在此处添加构造函数逻辑
       //
      }

      public int AddCustomer2(SqlConnection connection,
       string customerName,
       string country,
       string province,
       string city,
       string address,
       string telephone)
      {
       SqlCommand command=new SqlCommand("AddCustomer", connection);
       command.CommandType=CommandType.StoredProcedure;

       command.Parameters.Add("@CustomerName",SqlDbType.NVarChar,50).Value=customerName;
       command.Parameters.Add("@country",SqlDbType.NVarChar,20).Value=country;
       command.Parameters.Add("@Province",SqlDbType.NVarChar,20).Value=province;
       command.Parameters.Add("@City",SqlDbType.NVarChar,20).Value=city;
       command.Parameters.Add("@Address",SqlDbType.NVarChar,60).Value=address;
       command.Parameters.Add("@Telephone",SqlDbType.NVarChar,16).Value=telephone;
       command.Parameters.Add("@CustomerId",SqlDbType.Int,4).Direction=ParameterDirection.Output;

       connection.Open();
       command.ExecuteNonQuery();
       connection.Close();

       int custId=(int)command.Parameters["@CustomerId"].Value;
       return custId;
      } 
     
      /// <summary>
      /// 该方法中用到存储过程。存储过程的名称,就是方法的名称。
      /// </summary>
      /// <param name="connection"></param>
      /// <param name="customerName"></param>
      /// <param name="country"></param>
      /// <param name="province"></param>
      /// <param name="city"></param>
      /// <param name="address"></param>
      /// <param name="telephone"></param>
      /// <param name="customerId"></param>
      [ SqlCommandMethod(CommandType.StoredProcedure) ]
      public void AddCustomer([NonCommandParameter]SqlConnection connection,
       [SqlParameter(50)] string customerName,
       [SqlParameter(20)] string country,
       [SqlParameter(20)] string province,
       [SqlParameter(20)] string city,
       [SqlParameter(60)] string address,
       [SqlParameter(16)] string telephone,
       out int customerId )
      {
       customerId = 0; // 需要初始化输出参数

       // 调用Command生成器生成SqlCommand实例,该command对象参数集合中的参数没有设置为任何类型。
       SqlCommand command = SqlCommandGenerator.GenerateCommand(connection, null, new object[]{customerName, country, province, city, address, telephone, customerId});
       connection.Open();
       command.ExecuteNonQuery();
       connection.Close();

       // 必须明确返回输出参数的值
       customerId = (int)command.Parameters["@CustomerId"].Value;
      }

     }
    }

  • 相关阅读:
    当 Messaging 遇上 Jepsen
    Dubbo 在跨语言和协议穿透性方向的探索:支持 HTTP/2 gRPC
    MongoDB与阿里云达成战略合作,最新数据库独家上线阿里云!
    新网银行微服务转型实践
    微服务架构四大金刚利器
    揭秘2019 双11背后的阿里巴巴超强网络
    揭秘2019双11背后的云网络 – 双11网络架构和洛神系统
    小程序的餐饮之路:从流量捕手到流量塘主的进阶秘籍
    备忘录(Memento)模式
    状态(state)模式
  • 原文地址:https://www.cnblogs.com/admin11/p/200272.html
Copyright © 2011-2022 走看看