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

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

点击次数:31 次 发布日期:2008-11-21 22:16:33 作者:源代码网
源代码网推荐

源代码网整理以下以下是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控件用来接收存储过程的参数.

源代码网整理以下

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华