当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 在asp.net 2.0 中使用的存储过程解析

ASP.NET
为T-SQL添加intellisense功能
SQL Server 2005安装过程中出现错误的解决办法
SQL Server 2005 RTM 安装错误 :The SQL Server System Configuration Checker cannot be executed due to
有关于JSON的一些资料
不能忽略c#中的using和as操作符的用处
JavaScript系列之―同步还是异步?
获取远程网页的内容之一(downmoon原创)
获取远程网页的内容之二(downmoon原创)
ASP.Net中防止刷新自动触发事件的解决方案
asp.net下用js实现鼠标移至小图,自动显示相应大图
Asp.Net 和 AJAX.Net 的区别
提交页面的定位--scrollIntoView的用法
利用AJAX与数据岛实现无刷新绑定
asp.net下判断用户什么时候离开,以什么方式离开
DataSet 添加数据集、行、列、主键和外键等操作示例
读写xml所有节点个人小结 和 读取xml节点的数据总结
收藏的asp.net文件上传类源码
asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧
一个可以让.net程序在非WIN平台上运行的软件Mono
使用ASP.NET 2.0 CSS 控件适配器生成CSS友好的HTML输出

ASP.NET 中的 在asp.net 2.0 中使用的存储过程解析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 143 ::
收藏到网摘: n/a

以下是SQL中两个存储过程: CREATE PROCEDURE dbo.oa_selectalluser

AS

select * from UserInfo

GO

CREATE PROCEDURE dbo.oa_SelectByID

@id int

AS

select * from UserInfo where ID=@id

GO


一个是带参数的存储过程,一个是不带参数的存储过程.下面介绍怎么在VS2005中使用这两个存储过程.

(一).不带参数的存储过程:

protected void Page_Load(object sender, EventArgs e)

...{

if(!Page.IsPostBack)

...{

//不带参数的存储过程的使用方法

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["oaConnectionString"].ToString());

SqlDataAdapter da = new SqlDataAdapter();

DataSet ds=new DataSet();

da.SelectCommand = new SqlCommand();

da.SelectCommand.Connection = conn;

da.SelectCommand.CommandText = "oa_SelectAllUser";

da.SelectCommand.CommandType = CommandType.StoredProcedure;

da.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

在页面中添加了一个GridView控件用来绑定执行存储过程得到的结果.

(二).带参数的存储过程:

protected void btn_search_Click(object sender, EventArgs e)

...{

//带参数的存储过程的使用方法

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["oaConnectionString"].ToString());

SqlDataAdapter da = new SqlDataAdapter();


DataSet ds = new DataSet();

da.SelectCommand = new SqlCommand();

da.SelectCommand.Connection = conn;

da.SelectCommand.CommandText = "oa_SelectByID";

da.SelectCommand.CommandType = CommandType.StoredProcedure;


SqlParameter param = new SqlParameter("@id", SqlDbType.Int);

param.Direction = ParameterDirection.Input;

param.Value = Convert.ToInt32(txt_value.Text);

da.SelectCommand.Parameters.Add(param);


da.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

同样,在页面中添加了一个GridView控件用来绑定执行存储过程的结果,另外,在页面中还添加了一个textbox控件和一个BUTTON按钮,上面的执行存储过程是放在按钮的onclick事件中的.textbox控件用来接收存储过程的参数.