ASP.NET2.0网站开发(2)数据库设置
本节主要对数据库进行设置:
软件中所使用的数据库为MS SqlServer2000,建立一个数据库Books,并在库中建立两个数据表:
用户表:
CREATE TABLE [dbo].[USERTAB] (
[XH] [int] IDENTITY (1, 1) NOT NULL ,
[DLMC] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[YHMC] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[YHKL] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
[XH] [int] IDENTITY (1, 1) NOT NULL ,
[DLMC] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[YHMC] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[YHKL] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
此表只是做为测试表,有一个主键,没有索引,字段依次为:序号、登录名称、用户名称、用户口令,在用户登录时,将在此表中查询用户的资料,如果为合法用户则登录,反之提示错误。在实际使用过程中,可以使用MD5、SHA1等方式对口令进行加密处理。
图书资料表:
CREATE TABLE [dbo].[BOOKS] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[MC] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[CBS] [nvarchar] (60) COLLATE Chinese_PRC_CI_AS NULL ,
[DJ] [money] NOT NULL ,
[NRTY] [nvarchar] (2000) COLLATE Chinese_PRC_CI_AS NULL ,
[TP] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE INDEX [IX_BOOKS] ON [dbo].[BOOKS]([MC]) ON [PRIMARY]
GO
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[MC] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[CBS] [nvarchar] (60) COLLATE Chinese_PRC_CI_AS NULL ,
[DJ] [money] NOT NULL ,
[NRTY] [nvarchar] (2000) COLLATE Chinese_PRC_CI_AS NULL ,
[TP] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE INDEX [IX_BOOKS] ON [dbo].[BOOKS]([MC]) ON [PRIMARY]
GO
此表中存放保存的图书资料,字段依次为:序号、图书名称、出版社、定价、内容提要、图片。
根据个人的习惯,我将数据的操作以存储过程的方式保存的数据库中,这样做可以将开发过程中易出现问题地方单独调试,在保证数据库没有错误的前提下,尽可能简单的在代码中进行开发,并且在大数据量的情况下,以存储过程的方式进行查询能获得最佳查询速度。
CREATE PROCEDURE dbo.Books_AddValue
@mc NVARCHAR(20),
@cbs NVARCHAR(60),
@dj MONEY,
@nrty NVARCHAR(2000),
@tp IMAGE
AS
INSERT INTO dbo.BOOKS (
MC,
CBS,
DJ,
NRTY,
TP
) VALUES (
@mc,
@cbs,
@dj,
@nrty,
@tp
)
RETURN
GO
CREATE PROCEDURE dbo.Books_Delete
@id INT
AS
DELETE FROM dbo.BOOKS
WHERE dbo.BOOKS.ID = @id
RETURN
GO
CREATE PROCEDURE dbo.Books_GetValue
@id INT
AS
SELECT
dbo.BOOKS.ID,
dbo.BOOKS.MC,
dbo.BOOKS.CBS,
dbo.BOOKS.DJ,
dbo.BOOKS.NRTY,
dbo.BOOKS.TP
FROM dbo.BOOKS
WHERE dbo.BOOKS.ID = @id
RETURN
GO
CREATE PROCEDURE dbo.Books_GetValues
AS
SELECT
dbo.BOOKS.ID,
dbo.BOOKS.MC,
dbo.BOOKS.CBS,
dbo.BOOKS.DJ,
dbo.BOOKS.NRTY
FROM dbo.BOOKS
ORDER BY dbo.BOOKS.ID
RETURN
GO
CREATE PROCEDURE dbo.Books_Update
@id INT,
@mc NVARCHAR(20),
@cbs NVARCHAR(60),
@dj MONEY,
@nrty NVARCHAR(2000),
@tp IMAGE
AS
UPDATE dbo.BOOKS SET
dbo.BOOKS.MC=@mc,
dbo.BOOKS.CBS = @cbs,
dbo.BOOKS.DJ = @dj,
dbo.BOOKS.NRTY = @nrty,
dbo.BOOKS.TP = @tp
WHERE dbo.BOOKS.ID = @id
RETURN
GO
CREATE PROCEDURE dbo.User_GetValue
@dlmc NVARCHAR(20)
AS
SELECT
dbo.USERTAB.XH,
dbo.USERTAB.DLMC,
dbo.USERTAB.YHMC,
dbo.USERTAB.YHKL
FROM dbo.USERTAB
WHERE dbo.USERTAB.DLMC = @dlmc
RETURN
GO
@mc NVARCHAR(20),
@cbs NVARCHAR(60),
@dj MONEY,
@nrty NVARCHAR(2000),
@tp IMAGE
AS
INSERT INTO dbo.BOOKS (
MC,
CBS,
DJ,
NRTY,
TP
) VALUES (
@mc,
@cbs,
@dj,
@nrty,
@tp
)
RETURN
GO
CREATE PROCEDURE dbo.Books_Delete
@id INT
AS
DELETE FROM dbo.BOOKS
WHERE dbo.BOOKS.ID = @id
RETURN
GO
CREATE PROCEDURE dbo.Books_GetValue
@id INT
AS
SELECT
dbo.BOOKS.ID,
dbo.BOOKS.MC,
dbo.BOOKS.CBS,
dbo.BOOKS.DJ,
dbo.BOOKS.NRTY,
dbo.BOOKS.TP
FROM dbo.BOOKS
WHERE dbo.BOOKS.ID = @id
RETURN
GO
CREATE PROCEDURE dbo.Books_GetValues
AS
SELECT
dbo.BOOKS.ID,
dbo.BOOKS.MC,
dbo.BOOKS.CBS,
dbo.BOOKS.DJ,
dbo.BOOKS.NRTY
FROM dbo.BOOKS
ORDER BY dbo.BOOKS.ID
RETURN
GO
CREATE PROCEDURE dbo.Books_Update
@id INT,
@mc NVARCHAR(20),
@cbs NVARCHAR(60),
@dj MONEY,
@nrty NVARCHAR(2000),
@tp IMAGE
AS
UPDATE dbo.BOOKS SET
dbo.BOOKS.MC=@mc,
dbo.BOOKS.CBS = @cbs,
dbo.BOOKS.DJ = @dj,
dbo.BOOKS.NRTY = @nrty,
dbo.BOOKS.TP = @tp
WHERE dbo.BOOKS.ID = @id
RETURN
GO
CREATE PROCEDURE dbo.User_GetValue
@dlmc NVARCHAR(20)
AS
SELECT
dbo.USERTAB.XH,
dbo.USERTAB.DLMC,
dbo.USERTAB.YHMC,
dbo.USERTAB.YHKL
FROM dbo.USERTAB
WHERE dbo.USERTAB.DLMC = @dlmc
RETURN
GO
在SqlServer中与Oracle相比较而言,返回数据还是很简单的,在Oracle中需要使用游标等操作方式,才能返回数据集。如果查询的数据量很大,如表中有千万级,那么在查询时,最好只返回你当前需要的数据,比如一页。当然了,合适的索引、行列分区都是不可少的。
数据库内的代码就列出这么多,下一节开始对与数据库关联的数据类进行开发。