码迷,mamicode.com
首页 > 其他好文 > 详细

Command调用存储过程小实例

时间:2016-05-03 23:35:52      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         <asp:GridView ID="GridView1" runat="server"></asp:GridView>
14         <asp:Label ID="Label1" runat="server" Text="请输入类别名:"></asp:Label>
15         <br />
16         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
17         <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
18         <br />
19     </div>
20     </form>
21 </body>
22 </html>
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Data.SqlClient;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 
10 namespace WebApplication1
11 {
12     public partial class WebForm1 : System.Web.UI.Page
13     {
14         string db = System.Configuration.ConfigurationManager.ConnectionStrings["GetConnection"].ConnectionString;
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             bind();
18         }
19         protected void bind()
20         {
21             
22             SqlConnection myConn = new SqlConnection(db);
23             myConn.Open();
24             string sqlStr = "select * from classtable";
25             SqlDataAdapter myDa = new SqlDataAdapter(sqlStr,myConn);
26             DataSet myDs = new DataSet();
27             myDa.Fill(myDs);
28             GridView1.DataSource = myDs;
29             GridView1.DataKeyNames = new string[] { "classID" };
30             GridView1.DataBind();
31             myDa.Dispose();
32             myDs.Dispose();
33             myConn.Close();
34 
35         }
36         protected void Button1_Click(object sender, EventArgs e)
37         {
38             if (this.TextBox1.Text!="")
39             {
40                 SqlConnection myConn =new SqlConnection(db);
41                 myConn.Open();
42                 SqlCommand myCmd = new SqlCommand("insertClass",myConn);
43                 myCmd.CommandType = CommandType.StoredProcedure;
44                 myCmd.Parameters.Add("@className",SqlDbType.VarChar,50).Value=this.TextBox1.Text.Trim();
45                 myCmd.ExecuteNonQuery();
46                 myConn.Close();
47                 this.bind();
48             }
49             else
50             {
51                 this.bind();
52             }
53         }
54        
55 
56 
57 
58     }
59     
60 }
 1 create table classtable
 2 (
 3 classID int identity(1,1)Primary key,
 4 className varchar(50)
 5 )
 6 insert into classtable values (电器类)
 7 insert into classtable values (鲜花类)
 8 insert into classtable values (服装类)
 9 insert into classtable values (家具类)
10 insert into classtable values (化妆类)
11 insert into classtable values (文具类)
12 insert into classtable values (软件光盘)
13 select * from classtable
14 
15 --存储过程
16 use t1
17 go
18 create proc InsertClass
19 (@ClassName varchar(50))
20 as
21 insert into classtable(ClassName) values(@ClassName)
22 go

 

Command调用存储过程小实例

标签:

原文地址:http://www.cnblogs.com/xiaz/p/5456657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!