- Home /
ODBCConnection.GetSchema make unity crash
Hi everyone,
For some days now, I try to connect an Excel file to a custom AssetImporter with an ODBCConnection. It's work pretty well. I am able to read it and parse the cells.
But I cannot get the list of the sheet name...
Along my research on this site or google, I found that "ODBCConnection.GetSchema("Tables")" will normally do the job... but as soon as I add this line to my code, Unity just crash... and I found no way to get the exception or any information back...
Has anybody already tryed this or get the same bug ? Any advices ?
Thanks in advance for all ! Thor
P.S. Hope my English is at least understandable... I really need some more practice... sorry... Don't hesitate to ask when any part is unclear ! P.P.S. Code source is not really relevant in this case but... sometimes it can help ^^
As Unity doesn't include the System.Data, I have create a new dll which I have put in my plugin folder. Here is the code of the unique class of the Dll.
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
public class LocalisationExcelImporter : UnityEditor.AssetPostprocessor
{
private static string excelFileExtension = ".xls";
private static string extendedExcelFileExtension = ".xlsx";
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string assetPath in importedAssets)
{
if (assetPath.ToLower().EndsWith(LocalisationExcelImporter.excelFileExtension) ||
assetPath.ToLower().EndsWith(LocalisationExcelImporter.extendedExcelFileExtension))
{
LocalisationExcelImporter.ReadXls(assetPath);
}
}
}
private static void ReadXls(string assetPath)
{
string applicationPath = System.IO.Path.GetDirectoryName(UnityEngine.Application.dataPath);
string path = applicationPath + "/" + assetPath;
try
{
string connectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + path + ";";
OdbcConnection odbcConnection = new OdbcConnection(connectionString);
odbcConnection.Open();
// ------> As soon as I add this line... it's crash... <------
DataTable sheettable = odbcConnection.GetSchema("Tables");
////foreach(DataRow row in sheettable.Rows)
////{
//// UnityEngine.Debug.Log("sheet name : " + row["TABLE_NAME"].ToString());
////}
string tableName = "Feuil1";
string query = "SELECT * FROM [" + tableName + "$]";
OdbcCommand odbcCommand = new OdbcCommand(query, odbcConnection);
OdbcDataReader odbcReader = odbcCommand.ExecuteReader();
DataTable datatable = new DataTable("copy");
datatable.Load(odbcReader);
odbcReader.Close();
odbcConnection.Close();
// DO SOMETHING with the file... Which works when I doesn't try to get the sheets names.
}
catch (System.Exception e)
{
UnityEngine.Debug.LogError("[LocalisationExcelImporter] : caught exception: " + e.ToString() + ", " + e.Message + "\n\n" + e.StackTrace);
}
}
}
Your answer
Follow this Question
Related Questions
can i retrieve excel datas in unity 5 Answers
"Load Data from Excel with Odbc" doesn't work in Unity 5 64bit 3 Answers
Retrieving metadata from an excel file 1 Answer
How can I get data from an outside database (specifically excel). 2 Answers
Can I use the Excel Object Library in a C# script in Unity? 1 Answer