在asp.net中执行存储过程(转)
摘自:http://www.cnblogs.com/smhy8187/articles/677742.html
|
ASP.NET技巧(二)-在asp.net中执行存储过程
2006-12-18 20:27:15
大中小
声明:本例用的数据库是系统提供的pubs数据库,表是是employee,编程语言用C#
1.执行不带参数的存储过程存储过程:
create proc example1
as
begin
select top 6 emp_id,fname,lname
from employee
end
执行代码:
SqlConnection Conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
Conn.Open();
SqlCommand cmd = new SqlCommand("example1", Conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
//输出执行结果
string table = "<table border='1'><tr>";
int field = dr.FieldCount;
for (int i = 0; i <= field - 1; i++)
{
table += "<TD>" + dr.GetName(i) + "</td>";
}
table += "</tr>";
while (dr.Read())
{
table += "<tr>";
for (int i = 0; i <= field - 1; i++)
{
table += "<TD>" + dr.GetValue(i) + "</td>";
}
table += "</tr>";
}
table += "</table>";
Conn.Close();
Response.Write(table);
2.执行带普通参数的存储过程
存储过程:
create proc example2
@empid varchar(9)
as
begin
select emp_id,fname,lname from employee
where emp_id=@empid
end
执行代码:
SqlConnection Conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");Conn.Open(); SqlCommand cmd = new SqlCommand("example2", Conn); cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@empid", SqlDbType.VarChar, 9);
cmd.Parameters["@empid"].Value = "PMA42628M";
SqlDataReader dr = cmd.ExecuteReader();
//显示执行结果
string table = "<table border='1'><tr>";
int field = dr.FieldCount;
for (int i = 0; i <= field - 1; i++)
{
table += "<TD>" + dr.GetName(i) + "</td>";
}
table += "</tr>";
while (dr.Read())
{
table += "<tr>";
for (int i = 0; i <= field - 1; i++)
{
table += "<TD>" + dr.GetValue(i) + "</td>";
}
table += "</tr>";
}
table += "</table>";
Conn.Close();
Response.Write(table);
3.执行带output型参数的存储过程
存储过程:
create proc example3 @Count int output as begin select @Count=(select count(*) from employee) end
执行代码:
SqlConnection Conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
Conn.Open();
SqlCommand cmd = new SqlCommand("example3", Conn); cmd.CommandType =andType.StoredProcedure;
cmd.Parameters.Add("@Count", SqlDbType.VarChar, 9);
cmd.Parameters["@count"].Direction = ParameterDirection.Output;
SqlDataReader dr = cmd.ExecuteReader();
Conn.Close();
int c =Convert.ToInt32(cmd.Parameters["@count"].Value); Response.Write(c.ToString());
4.执行用return返回的存储过程
存储过程:
create proc example4as
Begin
declare @Count int
select @Count=(select count(*) from employee)
return @Countend
执行代码:
SqlConnection Conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
Conn.Open();
SqlCommand cmd = new SqlCommand("example4", Conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("Count", SqlDbType.VarChar, 9); cmd.Parameters["count"].Direction = ParameterDirection.ReturnValue;
SqlDataReader dr = cmd.ExecuteReader();
Conn.Close();
int c =Convert.ToInt32(cmd.Parameters["count"].Value); Response.Write(c.ToString());
在asp.net中执行存储过程(转)的更多相关文章
- Oracle中执行存储过程call和exec区别
Oracle中执行存储过程call和exec区别 在sqlplus中这两种方法都可以使用: exec pro_name(参数1..); call pro_name(参数1..); 区别: 1. 但是e ...
- 在SQL Server数据库中执行存储过程很快,在c#中调用很慢的问题
记录工作中遇到的问题,分享出来: 原博客地址:https://blog.csdn.net/weixin_40782680/article/details/85038281 今天遇到一个比较郁闷的问题, ...
- ASP.NET中调用存储过程方法
两种不同的存储过程调用方法 为了突出新方法的优点,首先介绍一下在.NET中调用存储过程的“官方”方法.另外,本文的所有示例程序均工作于SqlServer数据库上,其它情况类似,以后不再一一说明.本文所 ...
- 转载MSDN 在ASP.NET 中执行 URL 重写
转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...
- Java中执行存储过程和函数(web基础学习笔记十四)
一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. CallableStatement 接口继承自PreparedStatement 接口.所以CallableS ...
- C#中执行存储过程并在SQL server中调试
1.编写存储过程 ) drop PROCEDURE [dbo].[sp_calcPci_of_baseRcd_GTmpTbl] CREATE PROCEDURE [dbo].[sp_calcPci_o ...
- Java中执行存储过程和函数
装载于:http://www.cnblogs.com/liunanjava/p/4261242.html 一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. Ca ...
- EFCore 中执行存储过程返回DataSet DataTable
在项目中由于需求,需要返回复杂的数据,需要执行存储过程,但是在DONETCORE2.0中,看官网文档执行的sql的有点操蛋,满足不了需求,就想到了ADO.NET 于是找资料,也没有合适的,就动手自己封 ...
- EF中执行存储过程,获取output返回值
EF不能直接支持执行存储过程,于是使用转化成执行SQL语句的形式,却怎么也获取不到output的值,折腾的好久,终于解决了,分享下曲折的经历: public int AddVote(int title ...
随机推荐
- NLP知识结构概述
NLP知识结构概述 1)自然语言处理:利用计算机为工具,对书面实行或者口头形式进行各种各样的处理和加工的技术,是研究人与人交际中以及人与计算机交际中的演员问题的一门学科,是人工智能的主要内容. 2)自 ...
- Kmeans算法原理极其opencv实现(转帖)
原帖地址:http://blog.csdn.net/qll125596718/article/details/8243404 1.基本Kmeans算法[1] 选择K个点作为初始质心 repeat ...
- OpenCV学习(19) 细化算法(7)
最后再来看一种通过形态学腐蚀和开操作得到骨架的方法.http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/ 代码非常简单: v ...
- Page Redirect Speed Test
现在,有两种方法可以实现网页的自动跳转. (1) 用html自带的<meta>标签(如下)可以实现网页的自动跳转,而且可以控制跳转的延时. <meta http-equiv=&quo ...
- Multi-label && Multi-label classification
Multi-label classification with Keras In today’s blog post you learned how to perform multi-label cl ...
- SQL Server AlwaysOn Setup Step-By-Step Guide
Step-By-Step: Creating a SQL Server 2012 AlwaysOn Availability Group http://blogs.technet.com/b/cani ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- html 中shadow DOM 的使用
什么是shadow DOM? An important aspect of web components is encapsulation — being able to keep the marku ...
- project开发的程序设计与逻辑设计
非常多时候我们要做庞大project, 就像一棵大树, 方方面面都有自己的细枝末节,而作为开发员的我们,无法时时刻刻去保持对程序的全面认知,所以我们要把程序设计与逻辑设计区分开来. 那么什么是程序设计 ...
- PHP导出word,CVS,PDF
WORD: <?php header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no ...