.net调用存储过程

作者在 2012-09-10 13:45:56 发布以下内容
   我们经常做网站的时候,经常用到SQL语句通过方法封装起来,我们想到
存储过程么?还记得存储过程的优点么?
存储过程允许标准组件式编程
     ◆存储过程能够实现较快的执行速度
     ◆存储过程能够减少网络流量
     ◆存储过程可被作为一种安全机制来充分利用
    我做了一个简单带参数存储过程和大家分享下!
如图所示:我在数据库里做的存储过程,这个存储过程已经存放在数据库中。
执行编译之后在数据库的可编程性节点里面的存储过程可以看得到!
以下是.NET页面调用存储过程的源码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //连接数据库
        SqlConnection con = new SqlConnection("server=localhost;database=test;user id=sa;pwd=123456;");
        con.Open();
        //调用存储过程
        SqlDataAdapter sda = new SqlDataAdapter("sp_wh",con);
        //给存储过程的参数赋值
        SqlParameter parao = new SqlParameter("@int",1);
        //添加参数值
        sda.SelectCommand.Parameters.Add(parao);
        //执行指定的存储过程
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
        try
        {
            DataSet ds = new DataSet();
            sda.Fill(ds,"table");
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
        }
        catch
        {
            Console.WriteLine("something is wrong");
            con.Close();  
        }
    }
}
执行效果如图所示:
以上是有参存储过程,无参存储过程类似有参存储过程。
很简单的存储过程,还希望给位同学多多指教!
.net | 阅读 1467 次
文章评论,共0条
游客请输入验证码