读取访问数据库

时间:2016-08-02 14:10:17

标签: c# ms-access oledb oledbdatareader

我编写了基于搜索框返回值的代码,但有时,输入搜索框的值可以应用于MSAccess DB中的多个记录。

有没有办法可以使用按钮滚动浏览应用于搜索框中值的记录?

    public void LoopThroughRecs(OleDbDataReader Data)
    {
        Data.Read();
        {
            FirstName.Text = Data["Initial"].ToString();
            LastName.Text = Data["Surname"].ToString();
            Address1.Text = Data["Address 1"].ToString();
            Address2.Text = Data["Address 2"].ToString();
            Address3.Text = Data["Address 3"].ToString();
            TownCity.Text = Data["Post Town"].ToString();
            PostCode.Text = Data["Post Code"].ToString();
            Telephone.Text = Data["Telephone"].ToString();
            LstSvcDat.Text = Data["LastService"].ToString();
            BoilerMan.Text = Data["Manufacturer"].ToString();
            BoilerMod.Text = Data["Model"].ToString();
        }
        Data.Close();
    }

    public void button2_Click(object sender, EventArgs e)
    {

        System.Data.OleDb.OleDbConnection conn = new
        System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb";
        try
        {
            conn.Open();
            OleDbCommand command = new OleDbCommand("SELECT CustCode,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Equipment.CustID AS CustID,Equipment.Manufacturer AS Manufacturer,Equipment.Model AS Model,Equipment.LastService AS LastService FROM Contacts LEFT OUTER JOIN Equipment ON Equipment.CustID = Contacts.CustID WHERE Archived = 0 AND (CustCode = '" + textBox12.Text + "' OR Initial = '" + textBox12.Text + "' OR Surname = '" + textBox12.Text + "' OR Initial = '" + textBox12.Text + "' OR [Post Town] = '" + textBox12.Text + "' OR [Post Code] = '" + textBox12 + "')", conn);
            OleDbDataReader Data = command.ExecuteReader();

            LoopThroughRecs(Data);

        }
        finally
        {
            conn.Close();
        }
    }

0 个答案:

没有答案