【问题标题】:How to check if a value already exists in my database and show a validation message如何检查数据库中是否已存在值并显示验证消息
【发布时间】:2014-04-22 00:35:41
【问题描述】:

我有下面的代码,它连接到一个 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();

如何检查在“txtEmail”文本框中输入的电子邮件是否已存在于我的数据库中,在电子邮件列中,然后警告消息说电子邮件已经存在,因此它不会插入到我的数据库中?

【问题讨论】:

  • 调用存储过程而不是直接插入数据库。在过程中进行测试以查看电子邮件地址是否已经存在,如果存在/不存在则返回一个值,以及插入是否正常。
  • 并且,将连接的打开放入 try 语句中,并在 finally 中关闭连接。

标签: c# asp.net sql ado.net


【解决方案1】:

这对我有用:

Create a function Called CheckMail(string email)

public bool CheckMail(string email)

        {
            SqlConnection con = new SqlConnection("Data Source=*******; Initial Catalog=Your Database Name; Persist Security Info=True;User ID=****; Password=******");

                SqlCommand cmd = new SqlCommand("select email from  Table Name where email='"+email+ "'",con);

                con.Open();

            SqlDataReader sdr = cmd.ExecuteReader();

            if (sdr.Read())
            {
                return false;
            }
            else
            {
                return true;
            }            
        }

 

Then Implement in Button Click as 

Pass Textbox value in function that were created..

 

if (CheckMail(EmailTxt.Text))  
                {

Write Your insert code to database 

}

else

{

Error Message or Alert to Show Already Exists in database

}

【讨论】:

    【解决方案2】:

    在 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
    

    这是一个示例程序。如果@isExist=1 表示该值存在。否则不存在。创建一个方法来调用此过程并继续... 快乐编码

    【讨论】:

      【解决方案3】:

      在需要的文本框或区域调用此方法

      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;
                  }
              }
      
          }
      

      【讨论】:

        【解决方案4】:

        调用存储过程,在存储过程中你可以检查 插入前

         IF NOT EXISTS(SELECT * FROM EmailSignUp WHERE Email =@email)   
         Begin
        insert query here
         end
        

        您也可以在文本更改事件中检查它

        【讨论】:

          【解决方案5】:

          试试这个

          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";
          

          【讨论】:

            猜你喜欢
            • 2016-12-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-15
            • 2018-03-14
            • 2021-11-06
            • 2011-05-16
            • 2021-10-10
            相关资源
            最近更新 更多