0

After several attempts, I finally managed to use DataBase access in Unity with System.Data.Odbc.

I know the connection and the query works, because I can do this :

cmd = new OdbcCommand(req);
cmd.Connection = accessConnection;

OdbcDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
    Debug.Log("test: "+reader[0]); // This works fine
}

But when I try to use a DataTable, it does crash pretty badly.

I tried :

cmd = new OdbcCommand(req);
cmd.Connection = accessConnection;

OdbcDataReader reader = cmd.ExecuteReader();

output = new DataTable();
output.Load(reader); // This line crash somehow
reader.Close();

Here is the exception details I catched :

TargetSite: Int32 GetColumnAttribute(Int32, FieldIdentifier)

Source: System.Data

Message: Object reference not set to an instance of an object

StackTrace:   at System.Data.Odbc.OdbcDataReader.GetColumnAttribute (Int32 column, FieldIdentifier fieldId) [0x00000] in <filename unknown>:0 
  at System.Data.Odbc.OdbcDataReader.GetSchemaTable () [0x00000] in <filename unknown>:0 
  at System.Data.Common.DataAdapter.BuildSchema (IDataReader reader, System.Data.DataTable table, SchemaType schemaType, MissingSchemaAction missingSchAction, MissingMappingAction missingMapAction, System.Data.Common.DataTableMappingCollection dtMapping) [0x00000] in <filename unknown>:0 
  at System.Data.DataTable.Load (IDataReader reader, LoadOption loadOption) [0x00000] in <filename unknown>:0 
  at System.Data.DataTable.Load (IDataReader reader) [0x00000] in <filename unknown>:0 
  at DataBase.Execute (System.String req) [0x00063] in D:\Unity Project\Assets\DataBase.cs:131 
UnityEngine.Debug:LogError(Object)
DataBase:Execute(String) (at Assets/DataBase.cs:150)
ModelHandler:Start() (at Assets/ModelHandler.cs:34)

Does anyone have an idea to fix this ?

For information, I also tried with an OdbcDataAdapter, with the Fill method, an I've cached the same kind of issue.

Dean
  • 1,512
  • 13
  • 28
  • Related: [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül May 15 '15 at 14:42
  • Well, It was interesting. Unfortunately, all of my objects aren't null when I test them, and the error is throwed by code I did not write (in `System.Data` and I can't figure it out why there is this error yet) I did understand that there was a null reference problem, I just can figure it out what to do to prevent it in that particular scenario – Dean May 15 '15 at 14:56
  • i had EXACTLY the same problem. have a look at my question / answer: http://answers.unity3d.com/questions/923013/load-data-from-excel-with-odbc-doesnt-work-in-unit.html – Jinjinov May 15 '15 at 16:18
  • Unfortunately, I use this with an Access Database, not an Excel sheet. – Dean May 20 '15 at 15:31

0 Answers0