三層登入實現

2021-07-10 21:41:25 字數 3743 閱讀 2804

一實體類model(與資料庫中的表對應)

public

class user

private name as

string

private password as

string

private userid as

string

private level as

string

private account as

string

'以下為可讀寫屬性

public

property user_name() as

string

getreturn name

endgetset(byval value as

string)

name = value

endsetend

property

public

property user_id() as

string

getreturn userid

endgetset(byval value as

string)

userid = value

endsetend

property

public

property user_pwd() as

string

getreturn password

endgetset(byval value as

string)

password = value

endsetend

property

public

property user_level() as

string

getreturn level

endgetset(byval value as

string)

level = value

endsetend

property

public

property user_account() as

string

getreturn account

endgetset(byval value as

string)

account = value

endsetend

property

endclass

二ui層-表現層 (介面的操作)

public

class form1

private

sub butcancel_click(byval sender as system.object, byval e as system.eventargs) handles butcancel.click

endendsub

private

sub butok_click(byval sender as system.object, byval e as system.eventargs) handles butok.click

'定義乙個實體類物件,將文字框中的內容賦給實體類物件

dim thisuser as model.user = new model.user

thisuser.user_id = txtuserid.text.tostring()

thisuser.user_pwd = txtuserpwd.text.tostring()

'定義乙個業務邏輯層物件,根據業務邏輯層返回的結果,通知使用者資訊

dim ub as bll.userbll = new bll.userbll

if ub.query_user(thisuser) then

msgbox(true)

else

msgbox(false)

endifend

subend

class

三bll層-業務邏輯層 (將資料層的資料返回,傳遞資料)

public

class userbll

'定義乙個d層的物件

dim db as dal.userdb = new dal.userdb

'此函式的作用是返回d層的執行結果

public

function query_user(byval model as model.user) as

boolean

return db.query_user(model)

endfunction

endclass

五dal層-資料訪問層 (資料庫連線,查詢語句)

imports system.data.sqlclient  

public

class userdb

'資料庫連線語句

dim str as

string = "data source=localhost;initial catalog=charge;persist security info=true;user id=sa;password=123456"

dim conn as sqlclient.sqlconnection

public

subnew() '建構函式 建立連線,開啟資料庫

conn = new sqlclient.sqlconnection

conn.connectionstring = str

conn.open()

endsub'查詢資料庫中的使用者是否存在

public

function query_user(byval tstuser as model.user) as

boolean

'資料庫查詢語句

dim sqlstr as

string

sqlstr = "select * from user_info where user_id='" & tstuser.user_id & "' and user_pwd = '" & tstuser.user_pwd & "'"

'查詢

dim sqlcmd as sqlclient.sqlcommand = new sqlclient.sqlcommand(sqlstr, conn)

dim myreader as sqldatareader '定義乙個reader用來讀資料

myreader = sqlcmd.executereader() '執行你的查詢,結果到myreader

if myreader.read() then

'如果查到了資料

return

true

else

return

false

endif

endfunction

endclass

總結:

學習了三層架構,將乙個系統分成dal,bll,ui層,實現了客戶端和資料庫之間的解耦,當我需要改動某一項功能時,只需改動本層的結構,這樣大大的提高了系統的可維護性和可擴充套件性。

三層的使用,使得系統有一定的層次,方便理解,當需求改動時不需要大範圍的修改,而且系統也易於擴充套件。

三層還是要我們多多的去理解,去應用,自己應該努力的地方還有很多。

三層登入實現

上篇部落格說到三層的四個問題 what why when how how部分沒有說,這篇部落格以登入為例,用c 實現三層登入,了解它的執行過程以及 詳解。下面這張圖是通過vs生成的時序圖,詳細地講述了登入的整個過程。上面這張圖有些繁瑣,其實簡單地說,就是ui層採集使用者的使用者名稱和密碼,然後傳遞給...

三層登入窗體

經過幾天的努力奮鬥,三層登入窗體終於完成了,雖然歷經風雨,但是經過小夥伴們的幫助,順利的學習到了很多的東西。u層 namespace loginui private void btnlogin click object sender,eventargs e b層 namespace login.bl...

三層架構之登入

在專案開發的過程中,有時把整個專案分為三層架構,其中包括 表示層 ui 業務邏輯層 bll 和資料訪 問層 dal 三層的作用分別如下 表示層 為使用者提供互動操作介面,這一點不論是對於web還是winform都是如此,就是使用者介面操作 我們 展示給使用者看的介面。業務邏輯層 負責關鍵業務的處理和...