Simple example of using Stored Procedure in asp.net

Source: Internet
Author: User

This article introduces a simple example of using stored procedures in asp.net. stored procedures are an important part of big data operations. I will introduce them in detail below.

Add controls according

Make sure that your database contains the northwind database and the Region table. If not, create one by yourself.


Add controls according

All last files

Make sure that your database contains the northwind database and the Region table. If not, create one by yourself.
1. Create a stored procedure:

CREATE PROCEDURE insert_Region@RegionID int,@RegionDescription nchar(50)ASinsert into Region(RegionID,RegionDescription)values(@RegionID,@RegionDescription)

2: Add a DB class:

Using System; using System. data; using System. configuration; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. data. sqlClient; // <summary> /// Brief description of DB /// </summary> public class DB {public DB () {} public static SqlConnection createCon () {return new SqlConnection ("server = .; database = northwind; uid = sa; pwd = ;");}}

Default. aspx

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Default. aspx. cs

Using System; using System. data; using System. configuration; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. data. sqlClient; public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {bind () ;}} public void bind () {string SQL = "select * from Region"; SqlConnection con = DB. createCon (); // create a database connection SqlDataAdapter sda = new SqlDataAdapter (SQL, con); DataSet ds = new DataSet (); con. open (); sda. fill (ds, "Region"); GridView1.DataSource = ds; GridView1.DataBind (); // bind data con. close ();} protected void Button1_Click (object sender, EventArgs e) {SqlConnection con = DB. createCon (); con. open (); SqlCommand sqlCmd = new SqlCommand ("insert_Region", con); // call the sqlCmd stored procedure. commandType = CommandType. storedProcedure; // use the Stored Procedure sqlCmd. parameters. add ("@ RegionID", SqlDbType. int ). value = TextBox1.Text. trim (); // Add the sqlCmd parameter. parameters. add ("@ RegionDescription", SqlDbType. NChar, 50 ). value = TextBox2.Text. trim (); sqlCmd. executeNonQuery (); // execute con. close (); bind ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.