任務:用C#製作一個登錄登錄界面
目錄
7.寫代碼:關閉、打開等操作(顯示、隱藏、跳轉)
1.新建窗體,設計
如圖,分別爲Label、TextBox、Button。
2.添加新窗體
3.寫代碼:取出賬號、密碼;密碼加密
string username = textBox1.Text.Trim(); //取出賬號
string password = textBox2.Text.Trim(); //取出密碼
插眼:Trim()函數
是取出字符串中的空格,因爲賬號、密碼不會出現空格。
因爲密碼需要加密、無法顯示,將密碼處的TextBox的PasswordChar用來代替密碼的字符。
4. ※※※連接數據庫※※※
SQL中的sa爲系統管理員,點擊即可查看、修改本機的密碼。
插眼:修改sa密碼(先挖個坑,之後填上)
C#連接SQL,(代碼中的***即爲密碼,此處加密)
string myConnString = "Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=*******;
SqlConnection sqlConnection = new SqlConnection(myConnString); //實例化連接
sqlConnection.Open();
.是本機、STU是要連接的數據庫、sa是用戶名。
注:
這裏會報錯,雙擊點擊即可消除錯誤。
5.用C#寫SQL語句
將用戶賬號、密碼存入Usertable表中,將表中的賬號、密碼與username和password進行對比。
string sql = "select Userid,Password from Usertable where Userid = '"
+ username + "' and Password = '" + password + "'"; //編寫SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection); //在數據庫上執行SELECT操作
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); //讀取
Usertable表中的賬號、密碼。
插眼:(吐槽)這裏真的出了好多好多好多好多好多問題...
6.新建窗體,顯示數據庫中的表
點擊DataGridView。
點擊右上角的三角,點擊添加項目數據源,選擇數據庫——→數據集——→新建連接——→選擇Microsoft SQL Server。
服務器名:.;
身份驗證選擇SQL Server驗證;
用戶名、密碼即爲上文提到的管理員sa及密碼。
選擇要用到的數據庫:STU。
注:勾選“顯示將保存在應用程序中的連接字符串”。這段即爲前文提到的連接處的代碼。
之後接點擊下一步。
最後選擇用到的對象,這裏選擇數據庫中的表。
7.寫代碼:關閉、打開等操作
實例化窗體。
Form4 form4 = new Form4(); //實例化
顯示、隱藏、關閉窗體。
form2.Show(); //顯示窗體
this.Hide(); //隱藏當前窗體
this.Close(); //關閉當前窗體
跳轉至另一窗體。
form4.ShowDialog();
結束程序。
Application.Exit();
8.寫代碼:登錄成功、失敗
if (sqlDataReader.HasRows) //登錄成功
{
form2.Show();
}
else //登錄失敗
{
form3.Show();
}
注:這裏用的方法,若登錄成功打開form2,在form2的代碼中,點擊form2的確定跳轉至form4.
界面展示
form1——主界面:
form2——登錄成功:
form3——登錄失敗:
form4——數據庫表界面:
演示
登錄失敗:
登錄成功:
點擊確定跳轉至form4——表界面。
點擊取消結束所有程序。
代碼展示
form1——主界面:
注:這裏的Password我用***代替了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //“確定”按鈕
{
string username = textBox1.Text.Trim(); //取出賬號
string password = textBox2.Text.Trim(); //取出密碼
string myConnString = "Data Source=.;Initial Catalog=STU;Persist Security Info=True;User ID=sa;Password=******"; //連接數據庫
SqlConnection sqlConnection = new SqlConnection(myConnString); //實例化連接對象
sqlConnection.Open();
string sql = "select Userid,Password from Usertable where Userid = '" + username + "' and Password = '" + password + "'"; //編寫SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection); //在數據庫上執行SELECT操作
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); //讀取數據
Form4 form4 = new Form4(); //實例化
Form3 form3 = new Form3(); //實例化
Form2 form2 = new Form2(); //實例化
if (sqlDataReader.HasRows) //登錄成功
{
this.Hide(); //隱藏當前窗體
form2.ShowDialog(); //新打開窗體
this.Close(); //關閉當前窗體
}
else //登錄失敗
{
form3.Show();
}
sqlConnection.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
form2——登錄成功:
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;
namespace Login
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide(); //隱藏當前窗體
Form4 form4 = new Form4(); //實例化
form4.ShowDialog(); //新打開窗體
this.Close(); //關閉當前窗體
}
}
}
form3——登錄失敗:
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;
namespace Login
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
form4——數據庫表界面:
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;
namespace Login
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數據加載到表“sTUDataSet3.Student”中。您可以根據需要移動或刪除它。
this.studentTableAdapter.Fill(this.sTUDataSet3.Student);
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
總結
本來以爲很簡單,真的出了好多好多問題...