//在sql server 建立表 info ,其中字段分别为:uid Int primary key ,
// uname VarChar(50),
// upwd VarChar(50)
/*存储过程 sp_add 如下:
create procedure sp_add
(
@uname varchar(50),
@upwd varchar(50)
)
as
insert into info
(
uname,
upwd
)
values
(
@uname,
@upwd
)
*/
//在 web.config 里加以下代码:
<appSettings>
<add key="MySqlDb" value="server=(local);DataBase=mydb;uid=sa;pwd=;"></add>
</appSettings>
//test.aspx 页代码
<%@ Page language="C#" Codebehind="add.aspx.cs" AutoEventWireup="false" Inherits="add.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 160px; WIDTH: 300px; POSITION: absolute; TOP: 104px; HEIGHT: 144px"
cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD style="WIDTH: 75px"><FONT face="宋体">unaem:</FONT></TD>
<TD><FONT face="宋体"> </FONT>
<asp:TextBox id="uname" runat="server" Width="184px"></asp:TextBox></TD>
</TR>
<TR>
<TD style="WIDTH: 75px"><FONT face="宋体">upwd:</FONT></TD>
<TD><FONT face="宋体"> </FONT>
<asp:TextBox id="upwd" runat="server" Height="200px" TextMode="MultiLine"></asp:TextBox></TD>
</TR>
<TR>
<TD style="WIDTH: 75px"></TD>
<TD><FONT face="宋体"> </FONT>
<asp:Button id="btn_add" runat="server" Text="添 加"></asp:Button></TD>
</TR>
</TABLE>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 224px; POSITION: absolute; TOP: 408px" runat="server"
Height="32px" Width="152px"></asp:Label>
</form>
</body>
</HTML>
//test.cs 代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace add
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox uname;
protected System.Web.UI.WebControls.TextBox upwd;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btn_add;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Label1.Visible = false;
}
private readonly string db = ConfigurationSettings.AppSettings["mydb"].ToString();//获取连接字符串
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 asp.net Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_add.Click += new System.EventHandler(this.btn_add_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Add ( string suname,string supwd )
{
//string db = ConfigurationSettings.AppSettings["mydb"];
SqlConnection conn = new SqlConnection (db);
SqlCommand cmd = new SqlCommand ( "sp_add",conn);//存储过程
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter sp_uname = new SqlParameter ( "@uname",SqlDbType.VarChar );//uname
sp_uname.Value = suname;
cmd.Parameters.Add(sp_uname);
SqlParameter sp_upwd = new SqlParameter ( "@upwd", SqlDbType.VarChar);//upwd
sp_upwd.Value = supwd;
cmd.Parameters.Add(sp_upwd);
conn.Open();//打开数据库
cmd.ExecuteNonQuery(); //执行操作
conn.Close(); //关闭数据库
}
private void btn_add_Click(object sender, System.EventArgs e)
{
if( uname.Text.Trim().Length > 0 && upwd.Text.Trim().Length > 0 )
{
Add ( uname.Text.Trim(),upwd.Text.Trim() );
uname.Text=""; //清空表单中的数据
upwd.Text=""; //清空表单中的数据
Label1.Visible=true; //置Label1 控件为显示状态
Label1.Text="添加成功!"; //提示相关信息
}
else
{
Label1.Visible = true; //置Label1 控件为显示状态
Label1.Text="请输入要添加的信息!"; //提示相关信息
}
}
}
}
http://www.365asp.net/show.aspx?id=54&cid=7
浙公网安备 33010602011771号