确定行是否存在的最佳方法 [英] Best method for determining if a row exists
本文介绍了确定行是否存在的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用SQL Server和WebMatrix Razor语法来尝试确定用户名是否包含在名为Users的表中。
I am using SQL Server and WebMatrix Razor syntax to try and determine is a username is contained within a table called Users.
我希望的逻辑是:
bool usernameExists = db.QueryValue("SELECT Username from Users where Username = ?", username);
if(usernameExists.IsEmpty()){ //username is not in table
Response.Redirect("Register.cshtml");
}
进行此操作的最佳做法是什么?
What is the best practice for going about this?
推荐答案
最简单的可能是:
bool usernameExists = db.QueryValue("SELECT usernameExists = CASE WHEN EXISTS
(SELECT 1 FROM Users WHERE Username = ?) THEN 1 ELSE 0 END", username);
if (!usernameExists) { Response.Redirect("Register.cshtml"); }
虽然我严格地说从SQL Server端。我不知道这是否是从C#侧的最好的方法。
Though I'm speaking strictly from the SQL Server side. I don't know if this is the best approach from the C# side.
这篇关于确定行是否存在的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文