Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查sql表上是否存在值?_C#_Sql - Fatal编程技术网

C# 如何检查sql表上是否存在值?

C# 如何检查sql表上是否存在值?,c#,sql,C#,Sql,嗨,现在我正在用C#编程,正在为他们的帐户进行用户注册。我已经完成了向sql插入值,现在我想检查该值是否存在? 表格详情: Username Password Peter abc Hannah abd John bdf 我建议使用一种简单的方法将Username设置为主键,并使用try-catch-error来检测它。但是我想找到找到我的用户名存在的具体方法。Tks

嗨,现在我正在用C#编程,正在为他们的帐户进行用户注册。我已经完成了向sql插入值,现在我想检查该值是否存在? 表格详情:

Username              Password
Peter                 abc  
Hannah                abd 
John                  bdf 
我建议使用一种简单的方法将Username设置为主键,并使用try-catch-error来检测它。但是我想找到找到我的用户名存在的具体方法。Tks

SELECT TOP 1 Username FROM mytable WHERE Username='Peter'
然后您可以使用代码来确定是否有返回的行

您可以使用这种方法进行登录。但是对于注册一个新用户,不能保证在您检查和插入新记录之间没有人使用用户名。所以在任何情况下,尝试。。。捕获是不可避免的

然后您可以使用代码来确定是否有返回的行


您可以使用这种方法进行登录。但是对于注册一个新用户,不能保证在您检查和插入新记录之间没有人使用用户名。所以在任何情况下,尝试。。。捕获是不可避免的。

如果要在尝试插入之前检查该值是否存在,可以编写如下SQL语句:

if not exists (select 1 from table where username=@username)
begin
     insert into ...
end
else
begin
    RAISERROR ('User already exists. Pick another one.', -- Message text.
           16, -- Severity.
           1 -- State.
           );
end
如果由于用户名已存在而导致插入失败,则只需在客户端输出
异常
消息即可

try
{
    //do the insert
}
catch(Exception ex)
{ 
   //Output ex.Message, it will say: User already exists. Pick another one.
}

更好的方法是在用户名上创建唯一索引,并让它因违反唯一约束而失败。您只需捕获异常并显示一条用户友好的消息。

如果要在尝试插入之前检查该值是否存在,可以编写如下SQL语句:

if not exists (select 1 from table where username=@username)
begin
     insert into ...
end
else
begin
    RAISERROR ('User already exists. Pick another one.', -- Message text.
           16, -- Severity.
           1 -- State.
           );
end
如果由于用户名已存在而导致插入失败,则只需在客户端输出
异常
消息即可

try
{
    //do the insert
}
catch(Exception ex)
{ 
   //Output ex.Message, it will say: User already exists. Pick another one.
}

更好的方法是在用户名上创建唯一索引,并让它因违反唯一约束而失败。您只需捕获异常并显示一条用户友好的消息。

您不想将用户名作为主键。但是,您应该将其设置为唯一索引

无论如何,这很简单:

SELECT COUNT(*) from UserTable where Username = 'Peter'

。。。如果用户名存在,将返回1(或更多),如果不存在,将返回0。

您不想将用户名作为主键。但是,您应该将其设置为唯一索引

无论如何,这很简单:

SELECT COUNT(*) from UserTable where Username = 'Peter'
。。。如果用户名存在,则返回1(或更多);如果用户名不存在,则返回0。

您可以使用

 `IF Exists(Select Top 1 * from Users where username = @userNameYouWantToCheck)
  Begin
... Do what you want to do if it exists...   
  END

 ELSE      
 BEGIN     
   ...catch error here...     
 END
`您可以使用

 `IF Exists(Select Top 1 * from Users where username = @userNameYouWantToCheck)
  Begin
... Do what you want to do if it exists...   
  END

 ELSE      
 BEGIN     
   ...catch error here...     
 END

`

不要将密码存储在普通文本中HM我理解,但这些答案似乎不适用于c#?不要将密码存储在普通文本中HM我理解,但这些答案似乎不适用于c#?