gpt4 book ai didi

c# - 如何检查我的数据库中是否已存在一个值并显示验证消息

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:06 24 4
gpt4 key购买 nike

我有下面的代码,它连接到一个 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”文本框中输入的电子邮件是否已经存在于我的数据库中,在电子邮件列中,然后警告消息说电子邮件已经存在,所以它不会被插入到我的数据库中?

最佳答案

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

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

}

关于c# - 如何检查我的数据库中是否已存在一个值并显示验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22443634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com