您好我有一个网站,我使用用户表。我希望能够添加用户但不能删除它们 - 我需要更新数据库中的文件,说明已删除 - 任何想法如何执行此操作?
答案 0 :(得分:0)
我建议使用Linq to SQL或Entity Framework(或其他ORM)之类的东西,并创建一个CRUD存储库(创建,读取,更新,删除)。但是“D”的方法实际上是更新isDeleted
字段的更新方法。
以下是我的CRUD存储库的示例(也包含服务层) http://dotnetblogger.com/post/2010/07/04/MVC2-Separating-Concerns-with-Service-and-Repository-Layers.aspx
示例:
Public Class UserRepository : Implements IUserRepository
Private dc As MyDatabaseDataContext
Public Sub New()
dc = New MyDatabaseDataContext
End Sub
''# note, this adds a user based on an OpenID - might not be what you're looking for
Public Sub AddUser(ByVal openid As OpenID) Implements IUserRepository.AddUser
Dim user As New User
user.MemberSince = DateTime.Now
openid.User = user
dc.OpenIDs.InsertOnSubmit(openid)
End Sub
''# This gets users as Queryable so that we can defer the query till the end.
Public Function GetUsers() As IQueryable(Of User) Implements IUserRepository.GetUsers
Dim users = (From u In dc.Users
Select u)
Return users.AsQueryable
End Function
''# Here's the magic function that you're looking for. DELETE is actually an UPDATE.
Public Sub DeleteUser(ByVal user as User) Implements IUserRepository.DeleteUser
Dim _user = (From u In dc.Users
Where u.ID = user.ID
Select u).Single
_user.isDeleted = True
End Sub
''# Basic Update Method
Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser
Dim _user = (From u In dc.Users
Where u.ID = user.ID
Select u).Single
With _user
.About = user.About
.BirthDate = user.BirthDate
.Email = user.Email
.isClosed = user.isClosed
.isProfileComplete = user.isProfileComplete
.RegionID = user.RegionID
.Reputation = user.Reputation
.UserName = user.UserName
.WebSite = user.WebSite
End With
End Sub
''# Make sure to call SubmitChanges
Public Sub SubmitChanges() Implements IUserRepository.SubmitChanges
dc.SubmitChanges()
End Sub
End Class
答案 1 :(得分:0)
我为每个实体创建一个自定义表单。