2016-08-22 77 views
0

我在我的网页上有一个表单,我希望在提交表单时在后台发布到我的数据库。这里是我的代码:使用ASP.NET将数据插入到数据库中

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)

+0

通过标记'sql',您可以简单地通过应用'unique' _constraint_来检查用户是否存在。查看链接,其中提供了有关如何将记录插入数据库的详细信息。 http://www.onlinebuff.com/article_step-by-step-select-insert-update-and-delete-using-aspnet-c-and-adonet_32.html – BNN

回答

1

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

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

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

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