I want to check if an equal or a similar value exists in database. I have build this code:
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = ConfigurationManager.ConnectionStrings["ChipstarALConn"].ToString();
string sql1 = "select Count(*) from SMS_KOD where KOD = @name";
SqlCommand cmd1 = new SqlCommand(sql1, con1);
cmd1.Parameters.AddWithValue("@name", TextBox1.Text);
con1.Open();
int result = (int)cmd1.ExecuteScalar();
if (result > 0)
{
Lab0.Text = "true";
}
else
{
Lab0.Text = "false";
}
And it returns true
. Now I want to return true
if a string
entered on TextBox
is entered wrong but similar.
For example if a value in Database is ASDFG
and I put on TextBox
ASDFH
or AXDFG
it must also return true
.
You could use a Levenshtein distance algorithm in T-SQL. For example (from here):
Now something like this works:
You can use a
SqlDataAdapter
to fill aDataTable
. If it contains rows there are at least similarKod
s.Sql-Fiddle
Here is a possible implementation: