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 ();}}