標籤:
建立一個新的使用者註冊表單(VIPRegistration.cs):
使用者註冊表單(VIPRegistration.cs)詳細代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 using System.Data.SqlClient;11 using System.Configuration;12 13 namespace 會員管理系統14 {15 public partial class VIPRegistration : Form16 {17 public VIPRegistration()18 {19 InitializeComponent();20 }21 22 23 //提交按鈕24 private void btnOK_Click(object sender, EventArgs e)25 {26 27 string connstr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;28 SqlConnection conn = new SqlConnection(connstr);29 string sql = string.Format("select vusername from VipAccount where vUserName=‘{0}‘ ",txtName.Text);30 SqlCommand cmd = new SqlCommand(sql, conn);31 conn.Open();32 SqlDataReader sda=cmd.ExecuteReader();33 34 if (txtName.Text.Trim() == "")35 {36 lblName.Text="使用者名稱不可為空";37 return;38 }39 else if (txtPwd.Text.Trim() == ""|| txtPwdConfirm.Text.Trim()=="")40 {41 lblPwd.Text = "密碼不可為空";42 return;43 }44 else if (txtPwdConfirm.Text.Trim()!= txtPwd.Text.Trim())45 {46 lblPwdConfirm.Text = "兩次密碼輸入不同,請確認後再輸";47 return;48 }49 else if (sda.Read())50 {51 lblName.Text = "使用者名稱已存在,請重新輸入";52 return;53 }54 else55 {56 conn.Close();57 SqlConnection conninsert = new SqlConnection(connstr);58 string insertsql = string.Format("insert into VipAccount(vUserName,vUserPwd) values(‘{0}‘,‘{1}‘)",txtName.Text,txtPwd.Text);59 SqlCommand cmdinsert = new SqlCommand(insertsql, conninsert);60 conninsert.Open();61 int n = cmdinsert.ExecuteNonQuery();62 if (n == 0)63 {64 MessageBox.Show("註冊失敗,請重新輸入");65 }66 else67 {68 MessageBox.Show("註冊成功");69 }70 conninsert.Close();71 72 }73 //conn.Close();74 }75 76 //返回主菜單77 private void btnBack_Click(object sender, EventArgs e)78 {79 VIPMain vm = new VIPMain();80 vm.Show();81 this.Hide();82 }83 84 private void VIPRegistration_Load(object sender, EventArgs e)85 {86 lblName.Text = "";87 lblPwd.Text = "";88 lblPwdConfirm.Text = "";89 }90 91 }92 }
主介面表單(VIPMain.cs)添加一個新的按鈕:
使用者註冊按鈕代碼:
1 private void btnRegistration_Click(object sender, EventArgs e)2 {3 VIPRegistration vrn = new VIPRegistration();4 vrn.Show();5 this.Hide();6 }
C#_會員管理系統:開發五(使用者註冊)