ASP.NET数据库添加记录练习

update.aspx

<% @ Page language = " C# "  Codebehind = " update.aspx.cs "  AutoEventWireup = " false "  Inherits = " DotNetTest.database.update "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN "   >
< HTML >
    
< HEAD >
        
< title > update </ title >
        
< meta name = " GENERATOR "  Content = " Microsoft Visual Studio 7.0 " >
        
< 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 >
        
< form id = " update "  method = " post "  runat = " server " >
            
< P >< FONT face = " 宋体 "  color = " #ff6600 " >------------ 帐号注册 ------------</ FONT ></ P >
            
< P >< FONT style = " FONT-STYLE: italic "  face = " 宋体 "  color = " #ff6600 " > 必填: </ FONT ></ P >
            
< P >< FONT face = " 宋体 " > 帐号:
                    
< asp:TextBox id = " TB_Account "  runat = " server " ></ asp:TextBox >
                    
< asp:Label id = " Lbl_Msg "  style = " Z-INDEX: 101; LEFT: 305px; POSITION: absolute; TOP: 94px "  runat = " server "  Width = " 177px "  Height = " 59px "  BackColor = " White "  ForeColor = " Red " ></ asp:Label ></ P >
            
</ FONT >
            
< P >< FONT face = " 宋体 " > 密码:
                    
< asp:TextBox id = " TB_Password1 "  runat = " server " ></ asp:TextBox ></ FONT ></ P >
            
< P >< FONT face = " 宋体 " > 确认密码:
                    
< asp:TextBox id = " TB_Password2 "  runat = " server " ></ asp:TextBox ></ FONT ></ P >
            
< P >< FONT face = " 宋体 " >---------
                    
< asp:Button id = " B_OKGOPinjie "  runat = " server "  Text = " OK,GO拼接字符串 "  Width = " 160px " ></ asp:Button >
                    
< asp:LinkButton id = " LB_SeeResult "  style = " Z-INDEX: 102; LEFT: 312px; POSITION: absolute; TOP: 203px "  runat = " server "  Width = " 116px "  Height = " 23px "  Visible = " False " > 查看数据库内容 </ asp:LinkButton ></ FONT ></ P >
            
< P >< FONT face = " 宋体 " >---------
                    
< asp:Button id = " B_OKGOParm "  runat = " server "  Text = " OK,GO使用参数 "  Width = " 161px " ></ asp:Button ></ FONT ></ P >
            
< P >< FONT face = " 宋体 " >---------
                    
< asp:Button id = " B_OKGOStore "  runat = " server "  Text = " OK,GO使用存储过程 "  Width = " 161px " ></ asp:Button ></ P >
            
</ FONT >
            
< P >< FONT style = " FONT-STYLE: italic "  face = " 宋体 "  color = " #ff6600 " > 选填: </ P >
            
</ FONT >
            
< P >< FONT face = " 宋体 " > 昵称:
                    
< asp:TextBox id = " TB_VirtualName "  runat = " server " ></ asp:TextBox ></ FONT ></ P >
            
< P >< FONT face = " 宋体 " > 性别:
                    
< asp:RadioButtonList id = " RBL_Sex "  runat = " server "  RepeatDirection = " Horizontal "  RepeatLayout = " Flow " >
                        
< asp:ListItem Value = " M "  Selected = " True " > </ asp:ListItem >
                        
< asp:ListItem Value = " F " > </ asp:ListItem >
                    
</ asp:RadioButtonList ></ FONT >< FONT face = " 宋体 " ></ P >
            
</ FONT >
        
</ form >
    
</ body >
</ HTML >

