且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查数据库中是否已存在用户数据

更新时间:2023-11-30 21:58:58

我建议您先在INSERT语句之前进行SELECT查询.另外,您只能将电子邮件用作SELECT语句上的参数,而不能包含姓名,因为人们可能会使用相同的姓名.

I would suggest that you have a SELECT query first before INSERT statement. Also you can only use email as parameter on your SELECT statement and not include name since there are chance that persons have the same name.

string query = "SELECT COUNT(ID) FROM tblUsers WHERE email = @email
int count = db.ExecuteReader(query);    


if (count > 0){
   return "email is already in use";
} else {
   //Call the insert statement here with try catch inside. In this way you can handle if error occur on the execution itself.
}