ASP.NET 2.0 新手SQL存储过程简单实例入门 绝对经典入门
首先,打开你的查询分析器。。
运行下列SQL代码。
CREATE PROCEDURE dbo.oa_selectalluser
AS
select * from zz_Stock_Barcode 这个表可以改成对应的测试表,下面的也一样
GO
CREATE PROCEDURE dbo.oa_SelectByID
@id int
AS
select * from zz_Stock_Barcode where ID=@id
GO
命令运行成功后你会看到存储过程中多了两条记录dbo.oa_selectalluser 和dbo.oa_SelectByID
下面开始写操作aspx页面
1。从工具箱中拖出一个TextBox,ID为:TextBox1(一般不用改,系统为默认为此ID)
2。再拖一个Button,ID为:Button1
3。再拖一个GridView,ID为:GridView1,(用来显示数据的)
好了,双击Button控件,在事件中添加代码,(代码如下:)
view plaincopy to clipboardprint?
protected void Button1_Click(object sender, EventArgs e)
{
//带参数的存储过程的使用方法
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
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(TextBox1.Text);
da.SelectCommand.Parameters.Add(param);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
//带参数的存储过程的使用方法
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
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(TextBox1.Text);
da.SelectCommand.Parameters.Add(param);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
再在Page_Load中添加代码,(代码如下:)
view plaincopy to clipboardprint?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//不带参数的存储过程的使用方法
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
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();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//不带参数的存储过程的使用方法
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
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();
}
}
运行,OK。。