Forums - C# Corner

Forum guidelines
Subin Thomas

Subin Thomas

  • 562
  • 2.7k
  • 28.3k

how to find the database name from code ?

Feb 27 2019 12:37 AM
i have one code and i have to find the table from which the data have been retrieved here is the code
  1. private static DataSet ImportExcelXLS(string FileName, bool hasHeaders)  
  2.         {  
  3.             string HDR = hasHeaders ? "Yes" : "No";  
  4.             string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1\"";  
  5.   
  6.             DataSet output = new DataSet();  
  7.   
  8.             using (OleDbConnection conn = new OleDbConnection(strConn))  
  9.             {  
  10.                 conn.Open();  
  11.   
  12.                 DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });  
  13.   
  14.                 foreach (DataRow row in dt.Rows)  
  15.                 {  
  16.                     string sheet = row["TABLE_NAME"].ToString();  
  17.   
  18.                     OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);  
  19.                     cmd.CommandType = CommandType.Text;  
  20.   
  21.                     DataTable outputTable = new DataTable(sheet);  
  22.                     output.Tables.Add(outputTable);  
  23.                     new OleDbDataAdapter(cmd).Fill(outputTable);  
  24.                 }  
  25.             }  
  26.             return output;  
  27.         } 
 

Answers (9)