51
1 个个个个个个个个个个个个

1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

Embed Size (px)

Citation preview

Page 1: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

1

一个银行数据库应用开发实例

Page 2: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

2

银行数据库需求

Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户( customer )有 0 个或多个账户 一个支行 有多个信贷( loan )记录 客户可以存钱( deposit )或取钱( withdraw ) 客户可以转账( transfer ) 客户可以借贷( borrow )或还贷( payback )( 为简化问题,不考虑利息 )

Page 3: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

3

Use caseBank System

CustomerManagement

AccountManagement

QueryAndReport

LoanManagement

Manager

Clerk

BankManagement

<<uses>>

<<uses>>

<<uses>>

<<uses>>

<<uses>>

<<uses>>

注意:此处忽略了数据库管理部分

Page 4: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

4

功能分析BankManagement

Add Branch

Delete Branch

Modify Branch

CustomerManagement

Add Customer

Delete Customer

Modify Customer

AccountManagement

Add Account

Delete Account

Deposit

Withdraw

Transfer

LoanManagement

Add Loan

Delete Loan

Borrow

Payback

QueryAndReport

Customer Info

Customer Account

Customer Loan

Bank Info

Bank Account

Bank Loan

Page 5: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

5

如果某个功能比较复杂,画一个活动图

input account A

display info of account A

input tannsfer amount

display info of account B

input account B

balance of account A > transfer amount ?

bagin a tranastion

subtract amount from A

Add amount into B

Success ?

commitrollback

Y

N

Y

N

report error

Page 6: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

6

使用 E-R 图设计

branch account customer

loan

has depositor

has borrower

10..nn1

11

0..nm

account_access

access

loan_access

access

1..2

n

n

1

Page 7: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

7

branch account customer

loan

has depositor

has borrower

10..nn1

11 0..nm

account_access

access

loan_access

access

1..2

n

n

1

Branch name

Branch city

assets

Account number

balance

Customer name

Customerstreet

Customer city

Loan number amount

access number Access type amounttime

access type can be can be deposit, withdraw or transfer.No primary key – weak entityset

access type can be can be deposit, withdraw or transfer.No primary key – weak entityset

access numberAccess type amount

time

access type can be can be borrow or payback.No primary key – weak entityset

access type can be can be borrow or payback.No primary key – weak entityset

Page 8: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

8

branch account customer

loan

has depositor

has borrower

10..nn1

11 0..nm

account_access

access

loan_access

access

1..2

n

n

1

Branch name

Branch city

assets

Account number

balance

Customer name

Customerstreet

Customer city

Loan number amount

access numberAccess type amount

time

access number Access type amounttime

Page 9: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

9

1. branch (branch_name, branch_city, assets)2. customer (customer_name, customer_street, customer_city)3. account (account_number, branch_name, balance)4. loan (loan_number, branch_name, amount)5. depositor (customer_name, account_number)6. borrower (customer_name, loan_number)7. account_access (account_number, access_number, access_type, amount, transfer_account_number, access_date, access_time)8. loan_access (loan_number, access_number, access_type, amount, access_date, access_time)

转换为模式

Primary key foreign key

Page 10: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

10

检查模式设计

使用设计范式检查 如果模式设计的不够好,返回上一步

进行改进

考虑以下设计 :Account-branch (account_number,branch_name,branch_city, branch_assets,account_balance)

Page 11: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

11

角色和权限Role table

privilege

select insert delete update

Manager

branch √ √ √ √

customer √

account √

loan √

depositor √

borrower √

account_access √

loan_access √

Clerk

branch √

customer √ √ √ √

account √ √ √ √

loan √ √ √ √

depositor √ √ √ √

borrower √ √ √ √

account_access √ √ √ √

loan_access √ √ √ √

Page 12: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

12

建数据库 Logon to pgAdmin as

administrator (postgres) Create a new DB

Page 13: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

13

建登录角色 Create role manager

and clerk

Page 14: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

14

写建表脚本create table account (account_number varchar(15) not null unique, branch_name varchar(15) not null, balance numeric not null, primary key(account_number));

create table branch (branch_name varchar(15) not null unique, branch_city varchar(15) not null, assets numeric not null, primary key(branch_name));

Save this file as make-bank.sql so that you can use it later.

Page 15: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

15

create table customer (customer_name varchar(15) not null unique, customer_street varchar(12) not null, customer_city varchar(15) not null, primary key(customer_name));

create table loan (loan_number varchar(15) not null unique, branch_name varchar(15) not null, amount numeric not null, primary key(loan_number));

Page 16: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

16

create table depositor (customer_name varchar(15) not null, account_number varchar(15) not null, primary key(customer_name, account_number), foreign key(account_number) references account(account_number), foreign key(customer_name) references customer(customer_name));

create table borrower (customer_name varchar(15) not null, loan_number varchar(15) not null, primary key(customer_name, loan_number), foreign key(customer_name) references customer(customer_name), foreign key(loan_number) references loan(loan_number));

Page 17: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

17

