简体   繁体   English

如何检查数据库中是否已存在值并显示验证消息

[英]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.

相关问题 如何检查我的数据库中是否已经存在一个值并在ASP.NET和实体框架中显示验证消息 - How to check if a value already exists in my database and show a validation message in ASP.NET & Entity Framework 如果数据库中已存在值,如何显示消息? - How can I display a message if a value already exists in database? 如何检查Unity中的firebase数据库中是否已存在键/值 - How to check if a key/value already exists in a firebase database in Unity 如何检查文本框中的值在连接的数据库中是否已经存在 - How to check if a value in a textbox already exists in a connected database 如何检查值是否已经存在 - How to check if value already exists 如何检查我尝试插入数据库中的值已存在 - How to check value that I am trying to insert in database already exists 在使用IDataErrorInfo在MVVM中执行验证时,我应该在哪里执行检查以查看数据库中是否已存在值? - Where should I perform a check to see if a value already exists in the database when performing validation in MVVM using IDataErrorInfo? 检查值是否已经存在 - check if value already exists 如果文件名已经存在,则显示一条消息 - show a message if the filename already exists 如果不存在,则插入其他显示消息“已经存在” - If not exists then insert else show message “Already exists”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM