I have task involving reading SAS .xpt files using .NET. For that I'm using sas.LocalProvider
, which allow me to read .xpt files. The only problem is that I need to provide table name from which I want to get data. That approach is not acceptable because table name can vary (although only one will exists in file) and user will have no knowledge about that name. Is they a way to read available tables in .xpt file? Or get the default, first table? I tried to use OleDbConnection.GetSchema()
but that gave me no information about tables. Below I pasted sample code that I'm using:
var cn = new OleDbConnection();
cn.ConnectionString = @"Provider=sas.LocalProvider; Data Source=test.xpt; SAS File Format=XPT";
cn.Open();
var cmd = cn.CreateCommand();
cmd.CommandType = CommandType.;
cmd.CommandText = "SAS";
var oleDa = new OleDbDataAdapter(cmd);
var ds = new DataSet();
oleDa.Fill(ds, "SAS");
Ok, I got it work. Because SAS xpt file header is constant i was able to read table name from it using binary reader. Each header record have 80bytes. The table name is in 6 record (counting header labels records) , preceaded by 8 bytes. So the first table name is located at bytes 408-415 (8bytes long). This little piece of code will get you first table name:
var streamReader = new FileStream(filePath, FileMode.Open);
var buffer = new byte[8];
streamReader.Position = 408;
streamReader.Read(buffer, 0, 8);
streamReader.Close();
string firstTableName = Encoding.UTF8.GetString(buffer, 0, buffer.Length);