【问题标题】:Using ASP.NET to Insert Data into a Database使用 ASP.NET 将数据插入数据库
【发布时间】:2016-08-22 10:41:22
【问题描述】:

我的网页上有一个表单,我想在提交表单后在后台将其发布到我的数据库中。这是我的代码:

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);
    }
}

这是示例代码,我正在尝试解释它。令我困惑的是,上面写着VALUES (@0)。但是,当此页面上的表单提交时,它仍然设法发布输入的电子邮件地址,尽管值显示为 (@0) ?

任何澄清将不胜感激!

问候,

乔什

(P.S,我是 ASP.NET 新手)

【问题讨论】:

  • 通过标记sql,您可以通过应用unique 约束来简单地检查用户是否存在。查看链接,它为您提供有关如何将记录插入数据库的详细信息。 onlinebuff.com/…

标签: sql asp.net database


【解决方案1】:

@0 指第一个参数。在您的情况下,它是变量email

db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);

您可以像这样插入多个有序参数;

db.Execute("INSERT INTO UserProfile (Email, SecondParam) VALUES (@0, @1)", @0, @1);

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 2013-11-21
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2021-07-23
    • 2018-11-21
    • 1970-01-01
    • 2013-01-23
    相关资源
    最近更新 更多