[英]How to check if a value already exists in my database and show a validation message
I have the below code, that connects to a Sql database and insert's data into a table : 我有以下代码,该代码连接到Sql数据库并将数据插入表中:
string firstNameV = txtFname.Text;
string surnameV = txtSname.Text;
string emailV = txtEmail.Text;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO EmailSignUp (Title,FirstName,Surname,Email,EstablishmentType,Interests) VALUES (@Title,@FirstName,@Surname,@Email,@EstablishmentType,@Interests)";
cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = title;
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = firstNameV;
cmd.Parameters.Add("@Surname", SqlDbType.NVarChar).Value = surnameV;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = emailV;
cmd.Parameters.Add("@EstablishmentType", SqlDbType.NVarChar).Value = eType;
cmd.Parameters.Add("@Interests", SqlDbType.NVarChar).Value = ins;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
How do I check if an email being entered in the "txtEmail" text box already exists in my database, in the email column and then alert message saying email already exists so it doesn't get inserted into my database? 我如何检查在“ txtEmail”文本框中输入的电子邮件是否已经存在于数据库中的“电子邮件”列中,然后发出警报消息说电子邮件已经存在,因此不会插入数据库中?
Call this method in required textbox or area 在所需的文本框或区域中调用此方法
public void EmailCheck()
{
string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("Select * from EmailSignUp where EmailId= @EmailId", con);
cmd.Parameters.AddWithValue("@EmailId", this.txtEmail.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true)
{
MessageBox.Show("EmailId = " + dr[5].ToString() + " Already exist");
txtEmail.Clear();
break;
}
}
}
Try this 尝试这个
cmd.CommandText = "IF NOT EXISTS(SELECT * FROM EmailSignUp WHERE Email = '"
+ txtEmail.Text + "')
BEGIN
INSERT INTO EmailSignUp (Title,FirstName,Surname,Email,EstablishmentType,Interests) VALUES (@Title,@FirstName,@Surname,@Email,@EstablishmentType,@Interests)
END";
Call a stored Procedure and inside the stored procedure you can check before insert 调用存储过程,您可以在插入之前检查存储过程内部
IF NOT EXISTS(SELECT * FROM EmailSignUp WHERE Email =@email)
Begin
insert query here
end
In another way you can check it in text changed event also 您还可以通过另一种方式在文本更改事件中检查它
Create a procedure on SQL server and check whether the name exists or not 在SQL Server上创建一个过程,并检查名称是否存在
CREATE PROCEDURE Procedure_Name
@mystring varchar(100),
@isExist bit out
AS
BEGIN
if exists(select column1 from tblTable1 where column1=@mystring)
begin
select @isExist=1
end
else
begin
select @isExist=0
end
END
GO
This is a sample procedure. 这是一个示例过程。 If @isExist=1 that means the value exist.otherwise not. 如果@ isExist = 1表示该值存在,否则不存在。 create a method to call this procedure and go on... Happy Coding 创建一个方法来调用此过程并继续进行...快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.