C# 在Unity中从excel检索元数据
我在c#Unity脚本中使用ODBC访问.xsl文件中的数据。连接工作正常,我可以从文件中检索数据,但它的元数据有问题。当我调用GetSchema(string)函数时,它会陷入无休止的递归调用中,直到导致堆栈溢出。无论我试图获得什么样的模式,这个问题都会发生 以下是我正在使用的代码:C# 在Unity中从excel检索元数据,c#,excel,mono,odbc,unity3d,C#,Excel,Mono,Odbc,Unity3d,我在c#Unity脚本中使用ODBC访问.xsl文件中的数据。连接工作正常,我可以从文件中检索数据,但它的元数据有问题。当我调用GetSchema(string)函数时,它会陷入无休止的递归调用中,直到导致堆栈溢出。无论我试图获得什么样的模式,这个问题都会发生 以下是我正在使用的代码: string connectionString = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq=" + file + ";UNICODE
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();
}
}我自己也遇到了这个问题。它与monodevel的getschema(stringstr)实现有关。它没有达到人们期望的效果;它调用它的另一个实现,然后递归地调用自己(这导致堆栈溢出),如果连接已关闭,则抛出异常,否则调用自己。换句话说,它永远不会返回它说要返回的内容,它会在stackoverflow或连接关闭之前调用自己(这可能不会发生,因为关闭命令通常在之后调用) ^从文件: 这就是你犯错误的原因,但不幸的是,我目前没有任何真正的解决方案,但我的变通方法可能对某些人有用。我想在我的Excel文件中获得一些工作表名称,所以我所做的是添加我想在特定工作表“Unity”中使用的所有工作表名称。然后我把它读进去,得到图纸名称,然后把它们一个一个地载入
这是一个非常容易出错的解决方案,因为它依赖于一个手动的图纸名称列表,并且该列表是正确的,但我被及时催促,没有其他已知的替代方案,但可能对某人有所帮助。我不熟悉将XLS用作数据源,但您是否需要关闭连接?
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);
}