2016-08-22 43 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); 
相關問題