摘自:http://www.cnblogs.com/smhy8187/articles/677742.html

声明:本例用的数据库是系统提供的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技巧(二)-在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中执行存储过程(转)的更多相关文章

  1. Oracle中执行存储过程call和exec区别

    Oracle中执行存储过程call和exec区别 在sqlplus中这两种方法都可以使用: exec pro_name(参数1..); call pro_name(参数1..); 区别: 1. 但是e ...

  2. 在SQL Server数据库中执行存储过程很快,在c#中调用很慢的问题

    记录工作中遇到的问题,分享出来: 原博客地址:https://blog.csdn.net/weixin_40782680/article/details/85038281 今天遇到一个比较郁闷的问题, ...

  3. ASP.NET中调用存储过程方法

    两种不同的存储过程调用方法 为了突出新方法的优点,首先介绍一下在.NET中调用存储过程的“官方”方法.另外,本文的所有示例程序均工作于SqlServer数据库上,其它情况类似,以后不再一一说明.本文所 ...

  4. 转载MSDN 在ASP.NET 中执行 URL 重写

    转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...

  5. Java中执行存储过程和函数(web基础学习笔记十四)

    一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. CallableStatement 接口继承自PreparedStatement 接口.所以CallableS ...

  6. C#中执行存储过程并在SQL server中调试

    1.编写存储过程 ) drop PROCEDURE [dbo].[sp_calcPci_of_baseRcd_GTmpTbl] CREATE PROCEDURE [dbo].[sp_calcPci_o ...

  7. Java中执行存储过程和函数

    装载于:http://www.cnblogs.com/liunanjava/p/4261242.html 一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. Ca ...

  8. EFCore 中执行存储过程返回DataSet DataTable

    在项目中由于需求,需要返回复杂的数据,需要执行存储过程,但是在DONETCORE2.0中,看官网文档执行的sql的有点操蛋,满足不了需求,就想到了ADO.NET 于是找资料,也没有合适的,就动手自己封 ...

  9. EF中执行存储过程,获取output返回值

    EF不能直接支持执行存储过程,于是使用转化成执行SQL语句的形式,却怎么也获取不到output的值,折腾的好久,终于解决了,分享下曲折的经历: public int AddVote(int title ...

随机推荐

  1. java 查询oracle数据库所有表DatabaseMetaData的用法

    DatabaseMetaData的用法(转) 一 . 得到这个对象的实例 Connection con ; con = DriverManager.getConnection(url,userName ...

  2. Android之LogUtil

    提供debug与release的时候是否屏蔽打印信息,把信息选择性的进行保存,可以是否自动保存crash的堆栈信息.来自github上的一个开源项目,https://github.com/syxc/L ...

  3. Windows Server 2003 下实现网络负载均衡(2) (转)

    四.测试 在第一台机器上,关闭网络负载平衡管理器后,用鼠标右键单击“网络负载平衡群集”,从出现的菜单中选择“连接到现存的”,将会弹出“连接”界面.输入第一台计算机的名称或IP地址,点击“连接”按钮,在 ...

  4. java单个方法达到了65536字节的限制

    可以使方法更小的一件事是关闭调试.打开调试时,每一行(带代码)都有一个标记该行的语句. 不.重构代码.没有方法应该那么久.永远. Write small methods! 说真的:任何IDE都会指导您 ...

  5. 【BZOJ】【3239】Discrete Logging

    BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...

  6. 【BZOJ】【3240】【NOI2013】矩阵游戏

    十进制快速幂+矩阵乘法+常数优化 听说这题还可以强行算出来递推式……然后乘乘除除算出来…… 然而蒟蒻选择了一个比较暴力的做法= = 我们发现这个递推的过程是线性的,所以可以用矩阵乘法来表示,$x=a* ...

  7. C/C++字符串查找函数

    C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...

  8. windows media server 组件安装后流媒体服务器启动失败

    做好的web应用,去客户现场部署的时候发现流媒体服务器不能启动.(现场服务器系统为windows server2008 R2) 自己测试的时候搭建环境没什么问题.从来没有遇到安装windows med ...

  9. go语言进阶之为结构体类型添加方法

    1.为结构体类型添加方法 示例: package main import "fmt" type Person struct { name string //名字 sex byte ...

  10. ubuntu14.04上编译安装python3.7.3

    首先先去python官网www.python.org下载python3.7.3的官方压缩包Python-3.7.3.tgz 一.先安装需要的包zlib1g,libffi apt-get update ...