在网络中使用C#编程从Access数据库中检索记录的最佳方法? [英] The best way to retrieve records from an Access database using C # programming within a network?

查看:94
本文介绍了在网络中使用C#编程从Access数据库中检索记录的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i用c#编写程序,数据库就是Access,现在我的Access'数据库在其他计算机上有很多数据(我使用网络服务器),我创建一个文本框从数据库中搜索,例如在'Google.com'上搜索;

当我执行搜索时出现问题我输入A然后我的代码转到数据库(服务器)并查找所有记录我接下来输入字符A我输入B现在我的文本Box是AB然后我的代码再次转到数据库并找到所有具有字符AB的记录下一步我输入c去数据库和返回。 ...一次又一次地往返,每次搜索大量记录都是坏的,现在我想解决这个问题,但我现在不怎么样?



查看此图片和我的搜索代码

图片: [ ^ ]



我的代码:

  private   void  textBox1_TextChanged( object  sender,EventArgs e)
{
try
{
Project par = new Project();
DataTable tabl = par.SearchProjeh(txtFamilSearch.Text.Trim());
if (tabl!= null && tabl.Rows.Count > 0
{
dgvSearch.DataSource = tabl;
dgvSearch.Visible = true ;


}
else
{
dgvSearch.DataSource = ; dgvSearch.Visible = false ;
}
}
catch (例外)
{

;
}
}
************************************ *****

public DataTable SearchProjeh( string Famil)
{
DataTable table = new DataTable();
OleDbCommand command = new OleDbCommand();
尝试
{
command.Connection = Connect;
command.CommandText = SELECT tblProjeh.CodeProj,tblProjeh.fldFamil,tblProjeh.fldName FROM tblProjeh WHERE fldFamil like '% + Famil + %';;
command.CommandType = CommandType.Text;
if (command.Connection.State!= ConnectionState.Open)command.Connection.Open();
OleDbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
table.Load(读者);
}
catch (例外)
{

;
}
return 表;
}

解决方案

而不是使用 TextChanged 事件尝试使用 textBox1_Validating 事件。当用户完成输入并尝试离开文本框时,它将触发。这样你只需要在数据库中搜索一次输入的整个字符串



ie在文本框的IDE Properties窗口中单击Lightening bolt图标以获取文本框的可能事件列表。



向下滚动列表,直到找到验证并双击它。



将当前在 textBox1_TextChanged 事件中的代码移动到新事件 textBox1_Validating 事件



你应该得到类似这样的东西

 private void textBox1_Validating(object sender,CancelEventArgs e)
{
Project par = new Project();
DataTable tabl = par.SearchProjeh(txtFamilSearch.Text.Trim());
if(tabl!= null&& tabl.Rows.Count> 0)
{
dgvSearch.DataSource = tabl;
dgvSearch.Visible = true;


}
else
{
dgvSearch.DataSource = null; dgvSearch.Visible = false;
}
}

private void textBox1_TextChanged(object sender,EventArgs e)
{

}

注意我已经删除了Try-catch块...如果你不打算对异常做些什么,那么就不要抓住它......很容易吞下异常或丢失有关它们的有价值的信息。


hi i write a program with c# and the Data Base for that is Access , now My Access' Data Base is on other computer with a lot of Data (i use Network with Server ) , i Create a textbox for search from Data Base like Search on 'Google.com';
there is a problem when i do Search for example i type "A" then my code go to database (Server) and find all record that have the character "A" next i type "B" now my text Box is "AB" then again my code go to Data Base and find all record that have the character "AB" next i type "c" go Database and Back .... Go and Back again and again , For each time a large number of records Searched it is Bad , now i want to solved this problem but i don't now How ?

see this image and my Code For Search
image : [^]

my Code :

 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Project par = new Project();
                DataTable tabl = par.SearchProjeh(txtFamilSearch.Text.Trim());
                if (tabl != null && tabl.Rows.Count > 0)
                {
                    dgvSearch.DataSource = tabl;
                    dgvSearch.Visible = true;
                    
                    
                }
                else
                {
                    dgvSearch.DataSource = null; dgvSearch.Visible = false;
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
*****************************************

 public DataTable SearchProjeh(string Famil)
        {
            DataTable table = new DataTable();
            OleDbCommand command = new OleDbCommand();
            try
            {
                command.Connection = Connect;
                command.CommandText = "SELECT tblProjeh.CodeProj, tblProjeh.fldFamil, tblProjeh.fldName FROM tblProjeh WHERE  fldFamil like '%" + Famil + "%';";
                command.CommandType = CommandType.Text;
                if (command.Connection.State != ConnectionState.Open) command.Connection.Open();
                OleDbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                table.Load(reader);
            }
            catch (Exception)
            {

                throw;
            }
            return table;
        }

解决方案

Instead of using the TextChanged event try using the textBox1_Validating event. It will fire when the user has finished typing and tries to move away from the text box. That way you will only be searching the database once for the whole string entered

i.e. In the IDE Properties window for the textbox click on the "Lightening bolt" icon to get a list of possible events for the textbox.

Scroll down the list until you find Validating and double-click it.

Move the code currently in your textBox1_TextChanged event into the new event textBox1_Validating event

You should end up with something like this

private void textBox1_Validating(object sender, CancelEventArgs e)
{
    Project par = new Project();
    DataTable tabl = par.SearchProjeh(txtFamilSearch.Text.Trim());
    if (tabl != null && tabl.Rows.Count > 0)
    {
        dgvSearch.DataSource = tabl;
        dgvSearch.Visible = true;


    }
    else
    {
        dgvSearch.DataSource = null; dgvSearch.Visible = false;
    }
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

NOTE I have removed the Try-catch block ... if you are not going to do something with an exception then don't catch it... it's very easy to "swallow" exceptions or lose valuable information about them.


这篇关于在网络中使用C#编程从Access数据库中检索记录的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