create table account_access (account_number varchar(15) not null, access_number varchar(15) not null, access_type varchar(15) not null, amount numeric not null, transfer_account_number varchar(15), access_date date, access_time time, primary key(account_number, access_number), foreign key(account_number) references account(account_number), foreign key(transfer_account_number) references account(account_number), check (access_type in ('deposit','withdraw','transfer')));

Page 18: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

18

create table loan_access (loan_number varchar(15) not null, access_number varchar(15) not null, access_type varchar(15) not null, amount numeric not null, access_date date, access_time time, primary key(loan_number, access_number), foreign key(loan_number) references loan(loan_number), check (access_type in ('borrow','payback')));

Page 19: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

19

授权grant select on branch, customer, account, loan, depositor, borrower, account_access, loan_access to manager;

grant insert, delete, update on branch to manager;

grant select on branch, customer, account, loan, depositor, borrower, account_access, loan_access to clerk;

grant insert, delete, update on customer, account, loan, depositor, borrower, account_access, loan_access to clerk;

Page 20: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

20

为每个功能准备 SQL 语句 例:查询用户名为 'custerName‘的用户的信息

SELECT customer.customer_name, account.account_number, account.branch_name, account.balanceFROM customer,account,depositorWHERE customer.customer_name = depositor.customer_name AND account.account_number = depositor.account_number AND customer.customer_name = 'custerName';

Page 21: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

21

在数据库中建表 Click “open file” and open the

“make-bank.sql” Select the create and insert commands

and execute

Page 22: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

22

配置 ODBC 数据源

Page 23: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

23

Page 24: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

24

创建访问界面 为了简化,忽略了

系统登录部分,并且把 manager 和 clerk 的页面合在一起。

Page 25: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

25

Select “design” panel

Right-click on the page and select “properties”

Modify the title of the web page

Page 26: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

26

Draw a label into the page text : “Bank Demo”Font -> zise: “xx-large”

Draw a “horizontal rule”

Page 27: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

27

Add HyperlinkText: “Edit Branch”NavigateUrl: EditBranch.aspxTarget: _blank (open in a new page)

Page 28: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

28

In the solution explorer, right-click the solution name -> add -> new itemEditBranch.aspx

Page 29: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

29

Draw a GridView control into the EditBranch.aspx and set the data source

Page 30: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

30

Select the bank connection

Page 31: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

31

Page 32: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

32

Page 33: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

33

Page 34: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

34

Set gridview properties

Page 35: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

35

Click source Remove the “[“ and “]” from the source

code since it is not supported by PostgreSQL

Page 36: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

36

Add text box and button to support inserting a new branch

Double-click button to add code: string connectString = "Dsn=PostgreSQL_manager;uid=manager;pwd=manager";

System.Data.Odbc.OdbcConnection myConn = new System.Data.Odbc.OdbcConnection(connectString); myConn.Open();

System.Data.Odbc.OdbcCommand insertBranch = myConn.CreateCommand(); insertBranch.CommandText = "INSERT INTO branch (branch_name,"; insertBranch.CommandText += " branch_city, assets) VALUES ('"; insertBranch.CommandText += (this.TextBox1.Text + "','" + this.TextBox2.Text + "'," + this.TextBox3.Text + ")");

insertBranch.ExecuteNonQuery(); insertBranch.Dispose(); myConn.Close(); this.GridView1.DataBind(); //refresh the gridview

Page 37: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

37

Add a new page CheckCustomer.aspx

Draw a ListBox into the page

Choose data source

Page 38: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

38

Page 39: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

39

Page 40: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

40

Add a new connection

Page 41: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

41

Page 42: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

42

Page 43: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

43

select customer_name from customer

Page 44: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

44

Add a new button named query Add a new GridView and create new

data source

Page 45: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

45

Page 46: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

46

Page 47: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

47

select customer.customer_name, account.account_number, account.branch_name, account.balancefrom customer,account,depositorwhere customer.customer_name = depositor.customer_name and account.account_number = depositor.account_number

Page 48: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

48

Set the “Visible” to be false so that it will not display before querying

Page 49: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

49

Add code for the “query” button

string custerName = this.ListBox1.Items[this.ListBox1.SelectedIndex].Text;

this.SqlDataSource2.SelectCommand += ("and customer.customer_name = '" + custerName + "'");

this.GridView1.Visible = true; this.GridView1.DataBind();

Page 50: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

50

小结 在本例中

有些问题没有考虑,如安全、异常处理、数据备份等。 图形界面的设计和实现,有多种方法,没有绝对的标准。

应用开发 在开发一个项目之前,你不可能什么都知道。遇到问题后去

研究解决,最好的方法是在网上搜索。 参考资料

For some specific issues about DB development, you can search on Code Project: http://www.codeproject.com/search.aspx?q=database&sbo=kw&pgnum=3

For real DB application, you can search on sourceforge: http://sourceforge.net/search/?q=erp (ERP is the common

DB application)

Page 51: 1 一个银行数据库应用 开发实例. 2 银行数据库需求 Requirements 一个银行有多个支行( branches ) 一个支行管理多个用户账户( accounts ) 一个客户(

51

End of Chapter