1

I´m starting to develop in C# and SQL Server, I don´t know how to extract information from one excel specific colum.

I have this code working, but what i need it´s to compare a textbox with a specific column and get the data:

Example

Select * 
From T_Empleado 
Where "Specific column" = "textbox".

public void mostrarExcel()
{
    String name = "Sheet1";
    String constr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + "C:\\Users\\alegriad\\Desktop\\sample\\Book2.xlsx" + "; Extended Properties='Excel 12.0 XML;HDR=YES;';";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]'", con);
    con.Open();

    OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
    DataTable data = new DataTable();
    sda.Fill(data);
    dgv_Reporte.DataSource = data;
}//mostrarExcel

Thank you.

12
  • How does this related to SQL Server? tutorialspoint.com/sql Commented Dec 19, 2016 at 21:45
  • Because you have to make a select the information inside of the excel file and then look where´s equal to a specific field in a data base in SQL Server Commented Dec 19, 2016 at 22:08
  • Do you want to read column value in each row? Commented Dec 19, 2016 at 22:10
  • Yes, that´s the point Commented Dec 19, 2016 at 22:11
  • I have to compare each row with a textbor Commented Dec 19, 2016 at 22:15

1 Answer 1

1

You can write your query like this

 OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$] where columnName = '"+ YourTextboxValue+ "'" , con);

I try with sample excel like below

enter image description here

And my query like this

 OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$] WHERE Name = 'T1'", con);

This works for me.

Sign up to request clarification or add additional context in comments.

2 Comments

columName it´s the header of the column in the excel file, already try that way but didin´t work either :D
I try it again, and that work, THANK YOU SO MUCH :DD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.