简体   繁体   English

Access数据库C#查询问题

[英]Access Database c# query Problems

Here is the background of my problem: I have a combobox that when users start typing, it should retrieve suggested items from a column in database table. 这是我遇到问题的背景:我有一个组合框,当用户开始键入内容时,它应该从数据库表的列中检索建议的项目。 The user starts inputing name and the program should suggest names by looking at both first and last names (database has separate tables for both ) 用户开始输入名称,程序应通过同时查看名字和姓氏来建议名称(数据库有两个单独的表)

Here is the code that I had: 这是我的代码:

        try{
            String temp = nameCBox.Text;
            AutoCompleteStringCollection namesSuggestion = new AutoCompleteStringCollection();
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=C:\\LogEntry\\LogEntry.accdb; Persist Security Info = False;");
            OleDbDataReader reader;
            conn.Open();
            String text2send = "Select Name from [Teachers] where FName like '" + temp + "' OR LName like '" + temp + "' Group by [Name]";
            OleDbCommand cmd = new OleDbCommand(text2send, conn);
            reader = cmd.ExecuteReader();
            if (reader.HasRows == true)
            {
                while (reader.Read())
                    namesSuggestion.Add(reader["temp"].ToString());
            }
            reader.Close();
            nameCBox.AutoCompleteCustomSource = namesSuggestion;

            conn.Close();
            }

errors: 1) I see no suggestions in the combo box 2) When I type in the combo box, it highlights the text and when I type something else again, it will write on the previous typed character. 错误:1)我在组合框中看不到任何建议2)当我在组合框中键入内容时,它会突出显示文本,而当我再次键入其他内容时,它将写在先前键入的字符上。

Please Help desktopmaker 请帮助桌面制作者

Since you are using Like operator, try to make the input between look like this : %input% To get something like : 由于您使用的是Like运算符,因此请尝试使输入之间看起来像这样: %input%要获得类似的信息:

Take a look at this , it may help 看看这个 ,可能会有所帮助

var sql = String.Format("Select Name from [Teachers] WHERE FName Like '%{0}%' OR LName Like '%{0}%'", temp);

Other points may be helpful about your current code : 其他几点可能对您当前的代码有所帮助:

  • Use the using statement in your code, it dispose the resources, and for the Connection, it close it. 在您的代码中使用using语句,它处理资源,对于Connection,它关闭它。

     using(var conn = new OleDbConnection("ConnectionStrong")) { //code } 
  • The best, is to use a parameterized query Link 最好的方法是使用参数化查询链接

Erm m

What's this doing 这是在做什么

namesSuggestion.Add(reader["temp"].ToString()); 

reader is returning a column called Name.. 读取器将返回一个名为Name的列。

I wouldunless the user is typing it in expect the search string to contain a wild card. 除非用户输入它,否则我期望搜索字符串包含通配符。

Eg Like 'John%' , or Like '%Smith' in standard sql, access uses * instead of % I seem to remember. 例如,在标准sql中, Like 'John%'Like '%Smith' ,访问使用*代替我似乎记得的%。

Oh and presumably you aren't worried about sql injection attacks?? 哦,大概您不担心sql注入攻击吗??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM