帮助C#代码进行快速搜索

时间:2010-07-15 11:37:13

标签: c# winforms

我有这个代码可以快速搜索数据库。

它在sqlCE上运行良好。

现在我需要翻译它将适用于Access 2007

怎么做?

public bool LOOK()
        {
            Tmp = "";
            MAK = "";
            DES = "";
            Cmd.CommandType = CommandType.TableDirect;
            Cmd.CommandText = "BarcodeTbl";
            Cmd.IndexName = "Barcode";
            Cmd.SetRange(DbRangeOptions.Match, new object[] { txtYad.Text }, null);
            SqlCeDataReader read = Cmd.ExecuteReader();
            while (read.Read())
            {
                Tmp = read[2].ToString(); 
                MAK = read[0].ToString(); 
                DES = read[1].ToString();
            }
            read.Dispose();
            if (Tmp == "")
            {
                return false;
            }
            else
            {
                txtYad.Text = DES;
                return true;
            }
        }

提前谢谢

1 个答案:

答案 0 :(得分:2)

您需要使用System.Data.OleDb namespace中的类,例如OleDbDataReaderOleDbCommand等。

顺便说一句,以下内容:

SqlCeDataReader read = Cmd.ExecuteReader();
while (read.Read())
 {
     Tmp = read[2].ToString(); 
     MAK = read[0].ToString(); 
     DES = read[1].ToString();
 }
 read.Dispose();

可写得更好:

using(SqlCeDataReader read = Cmd.ExecuteReader())
{
    while (read.Read())
    {
        Tmp = read[2].ToString(); 
        MAK = read[0].ToString(); 
        DES = read[1].ToString();
     }
} // .Dispose() is called automatically here