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

    }

}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章