如何验证我的sql数据库中是否已存在名字和姓氏 [英] how to validate if the first name and last name is already exist in my sql database
本文介绍了如何验证我的sql数据库中是否已存在名字和姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何验证我的sql数据库中是否已存在名字和姓氏我在temp = Convert.ToInt32(com.ExecuteScalar()。ToString())中有错误;
how to validate if the first name and last name is already exist in my sql database i have error in temp = Convert.ToInt32(com.ExecuteScalar().ToString());
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["registrationConnectionString"].ConnectionString);
conn.Open();
string check = "select count(*) from userdata where FirstName,LastName='" + TextBoxFN.Text + TextBoxFN.Text +"'";
SqlCommand com = new SqlCommand(check, conn);
com.Parameters.AddWithValue("@FN", TextBoxFN.Text);
com.Parameters.AddWithValue("@LN", TextBoxLN.Text);
temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Label1.Text = "Student Already Exist";
}
推荐答案
用
Replace this line with
string check = "select count(*) from userdata where FirstName,LastName='" + TextBoxFN.Text + TextBoxFN.Text +"'";
这个
With this
string check = string.formt("select count(*) from userdata where FirstName='{0}' AND LastName='{1}'", TextBoxFN.Text, TextBoxLN.Text);
您不需要使用参数化查询,因为您的查询已经包含值。所以让你的查询如下,然后运行它。
You need not to use parameterized query since your query already contains values. so make your query as follows and then run it.
conn.Open();
string inputQuery= "select count(*) from userdata where FirstName='" + TextBoxFN.Text + "' And LastName='" + TextBoxFN.Text +"'";
SqlCommand com = New SqlCommand(inputQuery, conn);
temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp >= 1) '<--- updated here
{
Label1.Text = "Student Already Exist";
}
否则你可以使用参数化查询:
or else you can use parameterized query as :
string check = "select count(*) from userdata where FirstName='{FN}' AND LastName='{LN}'"
SqlCommand com = new SqlCommand(check, conn);
com.Parameters.AddWithValue("@FN", TextBoxFN.Text);
com.Parameters.AddWithValue("@LN", TextBoxLN.Text);
temp = Convert.ToInt32(com.ExecuteScalar().ToString());
这篇关于如何验证我的sql数据库中是否已存在名字和姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文