Simple instance of using stored procedures in asp.net

Source: Internet
Author: User

Add controls as shown in the following figure

Make sure you have a Northwind database in your database and table region, and you can build one yourself if you don't.


Add controls as shown in the following figure

Last of all Files

Make sure you have a Northwind database in your database and table region, and you can build one yourself if you don't.
1 New stored procedures:

CREATE PROCEDURE insert_region
@RegionID int, @RegionDescription nchar (m)
as
insert 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>
///db Summary description
///</summary> public
class db
{public
	db () {
	}< C16/>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 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);//called stored procedure Sqlcmd.commandtype = commandtype.storedprocedure;//Use stored procedure method SqlCmd.Parameters.Add (" @Re Gionid ", SqlDbType.Int). Value = TextBox1.Text.Trim ();//Add Parameter SqlCmd.Parameters.Add ("@RegionDescription", sqldbtype.nchar,50).
        Value = TextBox2.Text.Trim (); Sqlcmd.executenonquery ()//execute con.
        Close ();
    Bind (); }
}

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.