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;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)