Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
C#
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Sawdusts where Name like('" + textBox1.Text + "%')";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView2.DataSource = dt;
            con.Close();


this is working filtering database only

i need like google search engine i type a means coming down starting all "a" words
Posted
Updated 8-Mar-21 5:02am
v2
Comments
Simon_Whale 22-Dec-15 11:53am    
is it a winform? is it a webpage? is it WPF? please we need more information to help you as what you are after is more a visual problem than the code snippet that you supplied

For starters, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Second I'd suggest that you look at Full Text seraching[^] rather than just using LIKE - your existing code wants an exact match at the beginning of the string rather than anything Google-like.

Full text searching won't give you Google - but they have put a humongous amount of man hours into their search system, so it's unlikely that you will be able to duplicate it in a reasonable time period! :laugh:
It's better than LIKE though.
 
Share this answer
 
As to "how to create search engine", the answer would be: by doing appropriate software development work. It all depends where you want to do the search. If you need to search on the Web or set of files, why are you doing something with a relational database? :-)

Just one thing: imagine that you already have the search engine with all the feature of Google software. Will you be able to do the same search as Google at http:/www.google.com? No! This is because you don't have Google data. Google collects and support the a lot of hashed data collected from the Web, "the second Web". You have access to this data only through Google site.

As to the way you work with the SQL…

Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA
 
Share this answer
 
v2
What your describing is not a "Google like search engine". What you've got is a type-ahead suggestion box.

Your implementation will only support a single word and only if the person types the beginning of the word and gets the spelling exact.

First things first. Google for "SQL Injection Attack" to find out why how you've written your SQL query is so bad that you risk destroying your database. Then Google for "C# paramterized sql queries" for what to do about it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900