三層登陸程式碼及錯誤集錦
前言
在偷懶和學習之間還是選擇敲一遍。這種感覺很踏實。下面是關於三層登陸我的收穫。
三層登陸
顯示層(UI),業務邏輯層(BLL),資料訪問層(DAL)
三層登陸需要引用,U層引用B層,B層引用D層。裡面的User需要貫穿三層,為了較少耦合度,所以將User單獨拿出來,用三層去引用這個獨立層就好了。
程式碼展示
U層
//這裡要增加一個名稱空間
using System.Data;
using System.Data.SqlClient;
namespace LoginUI //-------------U層
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//IDbConnection conn = new SqlConnection("c...");
//IDbCommand cmd = conn.CreateCommand();
//cmd.CommandText = "Select UserName From Users where...";
//cmd.ExecuteReader();
string userName = txtUserName.Text.Trim();//將文字中的姓名賦值給新的變數userName
string passWord = txtPassword.Text;
Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();//例項化Login.BLL.LoginManager
Login .Model .UserInfo user = mgr.UserLogin(userName, passWord);//把U層的傳入實體
MessageBox.Show("登陸使用者:"+user.UserName );
}
}
}
B層
namespace Login.BLL//------------B層
{
public class LoginManager
{
public Login .Model .UserInfo UserLogin(string userName, string passWord)
{
//throw new NotImplementedException();
Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();//需要引用D層
Login .Model.UserInfo user= uDao.SelectUser(userName, passWord);
if (user != null)
{
Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();
sDao.UpdateScore(userName, 10);
return user; //---------將實體user返回U層就好
}
else
{
throw new Exception("登陸失敗");
}
}
}
}
D層
namespace Login.DAL//-----------D層
{
public class UserDAO//----使用者
{
public Login.Model .UserInfo SelectUser(string userName,string passWord)
{
using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"SELECT ID,UserName,Password,Email FROM USERS WHERE UserName = @UserName AND Password=@Password";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@UserName", userName));
cmd.Parameters.Add(new SqlParameter("@Password", passWord));
conn.Open(); //開啟資料庫
SqlDataReader reader = cmd.ExecuteReader();
Login.Model.UserInfo user = null;
while (reader.Read())
{
if (user == null)
{
user = new Login.Model.UserInfo();
}
user.ID = reader.GetInt32(0);
user.UserName = reader.GetString(1);
user.Password = reader.GetString(2);
if (!reader.IsDBNull(3))
{
user.Email = reader.GetString(3);
}
// return user;注意返回值在括號外面
}
return user;
}
}
}
}
namespace Login.DAL
{
public class ScoreDAO //積分類
{
public void UpdateScore(string userName, int value)//積分
{
using (SqlConnection conn = new SqlConnection(DbUtil.ConnString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"INSERT INTO SCORES(UserName,Score) Values(@UserName,@Score)";
cmd.Parameters.Add(new SqlParameter("@UserName", userName));
cmd.Parameters .Add (new SqlParameter ("@Score",value));
conn.Open(); //開啟資料庫
cmd.ExecuteNonQuery();
}
}
}
}
public class DbUtil //單獨資料庫類
{
public static string ConnString = @"server = LOVQ-G\SQLEXPRESS;database = Login; User ID = sa;Password=123";
//資料庫名稱+伺服器+使用者名稱密碼
}
實體層
namespace Login.Model//獨立層
{
public class UserInfo
{
public int ID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
}
登陸展示
漏洞百出
1.空值null。
這裡插入時,只有User和Score,沒有ID,但是資料庫中ID是主鍵,不可能為空值,這個時候會報錯。那麼怎麼解決呢?
將標識規範中選擇是,並選擇表示增量,這樣,資料庫就可以自動去給ID新增資料,也就不會出現null值了。
2public/引用
Login.bll這裡一開始報錯是因為沒有引用B層,所以.後面沒有東西。但是當我引用了B層之後還是報錯,
然後就去尋找程式碼,發現class之前沒有public,這樣的話,它就沒辦法訪問到這個類的程式碼。所以,處處都是細節哦。
後記
在敲三層登陸時還遇到了其他麻煩。只不過我們身後有那麼多小夥伴和師傅,所以我們就大膽的去嘗試吧!
希望這篇部落格能讓一些小夥伴們有收穫~
相關文章
- Go 常見錯誤集錦 | 字串底層原理及常見錯誤Go字串
- Go常見錯誤集錦 | 字串底層原理及常見錯誤Go字串
- C#七層登陸及程式碼展示C#
- discuz教程QQ登陸提示錯誤程式碼:2000
- QQ登陸不上 QQ登陸錯誤0x00000001程式碼怎麼辦
- iOS 開發錯誤集錦iOS
- Android開發錯誤集錦Android
- 【論文寫作】錯誤集錦
- [python]python錯誤集錦Python
- 【ITOO】--Jekins整合錯誤集錦
- 單點登陸原理及程式碼(CAS)
- angular——線上競拍錯誤集錦(一)Angular
- Android錯誤解決方法集錦Android
- 程式設計師容易忽略的SQL Server錯誤集錦程式設計師SQLServer
- FTP 登陸的常見問題與FTP命令集錦FTP
- Python錯誤集錦:IndentationError: unexpected indentPythonError
- Android Studio 編譯錯誤集錦Android編譯
- Python專案開發案例集錦及配套程式碼Python
- Manen下的Spring MVC專案錯誤 集錦SpringMVC
- callback 和 promise 的錯誤捕獲-暗坑集錦Promise
- MySQL常見錯誤程式碼及程式碼說明MySql
- PHP開發常用程式碼集錦PHP
- CSS常用功能程式碼集錦CSS
- SSH安全登陸原理:密碼登陸與公鑰登陸密碼
- AIX 5.3 Install Oracle 10g RAC 錯誤集錦AIOracle 10g
- Python錯誤集錦:for x in range(5) ^ SyntaxError: invalid syntaxPythonError
- SSH登陸錯誤 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!REMIDE
- 深度學習用於文字摘要的論文及程式碼集錦深度學習
- 面試集錦(三)spring面試Spring
- 解決IE上登陸oracle OEM時報:“證書錯誤,導航已阻止”的錯誤Oracle
- tornado登陸豆瓣程式碼例項
- [BUG反饋]登陸沒反應,審查元素提示錯誤
- oracle錯誤及解決方式集(轉)Oracle
- 安裝RAC 報錯 集錦
- 遠端連線錯誤程式碼及處理
- win10商店無法登陸提示錯誤程式碼0x800704cf解決方法Win10
- gradle 入坑指南(基本原理介紹+錯誤集錦)Gradle
- 技術問答集錦(三)