ADO.NET - 2.调用存储过程
目录:
1.调用存储过程
存储过程 usp_GetBookCateId
存储过程 usp_GetPagesBooks
1.调用存储过程
1.个参数调用存储过程
存储过程 usp_GetBookCateId
代码
static void Main(string[] args) { //Console.WriteLine("增加数据:"); //string ss = Convert.ToString(Console.ReadLine());//字符串使用Console.ReadLine() //AddRecord(ss); //QuerySingle(); //QueryList(); //QueryListByAdapter(); QueryListByProcedure(); } //调用存储过程 public static void QueryListByProcedure() { SqlConnection conn = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand("usp_GetBookByCateId", conn);
cmd.CommandType = CommandType.StoredProcedure;//指定命令类型为存储过程
SqlParameter sp = new SqlParameter();//指定存储过程中的参数
sp.ParameterName = "@cateId";
sp.SqlDbType = SqlDbType.Int;
sp.Size = 4;
sp.Value = 2;//该参数的值
cmd.Parameters.Add(sp);
//将参数对象加入命令对象的参数集合中 SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); foreach (DataRow dr in ds.Tables[0].Rows) { Console.WriteLine(dr[0].ToString() + " " + dr[1].ToString()+ " " + dr[2].ToString()+ " " + dr[3].ToString()); } }
结果
2.多个参数调用存储过程
存储过程 usp_GetPagesBooks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ALTER procedure [dbo].[usp_GetPagesBooks] @PageIndex int = 1,@PageSize int = 5, @RowCount int output , @PageCount int output --11 as begin select * from ( select ROW_NUMBER() over( order by c_id) as nid,* from Category ) as a where a.nid > (@PageIndex - 1)* @PageSize and a.nid<= @PageIndex * @PageSize select @RowCount = COUNT (*) from Category select @PageCount = ceiling( CONVERT ( float ,@RowCount)/ CONVERT ( float ,@PageSize)) set @PageIndex =10000 set @PageSize =100 end |
代码
static void Main(string[] args) { //Console.WriteLine("增加数据:"); //string ss = Convert.ToString(Console.ReadLine());//字符串使用Console.ReadLine() //AddRecord(ss); //QuerySingle(); //QueryList(); //QueryListByAdapter(); //QueryListByProcedure(); QueryListByProcedure2(); } //调用多参数存储过程 public static void QueryListByProcedure2() { SqlConnection conn = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand("usp_GetPagesBooks", conn); cmd.CommandType = CommandType.StoredProcedure;//指定命令类型为存储过程 SqlParameter[] sp = { new SqlParameter("@PageIndex", SqlDbType.Int, 4), new SqlParameter("@PageSize", SqlDbType.Int, 4), new SqlParameter("@RowCount", SqlDbType.Int, 4), new SqlParameter("@PageCount", SqlDbType.Int, 4) }; sp[0].Value = 2;//变量:保存页码
sp[1].Value = 4;//变量:保存页容量(每页显示多少行)
sp[2].Value = 0;//总行数
sp[3].Value = 0;//总页数
cmd.Parameters.AddRange(sp); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); foreach (DataRow dr in ds.Tables[0].Rows) { Console.WriteLine(dr[0].ToString() + " " + dr[1].ToString() + " " + dr[2].ToString() + " " + dr[3].ToString()); } }
结果
3.调用带输出参数的存储过程 output
代码:
//3.调用带输出参数的存储过程 public static void QueryListByProcedure3() { SqlConnection conn = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand("usp_GetPagesBooks", conn); cmd.CommandType = CommandType.StoredProcedure;//指定命令类型为存储过程 SqlParameter[] sp = { new SqlParameter("@PageIndex", SqlDbType.Int, 4), new SqlParameter("@PageSize", SqlDbType.Int, 4), new SqlParameter("@RowCount", SqlDbType.Int, 4), new SqlParameter("@PageCount", SqlDbType.Int, 4) }; sp[0].Value = 2; sp[1].Value = 4; sp[2].Direction = ParameterDirection.Output;//设置参数的输出方向
sp[3].Direction = ParameterDirection.Output;
cmd.Parameters.AddRange(sp); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); foreach (DataRow dr in ds.Tables[0].Rows) { Console.WriteLine(dr[0].ToString() + " " + dr[1].ToString() + " " + dr[2].ToString() + " " + dr[3].ToString()); } Console.WriteLine(); //输入RowCount和PageCount的值
int RowCount =Convert.ToInt32(cmd.Parameters[2].Value);
int PageCount = Convert.ToInt32(cmd.Parameters[3].Value);
Console.WriteLine("RowCount=" + RowCount + ",PageCount=" + PageCount);
}
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/archive/2012/10/03/2711160.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具