虎克的博客

Enthusiasm Biogeography-Biodiversity Informatics-Data Sciences

ASP.NET调用存储过程示例代码

| Comments

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;

namespace CNPC
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string whereCondition = "name_string like '%" + txtName.Text.ToString() + "%'";


                string connString = "data source=.;initial catalog=northwind;integrated security=true";
                DataSet ds = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlConnection conn = new SqlConnection(connString);
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                SqlCommand cmd = new SqlCommand("usp_SelectNamesDynamic", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@whereCondition", SqlDbType.NVarChar);
                cmd.Parameters["@whereCondition"].Value = whereCondition;
                
                cmd.ExecuteNonQuery();
                adapter.SelectCommand = cmd;

                if (ds != null)
                {
                    adapter.Fill(ds, "Names");
                }
                
                grdName.DataSource = ds;
                grdName.DataBind();

                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
    }
}

   original link:
   <a href='http://Apiaceae.github.io/blog/2009/12/18/ASP.NET%E8%B0%83%E7%94%A8%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81/'>http://Apiaceae.github.io/blog/2009/12/18/ASP.NET%E8%B0%83%E7%94%A8%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81/</a><br/>
   &nbsp;written by <a href='http://Apiaceae.github.io'>Hooker</a>
   &nbsp;posted at <a href='http://Apiaceae.github.io'>http://Apiaceae.github.io</a>
   </p>

Comments