Daniel Alegría
Daniel Alegría

Reputation: 19

Select specific row in SQL Server

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.

Upvotes: 1

Views: 1339

Answers (1)

Seminda
Seminda

Reputation: 1773

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.

Upvotes: 1

Related Questions