實驗目的:
學習連接數據庫。
實驗內容:
使用Visual Studio編寫一個登錄系統,並且該系統還需要連接數據庫
目標效果:
實驗步驟:
1、數據庫數據準備:
需要在數據庫裏準備一張關於登錄的表,以便下面可以進行使用
2、創建新項目:
3、根據目標效果從工具箱添加控件:
初步效果:
4、按鈕’OK"的點擊事件:
(1)取出文本TextBox裏的文本
string username = textBox1.Text.Trim();//取出賬號
string password = textBox2.Text.Trim();//取出密碼
(2)連接、打開並使用數據庫,使用完畢需要關閉數據庫
string myConnString = "Data Source=.;Initial Catalog=Student;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);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
MessageBox.Show("WELCOME!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
label1.Text = "Log in:" + username;
}
else
{
MessageBox.Show("FAILED!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
sqlConnection.Close();
接着進行初步的測試:
5、按鈕“Cancel”的點擊事件:
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
6、對“OK”按鈕的點擊事件予以補充
實現功能:在輸入信息正確的前提下,彈出新的窗體,隱藏“Login”窗體
(1)在【解決方案管理器】中新建一個窗體
(2)添加代碼
FromMain fromMain = new FromMain();
fromMain.Show();
this.Hide();
(3)運行試驗:
成功
7、修改輸入密碼的TextBox的屬性:
修改屬性TextBox,使得輸入的時候無法看見輸入的密碼,而是以“*”代替。
修改屬性表中的“PasswordChar”,即可成功。
8、主窗體的構建:
(1)初步建立,爲窗體添加button按鈕和DataGridView控件
(2)添加項目數據源
測試連接:連接成功
(3)爲button按鈕添加點擊事件
運行效果:
點擊Cancel,則退出
輸入錯誤密碼時:
輸入正確時:
點擊確定後:
點擊Cancel,則退出
代碼:
From1.cs
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 Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text.Trim();//取出賬號
string password = textBox2.Text.Trim();//取出密碼
string myConnString = "Data Source=.;Initial Catalog=Student;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);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
MessageBox.Show("WELCOME!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
label1.Text = "Log in:" + username;
FromMain fromMain = new FromMain();
fromMain.Show();
this.Hide();
}
else
{
MessageBox.Show("FAILED!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
sqlConnection.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
FromMain.cs
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 FromMain : Form
{
public FromMain()
{
InitializeComponent();
}
private void FromMain_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數據加載到表“studentDataSet.Student”中。您可以根據需要移動或刪除它。
this.studentTableAdapter.Fill(this.studentDataSet.Student);
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
心得與體會:
嗯,學到了不少東西