CREATE TABLE [dbo].[Users] (
[id] INT IDENTITY (1, 1) NOT NULL,
[UserName] NVARCHAR (50) NULL,
[week] INT NULL,
[PassWord] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([id] ASC)
);
其中需要我门写的如下图所示
前台页面代码
后台功能代码
using System;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;
using System.Configuration;
namespace ClassApp
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Title = "Class";
TextBox1.Text = "admin";
PasswordBox.Password = "admin";
}
private void loginButton_Click(object sender, RoutedEventArgs e)
{
//string sqlconnstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//SqlConnection sqlconn = new SqlConnection(sqlconnstr);
//sqlconn.Open();
//if (sqlconn.State == ConnectionState.Open)
//{
// MessageBox.Show("connection successful");
//}
//else
//{
// MessageBox.Show("connection failure");
//}
//sqlconn.Close();
//sqlconn = null;
//连接数据库
//string con = "Server=.;Database=DatabaseDesign;user id=sa;pwd=1996";
//string sqlconnstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//SqlConnection sqlconn = new SqlConnection(sqlconnstr);
//sqlconn.Open();
//SqlDataReader sdr = sqlCmd.ExecuteReader();
//SqlCommand sqlCmd = new SqlCommand("select * from Users", sqlconn);
//if (TextBox1.Text == "")
//{
// MessageBox.Show("账号不可为空!");
//}
//if (sqlconn.State == ConnectionState.Open)
//{
// MessageBox.Show("connection successful");
//}
//else
//{
// MessageBox.Show("connection failure");
//}
//1
string userName = ""; //Sql->UserName
string showbb = "";//test字符
int jump = 0;
int week = -1; //Sql->Week
int id = 0; //Sql->ID
int accountNumberState = 0; //账号登录状态
//string sqlconnstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//SqlConnection sqlCon = new SqlConnection(sqlconnstr); //连接数据库
//sqlCon.Open();
string sqlconnstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
sqlconn.Open();
//SqlDataReader sdr = SqlHelper.ExecuteReader();
//SqlCommand sqlCmd = new SqlCommand("select * from Database", sqlconn);
SqlCommand com = sqlconn.CreateCommand();
com.CommandText = "select * from Users";
SqlDataReader read = com.ExecuteReader();
//while (read.Read())查询test
//{
// MessageBox.Show(read.GetString(1)+";");
//}
//sqlconn.Close();
if (TextBox1.Text == "")
{
MessageBox.Show("账号不可为空!");
}
else
{
while (read.Read())
{
if (TextBox1.Text == read["UserName"].ToString().Trim() && PasswordBox.Password.Trim() == read["PassWord"].ToString().Trim())
{
//accountNumber Week&ID
week = Convert.ToInt32(read["Week"].ToString());
id = Convert.ToInt32(read["ID"].ToString());
userName = read["UserName"].ToString();
accountNumberState = 1;
break;
}
}
if (accountNumberState == 0)
{
MessageBox.Show("账号或密码错误");
}
}
if (accountNumberState == 1)
{
MessageBox.Show("登录成功");
UserList UserListWindow = new UserList();//跳转用户列表页面
UserListWindow.Show();
this.Close();
////初始过程中传入账号ID等
//Login loginWindow = new Login(week,id,userName);
//sqlCon.Close();
//loginWindow.Show();
//this.Close();
}
else
{
sqlconn.Close();
}
}
private void registeredButton_Click(object sender, RoutedEventArgs e)
{
Registered registerdWindow = new Registered();
registerdWindow.Show();
this.Close();
}
}
}
前台页面代码
后台功能代码
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Windows;
namespace ClassApp
{
///
/// Registered.xaml 的交互逻辑
///
public partial class Registered : Window
{
public Registered()
{
InitializeComponent();
Random random = new Random();
verificationLable1.Content = random.Next(0, 50).ToString();
verificationLable3.Content = random.Next(0, 50).ToString();
}
//'取消'button
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
this.Close();
}
//'注册'button
private void registeredButton_Click(object sender, RoutedEventArgs e)
{
string classRowAndColumn = "";
string userName = "";
if (AccountCorrectness())
{
MessageBox.Show("1.账号&密码长度为3-16个字符 \n2.不包含空格");
}
else if (IsSameAccount())
{
Random random = new Random();
//
registeredAccountNumberTextBox.Text = null;
registeredPassWordBox.Password = null;
checkPassWordBox.Password = null;
VerificationCodeTextBox.Text = null;
verificationLable5.Content = null;
verificationLable1.Content = random.Next(0, 50).ToString();
verificationLable3.Content = random.Next(0, 50).ToString();
MessageBox.Show("账号已存在,请重新输入");
}
else if (registeredPassWordBox.Password != checkPassWordBox.Password)
{
MessageBox.Show("两次密码不一致");
}
else if (verificationLable5.Content.ToString() == "×" || verificationLable5.Content.ToString() == "")
{
MessageBox.Show("验证码错误");
}
else //数据库的操作
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
sqlconn.Open();
string addAcountToDB = string.Format("insert into Users (UserName,PassWord) values ("+registeredAccountNumberTextBox.Text.ToString()+ "," + checkPassWordBox.Password+ ")");
SqlCommand sqlCmd = new SqlCommand(addAcountToDB, sqlconn);
sqlCmd.ExecuteNonQuery(); //保存INSERT操作
MessageBox.Show("注册成功!");
//注册成功后返回登录界面
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
this.Close();
}
}
//注册账号的长度验证
private bool AccountCorrectness()
{
if(registeredAccountNumberTextBox.Text.Length > 18 || registeredAccountNumberTextBox.Text.Length<3)
{
return true;
}
if(registeredPassWordBox.Password.Length >18 || registeredPassWordBox.Password.Length < 3)
{
return true;
}
return false;
}
//验证码对错
private void button3_Click(object sender, RoutedEventArgs e)
{
if(VerificationCodeTextBox.Text=="")
{
verificationLable5.Content = "×";
}
else if (Convert.ToInt32(verificationLable1.Content) + Convert.ToInt32(verificationLable3.Content.ToString()) == Convert.ToInt32(VerificationCodeTextBox.Text))
{
verificationLable5.Content = "√";
}
else
{
verificationLable5.Content = "×";
}
}
//验证账号是否重复
private bool IsSameAccount()
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
sqlconn.Open();
SqlCommand sqlCmd2 = new SqlCommand("select * from Users", sqlconn);
SqlDataReader sDR = sqlCmd2.ExecuteReader();
while (sDR.Read())
{
if(registeredAccountNumberTextBox.Text==sDR["UserName"].ToString().Trim())
{
sqlconn.Close();
sDR.Close();
return true;
}
}
sqlconn.Close(); //关闭连接
sDR.Close();
return false;
}
}
}
前台页面代码
后台功能代码
using System;
using System.Data.SqlClient;
using System.Windows;
using System.Configuration;
using System.Data;
namespace ClassApp
{
///
/// UserList.xaml 的交互逻辑
///
public partial class UserList : Window
{
public UserList()
{
InitializeComponent();
}
class DataBaseHelper
{
/// 数据库打开连接的方法
///
///
///
public static SqlConnection getSqlConnection()
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(sqlconnstr);
try
{
//获取数据库字符串
sqlConnection.Open();
sqlConnection.Close();
}
catch
{
throw new Exception("无法连接数据库服务器");
}
return sqlConnection;
}
/// sql增删改的方法
///
///
///
///
public static int GetNonQueryEffect(string sqlstr)
{
SqlConnection sqlConnection = new SqlConnection();
try
{
sqlConnection.Open();
//创建要执行的语句
SqlCommand cmd = new SqlCommand(sqlstr, sqlConnection);
return cmd.ExecuteNonQuery();//返回执行语句中的错误
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
sqlConnection.Close();
sqlConnection.Dispose();//释放资源
}
}
/// 读取数据的的方法
///
///
///
///
public static DataSet GetDataset(string sqlstr)
{
SqlConnection conn = getSqlConnection();
try
{
conn.Open();//打开数据库连接
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);//更新数据库的命令
DataSet ds = new DataSet();
sda.Fill(ds);//填充
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
conn.Close();
conn.Dispose();
}
}
public static DataSet DeleteGetData(string sqlstr)
{
SqlConnection conn = getSqlConnection();
try
{
conn.Open();//打开数据库连接
//SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);//更新数据库的命令
DataSet ds = new DataSet();
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
conn.Close();
conn.Dispose();
}
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
string str = "select * FROM Users";//查询的语句
dataGrid.ItemsSource = DataBaseHelper.GetDataset(str).Tables[0].DefaultView;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (textBox.Text.Trim() == " ")
{
return;
}
string strr = string.Format("select * FROM Users where UserName='{0}'", textBox.Text);
dataGrid.ItemsSource = DataBaseHelper.GetDataset(strr).Tables[0].DefaultView;
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
try
{
MessageBoxResult dr = MessageBox.Show("是否删除该条数据?", "系统提示", MessageBoxButton.OK);
if (dr == MessageBoxResult.OK)
{
if (DeleteText.Text.Trim() == " ")
{
return;
}
string strrr = string.Format("delete from Users where UserName ='"+DeleteText.Text.ToString()+"'");
MessageBox.Show(DeleteText.Text.ToString());
dataGrid.ItemsSource = DataBaseHelper.DeleteGetData(strrr).Tables[0].DefaultView;
MessageBox.Show("删除成功");
}
}
catch (Exception)
{
throw;
}
}
}
}