當前位置:
首頁 > 最新 > C#簡易的用戶登陸界面設計二

C#簡易的用戶登陸界面設計二

上面我們已經講過如果設計用戶登陸界面了,下面我來為大家帶來用戶註冊界面。說到用戶註冊我們需要注意一個問題就行:首先,用戶註冊的賬戶不能和資料庫里已經存在的賬戶重名。

為了檢驗這個東西,我們可以設置一個button事件,當用戶點擊這個按鈕的時候觸發事件,從資料庫里檢測是否有相同賬戶名存在。

程序的頁面設計如下:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

下面是代碼設計:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsFormsApplication2

{

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

private void label1_Click(object sender, EventArgs e)

{

}

private void textBox1_TextChanged(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)//這個是檢測用戶是否已經存在按鈕

{

string userID = userid.Text.Trim();//獲取用戶註冊的信息

string constr = "server=.;DataBase=test; Integrated Security=True";//選取資料庫

SqlConnection mycon = new SqlConnection(constr);

mycon.Open();

SqlCommand checkCmd = mycon.CreateCommand();

string s = "select account from username where account ="" + userID + """;//從資料庫中查看是否已經存在相同的姓名

checkCmd.CommandText = s;

SqlDataAdapter check = new SqlDataAdapter();

check.SelectCommand = checkCmd;

DataSet checkData = new DataSet();

int n = check.Fill(checkData, "lianxi");

if (n != 0)//表示用戶名已經存在

{

MessageBox.Show("用戶名已存在!");

userid.Text = "";

userpw.Text = "";

nickname.Text = "";

}

else

{

MessageBox.Show("用戶名可以使用。");

}

}

private void zc_Click(object sender, EventArgs e)//這個是註冊按鈕

{

string userID = userid.Text.Trim();

string constr = "server=.;DataBase=test; Integrated Security=True";

SqlConnection mycon = new SqlConnection(constr);

mycon.Open();

SqlCommand checkCmd = mycon.CreateCommand();

string s = "select account from username where account ="" + userID + """;

string sl = "insert into username(account,password,name) values ("" + userid.Text + "","" + userpw.Text + "","" + nickname.Text + "")";//輸入用戶註冊的密碼和名稱

checkCmd.CommandText = s;

SqlDataAdapter check = new SqlDataAdapter();

check.SelectCommand = checkCmd;

DataSet checkData = new DataSet();

int n = check.Fill(checkData, "lianxi");

SqlCommand mycom = new SqlCommand(sl, mycon);

mycom.ExecuteNonQuery(); //執行語句

mycon.Close(); //關閉連接

mycom = null;

mycon.Dispose();

if (userpwz.Text != userpw.Text)//檢測兩次輸入的密碼是否相同

{

userpwz.Text = "";

userpw.Text = "";

MessageBox.Show("兩次密碼內容不一致");

}

else if(userid.Text==""||nickname.Text==""||userpwz.Text==""||this.checkBox1.Checked==false)//檢測是否有空缺內容,比如同意用戶協議是否沒有打勾

{

MessageBox.Show("請將信息補充完整");

}

else

{

MessageBox.Show("註冊成功");

this.Close();

}

}

private void userpwz_TextChanged(object sender, EventArgs e)

{

}

private void Form2_Load(object sender, EventArgs e)

{

}

}

}

之後我們實際來運行一下程序,首先大家可以看一下資料庫裡面有兩個用戶一個是li一個是xfnill。

當我運行程序點擊檢測用戶註冊名時,用於我所註冊的用戶已經存在與資料庫中便會彈出一下窗口。

那我們換一個名字好了。

之後我們可以在數據里看見,數據里的內容多了一行。

以上就是今天所要講述的內容。


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 小賤學圖像處理 的精彩文章:

C#顯示資料庫表格內容

TAG:小賤學圖像處理 |