首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为每个新用户WebMatrix更新数据库表中的行

问为每个新用户WebMatrix更新数据库表中的行
EN

Stack Overflow用户
提问于 2014-05-31 07:05:45
回答 1查看 146关注 0票数 0

我的数据库(StarterSite)中有一个可以工作的表(About_User),因此每个单独的行对应于每个单独的用户。但是,无论哪个用户登录,他们都有权访问同一行(换句话说,不是为每个新用户创建新行)。我已经进行了三次检查,以确保我的数据库中的所有内容都设置正确。我在此注册页面中使用StarterSite模板(为解决问题而编辑):

代码语言:javascript
复制
// If all information is valid, create a new account
    if (Validation.IsValid()) {
        // Insert a new user into the database
        var db = Database.Open("StarterSite");

        // Check if user already exists
        var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", email);
        if (user == null) {
            // Insert email into the profile table
            db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);

            // Create and associate a new entry in the membership database.
            // If successful, continue processing the request
            try {
                bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
                var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
                if (requireEmailConfirmation) {
                    var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
                    var confirmationUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/Confirm?confirmationCode=" + HttpUtility.UrlEncode(token));

                    WebMail.Send(
                        to: email,
                        subject: "Please confirm your account",
                        body: "Your confirmation code is: " + token + ". Visit <a href=\"" + confirmationUrl + "\">" + confirmationUrl + "</a> to activate your account."
                    );
                }

                if (requireEmailConfirmation) {
                    // Thank the user for registering and let them know an email is on its way
                    Response.Redirect("~/Account/Thanks");
                } else {
                    // Navigate back to the homepage and exit
                    WebSecurity.Login(email, password);

                    Response.Redirect("~/");
                }
            } catch (System.Web.Security.MembershipCreateUserException e) {
                ModelState.AddFormError(e.Message);
            }
        } else {
            // User already exists
            ModelState.AddFormError("Email address is already in use.");
        }
    }

该表确实有一个(应该)自动递增的ID列。

其他列来自一个表单,它从用户那里收集不同的信息,比如“关于我”页面,因此每个人都应该是不同的。

我如何才能做到这一点呢?

EN

回答 1

Stack Overflow用户

发布于 2014-05-31 17:05:30

你的问题中有一些我不明白的地方。首先,您的代码看起来没有经过编辑(它与第39行到第82行的模板相同)。其次,你的问题有mysql标签:如果你使用的是MySql数据库而不是入门网站模板的Sql Ce数据库,也许这就是问题所在(看看WebSecurity.CreateAccount fails for MySQL)。

使用默认的StarteSite Sql Ce数据库,您的问题的答案非常简单。添加以下行

代码语言:javascript
复制
db.Execute("INSERT INTO About_User(UserId, UserPage) VALUES (@0, @1)", 
    db.GetLastInsertId(), userPage);

紧跟在db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);语句之后。

您的About_User表中不需要ID自动增量列,只需要一个UserId主键列作为外键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23964604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档