update.aspx.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  DotNetTest.database
{
    
/// <summary>
    
/// update 的摘要说明。
    
/// </summary>

    public class update : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.TextBox TB_Account;
        
protected System.Web.UI.WebControls.TextBox TB_Password1;
        
protected System.Web.UI.WebControls.TextBox TB_Password2;
        
protected System.Web.UI.WebControls.TextBox TB_VirtualName;
        
protected System.Web.UI.WebControls.Button B_OKGOPinjie;
        
protected System.Web.UI.WebControls.Button B_OKGOParm;
        
protected System.Web.UI.WebControls.Button B_OKGOStore;
        
protected System.Web.UI.WebControls.Label Lbl_Msg;
        
protected System.Web.UI.WebControls.LinkButton LB_SeeResult;
        
protected System.Web.UI.WebControls.RadioButtonList RBL_Sex;

        
private bool CheckInfo()
        
{
            
if(this.TB_Account.Text.Trim()=="")
            
{
                Alert(
"学号不完整");
                
return false;
            }

            
else if(this.TB_Password1.Text.Trim()==""||this.TB_Password2.Text.Trim()=="")
            
{
                Alert(
"密码不完整");
                
return false;
            }

            
else if(this.TB_Password1.Text.Trim()!=this.TB_Password2.Text.Trim())
            
{
                Alert(
"密码确认失败!");
                
return false;
            }

            
return true;
        }

        
private void Alert(string str)
        
{
            Lbl_Msg.Text
=str;
            Lbl_Msg.BackColor
=System.Drawing.Color.DimGray;
            LB_SeeResult.Visible
=true;
        }

    
        
private bool IsOKAccount()
        
{
            
string usrAccount=this.TB_Account.Text.Trim();
            
try
            
{
                
//config the database
                string strConn=ConfigurationSettings.AppSettings["MSSQLConnStr_FSLink"];
                SqlConnection sqlConn
=new SqlConnection();
                sqlConn.ConnectionString
=strConn;
                
//the SQL string
                string sqlCount="SELECT COUNT(*) FROM FSL_USER WHERE USER_ID=@userID";
                
//create the command
                SqlCommand cmd=new SqlCommand(sqlCount,sqlConn);
                
//add parameters
                cmd.Parameters.Add(new SqlParameter("@userID",usrAccount));
                
//open the database
                sqlConn.Open();
                
//save  the affected result
                string aff=cmd.ExecuteScalar().ToString();
                
//close the database
                sqlConn.Close();
                
if(Int32.Parse(aff)!=0)
                
{
                    Alert(
"帐号已经存在");
                    
return false;
                }

                
return true;

            }

            
catch(Exception ex)
            
{
                Alert(ex.Message);
                
return false;
            }

        }


        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
        }


        
Web Form Designer generated code

        
private void B_OKGOPinjie_Click(object sender, System.EventArgs e)
        
{
            
if(!IsOKAccount())
            
{
                
return;
            }

            
if(this.CheckInfo())
            
{
                
//get the acquired value
                string usrAccount=this.TB_Account.Text.Trim();
                
string usrPassword=this.TB_Password1.Text.Trim();
                
string usrVirtualName=this.TB_VirtualName.Text.Trim();
                
string usrSex=this.RBL_Sex.SelectedItem.Value.Trim();

                
//operate the database
                try
                
{
                    
//config the database
                    string strConn=ConfigurationSettings.AppSettings["MSSQLConnStr_FSLink"];
                    SqlConnection sqlConn
=new SqlConnection();
                    sqlConn.ConnectionString
=strConn;
                    
//the SQL string
                    string sqlInsert="INSERT INTO FSL_USER(USER_ID,USER_NAME,USER_SEX,USER_PASSWORD)"+
                        
" VALUES('"+usrAccount+"','"+usrVirtualName+"','"+usrSex+"','"+usrPassword+"')";
                    
//create the command
                    SqlCommand cmd=new SqlCommand(sqlInsert,sqlConn);
                    
//open the database
                    sqlConn.Open();
                    
//save  the affected result
                    int aff=cmd.ExecuteNonQuery();
                    
//close the reader and the database
                    sqlConn.Close();
                    Alert(
"使用拼接字符串:数据添加成功"+aff+"条!");
                }

                
catch(Exception ex)
                
{
                    Alert(ex.Message);
                }

            }

        }


        
