sql-server创建存储过程

复制代码
use EFDB
go

--添加学生存储过程
--usp_AddStudents 自定义的存储过程name
if exists(select * from sysobjects where name='usp_AddStudents') drop procedure usp_AddStudents go
--@StudentName varchar(20), 对应表中的字段
--@Gender char(2), 对应表中的字段
--@Birthday smalldatetime, 对应...
--@StudentIdNo numeric(18, 0), 对应...
--@Age int, 对应...
--@PhoneNumber varchar(50), 对应...
--@StudentAddress varchar(500), 对应...
--@ClassId int 对应...
create procedure usp_AddStudents
@StudentName varchar(20),
@Gender char(2),
@Birthday smalldatetime,
@StudentIdNo numeric(18, 0),
@Age int,
@PhoneNumber varchar(50),
@StudentAddress varchar(500),
@ClassId int

-- as - go 中间写sql语句 as insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId) values(@StudentName,@Gender,@Birthday,@StudentIdNo,@Age,@PhoneNumber,@StudentAddress,@ClassId) go
复制代码

 使用 usp_AddStudents 存储过程 
1.封装参数: 

复制代码
SqlParameter parameter = new SqlParameter()
{
    new SqlParameter("@StudentName", StudentName),
    new SqlParameter("@Gender",Gender),
    new SqlParameter("@Birthday",Birthday),
    new SqlParameter("@StudentIdNo",StudentIdNo),
    new SqlParameter("@Age", Age),
    new SqlParameter("@PhoneNumber", PhoneNumber),
    new SqlParameter("@StudentAddress", StudentAddress),
    new SqlParameter("@ClassId", ClassId)
};
复制代码

封装SqlHelper

复制代码
public static int Update(string uspName, params SqlParameter[] parse)
{
    SqlConnection conn = new SqlConnection(connStr);
    SqlCommand cmd = new SqlCommand(uspName, conn);
    try
    {
        conn.Open();
        cmd.CommandType = CommandType.StordProcedure; //设置类型为存储过程
        cmd.Parameters.Clear();
        cmd.Parameters.AddRange(parse);
        return cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}
复制代码

 

posted @   龙卷风吹毁停车场  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2020-07-01 vue 判断当前图片地址是否404
2019-07-01 将H5页面打包成安卓app
2019-07-01 将h5用HBuilderX打包成安卓app后,document.documentElement.scrollTop的值始终为0或者document.body.scrollTop始终为0
点击右上角即可分享
微信分享提示