1、調用無輸入輸出參數的預存程序
建立無輸入輸出參數的預存程序create procedure testProc
as
select pub_id,title_id,price,pubdate
from titles
where price is not null
order by pub_id
使用下面的程式來調用這個預存程序。在調用該預存程序時,需要告訴Command對象要調用的是預存程序。<form id="form1" runat="server">
<div>
<asp:DataGrid ID="dg" runat="server" />
</div>
</form>
SqlConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
Conn = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=''");
SqlCommand Comm = new SqlCommand("testProc", Conn);
Comm.CommandType = CommandType.StoredProcedure;
Conn.Open();
SqlDataReader dr = Comm.ExecuteReader();
dg.DataSource = dr;
dg.DataBind();
Conn.Close();
}
2、調用帶輸入輸出參數的預存程序CREATE PROCEDURE sp_CheckPass
(@CHKName VARCHAR(30),@CHKPass VARCHAR(30),@ISValid VARCHAR(12) OUTPUT)
AS
IF EXISTS(SELECT UserName from WebUsers WHERE UserName=@CHKName and
UserPass=@CHKPass)
SELECT @ISValid='GOOD'
ELSE
SELECT @ISValid='BAD'
SqlConnection Conn;
protected void Page_Load(object sender, EventArgs e)
{
Conn = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=''");
SqlCommand Comm = new SqlCommand("sp_CheckPass", Conn);
Comm.CommandType = CommandType.StoredProcedure;
SqlParameter parm = Comm.Parameters.Add("@CHKName", SqlDbType.VarChar, 30);
parm.Value = "aa";
parm = Comm.Parameters.Add("@CHKPass", SqlDbType.VarChar, 30);
parm.Value = "aa";
parm = Comm.Parameters.Add("@ISValid", SqlDbType.VarChar, 12);
parm.Direction = ParameterDirection.Output;
Conn.Open();
SqlDataReader dr = Comm.ExecuteReader();
Response.Write(Comm.Parameters["@ISValid"].Value);
Conn.Close();
}