C#登入註冊程式
自學C#,練習。登入註冊窗體,功能:點選註冊將輸入的資訊儲存在sqlserver資料庫中,並判斷賬號是否已被註冊過(資料庫中是否已有該賬號),點選登入若賬號密碼資訊資料庫中已存在即登入成功。
另外如果把.EXE程式拷到別的電腦上這個程式還能不能執行
歡迎新手一起學習~望大神指點還有哪些地方需要改進~
介面:
話不多說直接上程式碼
註冊窗體的程式碼
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 Register : Form
{
public Register()
{
InitializeComponent();
}
//清空輸入
public void c()
{
txtIdR.Text = "";
txtPwdR.Text = "";
txtName.Text = "";
txtMail.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{//輸入不能為空
if (txtIdR.Text == "" | txtPwdR.Text == "" | txtName.Text =="" | txtMail.Text == "")
{
MessageBox.Show("請輸入完整資訊");
return;
}
///LoginId不能為重複
//定義連結字串
string connString = "Server=.;DataBase=Register;Uid=sa;Pwd=12345";
//建立連線物件
SqlConnection conn = new SqlConnection(connString);
//組合sql查詢語句
string sqlSelect = "select id from RegisterInformation where id='"+txtIdR.Text.Trim()+"'";
SqlCommand cmd1 = new SqlCommand(sqlSelect, conn);
//執行查詢
conn.Open();
object result1 = cmd1.ExecuteScalar();
//判斷賬號是否重複
if (result1!=null)
{
MessageBox.Show("該賬戶已被註冊");
conn.Close();
return;
}
conn.Close();
//添加註冊資訊SQL語句
string sql = "insert into RegisterInformation(id,password,name,mail)";
sql += "values('{0}','{1}','{2}','{3}')";
sql = string.Format(sql, txtIdR.Text, txtPwdR.Text, txtName.Text, txtMail.Text);
SqlCommand cmd = new SqlCommand(sql, conn);
//開啟連線
conn.Open();
//執行操作
int reslt = cmd.ExecuteNonQuery();
conn.Close();
//清空輸入
c();
//註冊成功
MessageBox.Show("註冊成功");
this.Close();
}
//重置
private void button2_Click(object sender, EventArgs e)
{
c();
txtIdR.Focus();
}
}
}
登入窗體的程式碼
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 LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void btnLog_Click(object sender, EventArgs e)
{
//定義連結字串
string connString = "Server=.;DataBase=Register;Uid=sa;Pwd=12345";
//建立連線物件
SqlConnection conn = new SqlConnection(connString);
string sqlLog = "select * from RegisterInformation where id='"+txtId.Text.Trim()+"'and password='"+txtPwd.Text.Trim()+"' ";
SqlCommand cmdL = new SqlCommand(sqlLog, conn);
conn.Open();
object resulta= cmdL.ExecuteScalar();
if (resulta!=null)
{
MessageBox.Show("登入成功");
}
else
{
MessageBox.Show("賬號或密碼錯誤,請重新輸入");
}
conn.Close();
}
private void btnReg_Click(object sender, EventArgs e)
{
Register form2 = new Register();
form2.Show();
}
private void LoginForm_Load(object sender, EventArgs e)
{
this.ActiveControl = this.txtId;
}
private void LoginForm_Paint(object sender, PaintEventArgs e)
{
}
}
}