Search code examples
c#exceloledbdatareader

Getting values from OleDbDataReader


I would like to know if it is possible to retrieve a specific value from OleDbdataReader.

For example, I import data from excel sheet which has 100 rows and 10 columns. Then I want the reader to get values from all the columns at specified row. Let's say I have a textbox, put random number into it(85), the reader finds this number in the first column and reads all the column values accordingly.

And, yes, if it is possible, could you please provide some guidance on how to do that.

So far I have only managed to get the reader to read all the values.

The main idea is that by changing value in textbox, reader would apply different set of values into method. Link to the picture from excel sheet to make it more clear:

https://imagizer.imageshack.us/v2/505x318q90/743/YIVGVf.jpg

    private void button1_Click(object sender, EventArgs e)
    {
        Model ThisModel = new Model();
        Beam ThisBeam = new Beam();

        OleDbConnection conn = new OleDbConnection();
        const string excelConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=@1;";
        const string connStringExtras = @"Extended Properties=""Excel 8.0;HDR=Yes;Imex=1""";
        conn.ConnectionString = excelConnString.Replace("@1", "C:\\Users\\Andrejs\\Desktop\\API2.xls;") + connStringExtras;

        try
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT A, B, C, D, E, F "+
                "FROM [SHEET1$]", conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                double X = (double)reader.GetValue(1);
                double Y = (double)reader.GetValue(2);
                CreatePadFooting(X, Y);
            }
        }
            catch
            {
            }

        ThisModel.CommitChanges();
    }
    private static void CreatePadFooting(double X, double Y)
    {
        Beam PadFooting1 = new Beam();

        PadFooting1.Name = "FOOTING";
        PadFooting1.Profile.ProfileString = "200*200";
        PadFooting1.Material.MaterialString = "K30-2";
        PadFooting1.Class = "8";
        PadFooting1.StartPoint.X = X;
        PadFooting1.StartPoint.Y = Y;
        PadFooting1.StartPoint.Z = 0.0;
        PadFooting1.EndPoint.X = X;
        PadFooting1.EndPoint.Y = Y;
        PadFooting1.EndPoint.Z = -500.0;
        PadFooting1.Position.Rotation = Position.RotationEnum.FRONT;
        PadFooting1.Position.Plane = Position.PlaneEnum.MIDDLE;
        PadFooting1.Position.Depth = Position.DepthEnum.MIDDLE;

        PadFooting1.Insert();
    }
    }
}

Solution

  • You need an If statement to compare the value of first column and then read from Reader. Insert an If statement like this:

    double randomValue = Convert.ToDouble(txtBoxRandomValue.Text); //get value to match and convert to double
    
    try
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT A, B, C, D, E, F "+
                "FROM [SHEET1$]", conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
    
            if (randomValue == Convert.ToDouble(reader.GetValue(0))) //checking here the first column
             {
                double X = (double)reader.GetValue(1);
                double Y = (double)reader.GetValue(2);
                CreatePadFooting(X, Y);
             }
           }
        }
            catch
            {
            }