2.5登錄用戶驗(yàn)證功能設(shè)計(jì)
效果圖
畫面主要功能
新建數(shù)據(jù)庫(kù)典尾、自行查詢數(shù)據(jù)庫(kù)并從程序中獲取數(shù)據(jù)庫(kù)中用戶表的完整數(shù)據(jù)。
后臺(tái)數(shù)據(jù)庫(kù)表結(jié)構(gòu)
AOD.NET查詢數(shù)據(jù)庫(kù)的流程
- 導(dǎo)入命名空間;
- 定義數(shù)據(jù)庫(kù)連接字符串,創(chuàng)建Connection對(duì)象贞铣;
- 打開連接寥茫;
- 利用Command對(duì)象的ExecuteReader()方法執(zhí)行Select查詢語句掌桩;
- 利用ExecuteReader()方法返回的DataReader對(duì)象讀取數(shù)據(jù)边锁,顯示到界面上;
-
關(guān)閉連接拘鞋。
重要代碼片段以及詳細(xì)描述
···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 AdoNetDemo
{
public partial class DataBaseForm : Form
{
public DataBaseForm()
{
InitializeComponent();
}
private void DataBaseForm_Load(object sender, EventArgs e)
{
String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
// 在數(shù)據(jù)庫(kù)中查詢USERS表
String sqlStr = "select * from USERS";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
SqlDataReader dr = cmd.ExecuteReader();
// 解析數(shù)據(jù)
while (dr.Read())
{
String Id = dr["ID"].ToString();
String Name = dr["NAME"].ToString();
String Password = dr["PASSWORD"].ToString();
String Phone = dr["PHONE"].ToString();
// 注意是累加
this.tb_Users.Text += Id + ", " + Name + ", " + Password + ", " + Phone + "\r\n";
}
}
catch (Exception exp)
{
MessageBox.Show("數(shù)據(jù)庫(kù)連接失敗" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
private void tb_Users_TextChanged(object sender, EventArgs e)
{
}
}
}···
2.6密碼修改界面功能設(shè)計(jì)
效果圖
描述畫面主要功能砚蓬,并列出支持這些功能的后臺(tái)數(shù)據(jù)庫(kù)表結(jié)構(gòu)
ADO.NET更新數(shù)據(jù)庫(kù)的流程
代碼
···1.連接數(shù)據(jù)庫(kù)
String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
2.構(gòu)造數(shù)據(jù)庫(kù)并查詢
String sqlStr = "update EMPLOYEE set PASSWORD=@pwd where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@pwd", newPwd));
cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));
// 將命令發(fā)送給數(shù)據(jù)庫(kù)
int res = cmd.ExecuteNonQuery();
3.根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("密碼修改成功");
this.Close();
}
else
{
MessageBox.Show("密碼修改錯(cuò)誤");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.ToString());
}
finally
{
sqlConn.Close();
}
}···
其他鏈接
2.3:http://www.reibang.com/p/9428b92045a8
2.4:http://www.reibang.com/p/74edd5607899
2.9: