Sunday, June 19, 2011

Get Column Names And Data Types from the Database without reading the data



/***********************************************************************
/* Description: Extracting column details from the excel book is as good as reading the details from MS-Access MDB file. Following code demonstrates the procedure. Same procedure can be followed for MDB file also just by changing the connection string.
This code is compiled in VS2010, C#.
***********************************************************************/




OleDbConnection conn = new OleDbConnection("Provider= Microsoft.Jet. OLEDB.4.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 8.0");
string strSheetName = GetExcelSheetNames(txtFileName.Text)[0];
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new object[] { null, null, strSheetName, null });

OleDbType[] exColTypes = new OleDbType[dt.Rows.Count];
string[] exColNames = new string[dt.Rows.Count];

foreach (DataRow row in dt.Rows)
{
int nColIndex = Convert.ToInt32(row[6]);
exColTypes[nColIndex - 1] = (OleDbType)row[11];
exColNames[nColIndex - 1] = row[3].ToString();
}

conn.Close();





No comments:

Post a Comment