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(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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