asp.net(c#) 下SQL預存程序使用詳細執行個體

來源:互聯網
上載者:User

記取記錄集
create procedure getArticle
as
select * from Article_Content

GO
asp.net 調用方法

  SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = Data.Connstr();
        Conn.Open();
        SqlDataAdapter sdr=new SqlDataAdapter();
        sdr.SelectCommand = new SqlCommand("getArticle", Conn);
        sdr.SelectCommand.CommandType=CommandType.StoredProcedure;
        DataSet rs = new DataSet();
        sdr.Fill(rs);
        sdr.Dispose();
        Conn.Dispose();
        Response.Write(rs.Tables[0].Rows.Count);
       

-------------------------------------------------------------------------------------
刪除記錄(帶輸入參數)
create procedure DelArticle
@Id int
as
delete from Article_Content where Id=@Id

GO

asp.net調用方法
 if (Request.QueryString["Id"] != null)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = Data.Connstr();
            Conn.Open();
            SqlCommand cmd = new SqlCommand("DelArticle", Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Id", SqlDbType.Int,4).Value = int.Parse(Request.QueryString["Id"].ToString());
            Response.Write(cmd.ExecuteNonQuery());
            cmd.Dispose();
            Conn.Dispose();
        }

-------------------------------------------------------------------------------------

添加記錄(帶輸出參數),修改記錄和方法一樣,就是用update。

設定兩個輸出參數,讓大家對輸出多參數有點理解。

create proc Add_Channel
@Channel_Title nvarchar(50),
@Channel_Name nvarchar(50),
@Channel_Ename nvarchar(50),
@Channel_Templet int,
@Keyword nvarchar(100),
@rtv int output,
@rtv1 int output,
@KDescription nvarchar(200)
as
 if exists(select * from Channel where Channel_Name = @Channel_Name)
 begin
  set @rtv=0
  set @rtv1=5
 end
 else
 if exists(select * from Channel where Channel_Ename = @Channel_Ename)
 begin
  set @rtv=1
  set @rtv1=6
 end
 else
 begin
  insert into Channel(Channel_Title,Channel_Name,Channel_Ename,Channel_Templet,Keyword,KDescription)values(@Channel_Title,@Channel_Name,@Channel_Ename,@Channel_Templet,@Keyword,@KDescription)
  set @rtv=2
  set @rtv1=7
 end

 
go

asp.net調用方法

public partial class Default2 : System.Web.UI.Page
{
    public string rvs(string str){ //根據參數值返回對應的提示資訊
        if(str=="0"){
            return "名稱重複";
        }
        else if(str=="1"){
            return "英文名稱重複";
        }
        else{
            return "添加成功!";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = Data.Connstr();
        Conn.Open();
        SqlCommand cmd = new SqlCommand("Add_Channel", Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Channel_Title",SqlDbType.NVarChar,50).Value=Channel_Title.Text.Trim();
        cmd.Parameters.Add("@Channel_Name",SqlDbType.NVarChar,50).Value=Channel_Name.Text.Trim();
        cmd.Parameters.Add("@Channel_Ename",SqlDbType.NVarChar,50).Value=Channel_Ename.Text.Trim();
        cmd.Parameters.Add("@Channel_Templet",SqlDbType.Int,4).Value=int.Parse(Channel_Templet.Text.Trim());
        cmd.Parameters.Add("@Keyword",SqlDbType.NVarChar,100).Value=Keyword.Text.Trim();
        cmd.Parameters.Add("@KDescription",SqlDbType.NVarChar,200).Value=KDescription.Text.Trim();
        cmd.Parameters.Add("@rtv", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
        cmd.Parameters.Add("@rtv1", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
        cmd.ExecuteNonQuery();
        Conn.Dispose();
        Conn.Close();
        Response.Write("<script>alert(" + rvs(cmd.Parameters["@rtv"].Value.ToString()) + "[" + cmd.Parameters["@rtv1"].Value.ToString()+ "]);window.history.back();</script>");
        
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.