如何创建像“google search engin”这样的搜索引擎使用C#和MSSQL?

问题描述:

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();





这只是工作过滤数据库



i需要像谷歌搜索引擎我输入一个意味着下来开始所有a字



this is working filtering database only

i need like google search engine i type a means coming down starting all "a" words

对于初学者,不要那样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。



其次我建议您查看全文文本搜索 [ ^ ]而不仅仅是使用LIKE - 您现有的代码想要在字符串的开头完全匹配,而不是像Google那样的任何东西。



全文搜索不会给你谷歌 - 但他们在搜索系统中花了大量的工时,所以你不太可能在合理的时间内复制它! :笑:

虽然它比LIKE好。
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.


至于如何创建搜索引擎,答案是:通过做适当的软件开发工作。这一切都取决于你想要搜索的地方。如果您需要在Web或文件集上进行搜索,为什么要使用关系数据库? :-)



只需一件事:想象一下,你已经拥有了具有谷歌软件所有功能的搜索引擎。您是否可以在http:/www.google.com上执行与Google相同的搜索?没有!这是因为您没有Google数据。谷歌收集并支持从网络收集的大量散列数据,第二个网络。您只能通过Google网站访问此数据。



关于您使用SQL的方式...



从一开始你的方法就错了。通过串联从UI获取的字符串组成的查询。不仅重复的字符串连接是低效的(因为字符串是不可变的;我是否必须解释为什么它会使重复连接变坏?),但是有更重要的问题:它打开了通向良好的大门已知的漏洞称为 SQL注入



这是它的工作原理: http://xkcd.com/327



你明白了吗?从控件中获取的字符串可以是任何东西,包括......一段SQL代码。



怎么办?只需阅读有关此问题和主要补救措施:参数化语句 http://en.wikipedia.org/ wiki / SQL_injection



使用ADO.NET,使用:http://msdn.microsoft.com/en-us/library/ff648339.aspx



请参阅我过去的答案有更多细节:

在com.ExecuteNonQuery中更新EROR( );

嗨姓名不显示?



-SA
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


你所描述的不是谷歌般的搜索引擎。你得到的是一个提前输入的意见箱。



你的实现只支持一个单词,并且只有当这个人输入单词的开头并得到准确的拼写。



首先要做的事情。 Google针对SQL注入攻击找出了为什么编写SQL查询的原因非常糟糕,以至于您有可能破坏数据库。然后谷歌为C#paramterized sql查询提供了解决方法。
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.