private void B_OKGOParm_Click(object sender, System.EventArgs e)
        
{
            
if(this.CheckInfo())
            
{
                
if(!IsOKAccount())
                
{
                    
return;
                }

                
//get the acquired value
                string usrAccount=this.TB_Account.Text.Trim();
                
string usrPassword=this.TB_Password1.Text.Trim();
                
string usrVirtualName=this.TB_VirtualName.Text.Trim();
                
string usrSex=this.RBL_Sex.SelectedItem.Value.Trim();
                
                
//operate the database
                try
                
{
                    
//config the database
                    string strConn=ConfigurationSettings.AppSettings["MSSQLConnStr_FSLink"];
                    SqlConnection sqlConn
=new SqlConnection();
                    sqlConn.ConnectionString
=strConn;
                    
//the SQL string
                    string sqlInsert="INSERT INTO FSL_USER(USER_ID,USER_NAME,USER_SEX,USER_PASSWORD)"+
                        
" VALUES(@userID,@userName,@userSex,@userPassword)";
                    
//create the command
                    SqlCommand cmd=new SqlCommand(sqlInsert,sqlConn);
                    
//add parameters
                    cmd.Parameters.Add(new SqlParameter("@userID",usrAccount));
                    cmd.Parameters.Add(
new SqlParameter("@userName",usrVirtualName));
                    cmd.Parameters.Add(
new SqlParameter("@userSex",usrSex));
                    cmd.Parameters.Add(
new SqlParameter("@userPassword",usrPassword));
                    
//open the database
                    sqlConn.Open();
                    
//save  the affected result
                    int aff=cmd.ExecuteNonQuery();
                    
//close the reader and the database
                    sqlConn.Close();
                    Alert(
"使用参数:数据添加成功"+aff+"条!");
                }

                
catch(Exception ex)
                
{
                    Alert(ex.Message);
                }

            }

        
        }


        
private void B_OKGOStore_Click(object sender, System.EventArgs e)
        
{
            
if(!IsOKAccount())
            
{
                
return;
            }

            
if(this.CheckInfo())
            
{
                
//get the acquired value
                string usrAccount=this.TB_Account.Text.Trim();
                
string usrPassword=this.TB_Password1.Text.Trim();
                
string usrVirtualName=this.TB_VirtualName.Text.Trim();
                
string usrSex=this.RBL_Sex.SelectedItem.Value.Trim();
                
//operate the database
                try
                
{
                    
//config the database
                    string strConn=ConfigurationSettings.AppSettings["MSSQLConnStr_FSLink"];
                    SqlConnection sqlConn
=new SqlConnection();
                    sqlConn.ConnectionString
=strConn;
                    
//create the command
                    SqlCommand cmd=new SqlCommand("InsertUser",sqlConn);
                    cmd.CommandType
=CommandType.StoredProcedure;
                    
//add parameters
                    cmd.Parameters.Add(new SqlParameter("@userID",usrAccount));
                    cmd.Parameters.Add(
new SqlParameter("@userName",usrVirtualName));
                    cmd.Parameters.Add(
new SqlParameter("@userSex",usrSex));
                    cmd.Parameters.Add(
new SqlParameter("@userPassword",usrPassword));
                    
//open the database
                    sqlConn.Open();
                    
//save  the affected result
                    int aff=cmd.ExecuteNonQuery();
                    
//close the reader and the database
                    sqlConn.Close();
                    Alert(
"使用存储过程:数据添加成功"+aff+"条!");
                }

                
catch(Exception ex)
                
{
                    Alert(ex.Message);
                }

            }

        }



        
private void LB_SeeResult_Click(object sender, System.EventArgs e)
        
{
            Server.Transfer(
"select_adapter.aspx");
        }

    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值