fpuglap
fpuglap

Reputation: 119

Get first table with GetSchema

I'm trying to get the first sheet of an Excel File:

try
{
    string constring = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filepath + ";Extended Properties=Excel 12.0;";
    OleDbConnection conn = new OleDbConnection(constring);
    conn.Open();
    DataTable myTables = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    var myTableName = conn.GetSchema("Tables").Rows[0].Table;
    string sqlquery = string.Format("SELECT * FROM [{0}]", myTableName);
    OleDbDataAdapter da = new OleDbDataAdapter(sqlquery, conn);
    da.Fill(dt); 
    MessageBox.Show(Convert.ToString(dt.Rows.Count));
    return dt;
}
catch (Exception e)
{
    MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, 
    MessageBoxImage.Error);
    return dt;
}

What am I doing wrong?

EDIT:

The following error is show up: The Microsoft Office Access database engine could not find the 'Tables' object.

This works with Visual Basic:

Try
    Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" &
                                        "Data Source=" & filepath &
                                        ";Extended Properties=Excel 12.0;"
    Dim conn As New OleDbConnection(constring)
    conn.Open()
    Dim myTables As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
    Dim myTableName = conn.GetSchema("Tables").Rows(0)("TABLE_NAME")
    Dim sqlquery As String = String.Format("SELECT * FROM [{0}]", myTableName)
    Dim da As New OleDbDataAdapter(sqlquery, conn)
    da.Fill(dt)
    conn.Close()
    MsgBox(dt.Rows.Count)
    Return dt
Catch ex As Exception
    MsgBox(Err.Description, MsgBoxStyle.Critical)
    Return dt
End Try

I'm trying to replicate it with C#.

The error line seems to be this one:

var myTableName = conn.GetSchema("Tables").Rows[0].Table;

It tries to replicate the following line in VB code:

Dim myTableName = conn.GetSchema("Tables").Rows(0)("TABLE_NAME")

Upvotes: 0

Views: 1520

Answers (1)

Mary
Mary

Reputation: 15091

Here it is all put together. The @ sign preceding the file path string take the string and does not use the \ as an escape. In the interpolated string we do want the escape character for the embedded double quotes. The using block ensures that your connection is closed and disposed. I displayed the result in a combo box.

 private void opCode()
    {
        string filePath = @"C:\Users\xxx\Documents\Excel\TestImport.xlsx";
        string conString = $"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {filePath} ; Extended Properties = \"Excel 12.0; HDR = YES;\"";
        Debug.Print(conString);
        DataTable dt;
        using (OleDbConnection cn = new OleDbConnection(conString))
        {
            cn.Open();
            dt = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        }
        var exSheets = (from dRow in dt.AsEnumerable()
                        select dRow["TABLE_Name"]).ToArray();
        cbo1.Items.AddRange(exSheets);
    }

****NOTE: The Linq query does not necessarily return the sheets in the order that appear in the workbook

Upvotes: 1

Related Questions