摘自: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. 第七章 Xmemcached客户端介绍

    提示:有关于XMemcached在实际开发中的具体使用,查看"Java企业项目开发实践"系列博客的<第八章 企业项目开发--分布式缓存memcached> 注意:本文主 ...

  2. Objective-C面向对象之实现类

    一般涉及到面向对象都会C#,Java都不可避免的涉及到类,C#中类的后缀名是.cs,Java中是.java,Object-C中一般用两个文件描述一个类,后缀名为.h为类的声明文件,用于声明成员变量和方 ...

  3. java.net.URI 简介 文档 API

    URI 简介 文档地址:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh public final class java.net.URI extend ...

  4. jQuery EasyUI 教程-Tooltip(提示框)

    <!DOCTYPE html> <html> <head> <title>jQuery Easy UI</title> <meta c ...

  5. Android -- 触摸Area对焦区域(更新)

    老早就想找关于不同点击不同地方的对焦,但是一直没有找到,现在项目又需要这个功能,又跑出来找找,最后还是找到啦~~关于对焦更多的是关于自动对焦. 废话不多说,直接来干货,主要是setFocusAreas ...

  6. 关于帝国CMS迁移到新服务器上出现问题的处理办法

    在帝国CMS项目整体迁移过程中,或多或少总会出点幺蛾子,以下就常见的注意事项整理一下: 一.修改 e/config/config.php中的数据库相关配置 二.让项目文件位置具有读写权限 三.设置ph ...

  7. 笔记本wifi热点设置好后,手机连上但不能上网问题

    这个问题我遇到过,我的原因是因为电脑上装有安全防护软件360的原因 解决方法是:打开360-->找到功能大全中的流量防火墙-->打开局域网防护-->关闭局域网隐身功能,立刻解决了这个 ...

  8. uva 213 - Message Decoding (我认为我的方法要比书上少非常多代码,不保证好……)

    #include<stdio.h> #include<math.h> #include<string.h> char s[250]; char a[10][250] ...

  9. easyui refresh 刷新两次的解决方法(推荐)

    //这样写刷新两次 $("#windowid").window('refresh','url01.php'); $("#windowid").window('o ...

  10. CentOS6.3 安装配置 ant

    OS:CentOS6.3 ant版本:apache-ant-1.9.2-bin 第1步:下载ant apache-ant-1.9.2-bin.tar.gz 第2步:解压 tar -zxvf apach ...