Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WiliLake · Oct 15, 2013 at 04:34 PM · c#excelodbc

Retrieving metadata from an excel file

I am using ODBC in a c# Unity script to access data in a .xsl file. The connection works and I can retrieve data from the file, but I am having trouble with its metadata. When I call the GetSchema(string) function, it gets stuck in a endless recursive call until it causes a stack overflow. This problem happens regardless of what particular schema I try to get.

Here is the code I am using:

 string connectionString = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq=" + file + ";UNICODESQL=1;Unicode=yes;";
     
 OdbcConnection dbCon = null;
 OdbcDataReader dbData = null;
 
 try
 {
   dbCon = new OdbcConnection(connectionString);
   Debug.Log(connectionString);
   
   dbCon.Open();

   DataTable sheets = dbCon.GetSchema(OdbcMetaDataCollectionNames.Tables);
   foreach(DataRow sheet in sheets.Rows)
   {
     string sheetName = sheet["TABLE_NAME"].ToString().Trim('\'').TrimEnd('$');    
     OdbcCommand dbCommand = new OdbcCommand("SELECT * FROM [" + sheetName + "$]", dbCon);
     DataTable data = new DataTable(sheetName);
     dbData = dbCommand.ExecuteReader();
     data.Load(dbData);
   }
 }
 catch (Exception ex)
 {
   if (ex != null)
   {
     Debug.LogError(ex.Message);
     Debug.LogError(ex.StackTrace);
   }
   else
     Debug.LogError("Exception raise loading '" + file + "'");
 }
 finally
 {
   if (dbData != null)
     dbData.Close();
   
   if (dbCon != null)
     dbCon.Close();
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rlyle1179 · Oct 15, 2014 at 03:09 PM 0
Share

I'm hitting this same issue, I'd like to know as well..

avatar image Oscar_HiS · Feb 24, 2015 at 09:54 AM 0
Share

I'm having this problem as well. Does anyone know of a solution?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Oscar_HiS · Mar 06, 2015 at 12:38 PM

Copied from my answer at stackoverflow (link http://stackoverflow.com/questions/19386012/retrieving-metadata-from-excel-in-unity/28718331#28718331)

I ran into this problem myself. It has to do with monodevelop's implementation of getschema(string str). It doesn't do what one would expect; it calls one of it's other implementations which then calls itself recursively (which is causing the stack overflow), casting exception if the connection has been closed, otherwhise calling itself. In other words it doesn't ever return what it says it's going to return, it calls itself until stackoverflow or the connection is closed (which probably won't happen since the close-command is usually called afterwards).

 public override DataTable GetSchema (string collectionName)
 {
     return GetSchema (collectionName, null);
 }
 public override DataTable GetSchema (string collectionName, string [] restrictionValues)
 {
     if (State == ConnectionState.Closed)
         throw ExceptionHelper.ConnectionClosed ();
     return GetSchema (collectionName, null);
 }

^ From documentation: https://github.com/mono/mono/blob/mono-4.0.0-branch/mcs/class/System.Data/System.Data.Odbc/OdbcConnection.cs

That is the reason for your error but I unfortunately don't have any real solution at this time but my workaround might be of use to someone. I want to get some sheetnames in my Excel-file so what I have done is add all sheetNames I want to use in a specific sheet, "Unity". I then read in that to get sheetnames then load them in one by one.

It's a very error-prone solution since it counts on a manual list of the sheetnames and that this list is correct but I'm pressed on time and have no other known alternative, but maybe it can be of help to someone.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I call an Office function from a C# script? 1 Answer

List export to Word or Excel 2 Answers

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges