for a while now I've been trying to connect to my local SQLite3 database through a C# project. The project is a FiveM (which is a modification of GtaV) resource, so it works with the CitizenFX framework.
In particular, I'm creating a server-side script that can execute a query using Microsoft.Data.Sqlite. As specified in the title, I'm using .NET Standard 2.0.
The main problem is that the error trace (runtime error) doesn't seem complete. Note that in the first part it reports
System.DllNotFoundException: e_sqlite3 assembly: type: member:(null)
Below is my ServerMain.cs
:
using System;
using System.Collections.Generic;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;
namespace database.Server
{
public class ServerMain : BaseScript
{
public ServerMain()
{
Database db = new Database();
RegisterCommand("dbTest", new Action<int, List<object>, string>((source, args, rawCommand) =>
{
List<Dictionary<string, object>> result = db.Query("SELECT * FROM example_table WHERE example=8;");
foreach (Dictionary<string, object> item in result)
{
foreach (KeyValuePair<string, object> kv in item)
{
Debug.WriteLine($"{kv.Key}: {kv.Value.GetType().Name}");
}
}
}), false);
}
}
}
This is the other file.cs
:
using System.Collections.Generic;
using Microsoft.Data.Sqlite;
namespace database.Server
{
class Database
{
public SqliteConnection connection;
public Database()
{
SqliteConnectionStringBuilder builder = new SqliteConnectionStringBuilder();
builder.DataSource = "db.sqlite3";
connection = new SqliteConnection(builder.ConnectionString);
}
public List<Dictionary<string, object>> Query(string query, Dictionary<string, string> args=null)
{
connection.Open();
SqliteCommand command = connection.CreateCommand();
command.CommandText = query;
if (args!=null)
foreach (KeyValuePair<string, string> item in args)
command.Parameters.AddWithValue(item.Key, item.Value);
SqliteDataReader reader = command.ExecuteReader();
List<Dictionary<string, object>> result = Serialize(reader);
connection.Close();
return result;
}
public static List<Dictionary<string, object>> Serialize(SqliteDataReader reader)
{
Dictionary<string, int> columns = new Dictionary<string, int>();
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
while (reader.Read())
{
int fields = reader.FieldCount;
Dictionary<string, object> row = new Dictionary<string, object>();
for (short i = 0; i < fields; i++)
{
string name = reader.GetName(i);
columns[name] = i;
row[name] = reader.GetValue(i);
}
result.Add(row);
}
return result;
}
}
}
The .csproj
file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<DebugType>portable</DebugType>
<TargetName>$(AssemblyName).net</TargetName>
<DefineConstants>SERVER</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CitizenFX.Core.Server" Version="1.0.*" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.3" />
<Compile Include="../Shared/**/*.cs" />
</ItemGroup>
</Project>
And finally the traceback:
[ c-scripting-core] Creating script environments for database
[ script:database] Failed to instantiate instance of script database.Server.ServerMain: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: e_sqlite3 assembly:<unknown assembly> type:<unknown type> member:(null)
[ script:database] at (wrapper managed-to-native) SQLitePCL.SQLite3Provider_e_sqlite3+NativeMethods.sqlite3_libversion_number()
[ script:database] at SQLitePCL.SQLite3Provider_e_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number () [0x00000] in <4449c65940e74ba7b1247a8a3c44b286>:0
[ script:database] at SQLitePCL.raw.SetProvider (SQLitePCL.ISQLite3Provider imp) [0x00008] in <40a4c727ed10429e9462ed3199f679d7>:0
[ script:database] at SQLitePCL.Batteries_V2.Init () [0x00005] in <155a967c9c00435ab07bcaf82817b9c1>:0
[ script:database] at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
[ script:database] at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] Exception_EndOfInnerExceptionStack
[ script:database] at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00083] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at Microsoft.Data.Sqlite.SqliteConnection..cctor () [0x0003b] in <67ae02770bef4af48e4dbca605b09ddb>:0
[ script:database] Exception_EndOfInnerExceptionStack
[ script:database] at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr)
[ script:database] at database.Server.Database..ctor () [0x00017] in C:\Users\yuppi\Documents\Nico\Server FiveM\txData\ServerData\builders\database\Server\Database.cs:18
[ script:database] at database.Server.ServerMain..ctor () [0x0000c] in C:\Users\yuppi\Documents\Nico\Server FiveM\txData\ServerData\builders\database\Server\ServerMain.cs:13
[ script:database] at (wrapper managed-to-native) System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo,object,object[],System.Exception&)
[ script:database] at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00005] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] Exception_EndOfInnerExceptionStack
[ script:database] at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x0001a] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic, System.Boolean wrapExceptions) [0x00095] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean wrapExceptions, System.Boolean skipCheckThis, System.Boolean fillCache) [0x00009] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Boolean wrapExceptions, System.Threading.StackCrawlMark& stackMark) [0x00027] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic, System.Boolean wrapExceptions) [0x00020] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at System.Activator.CreateInstance (System.Type type) [0x00000] in <fbc4ec45371543bfba3678ebb82caf6d>:0
[ script:database] at CitizenFX.Core.InternalManager.CreateAssemblyInternal (System.String assemblyFile, System.Byte[] assemblyData, System.Byte[] symbolData) [0x0007b] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore\InternalManager.cs:156
I tried to change the version of Microsoft.Data.Sqlite
to 2.2.0
, but I still got an error, this time a different one:
[ script:database] Failed to instantiate instance of script database.Server.ServerMain: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: This is the 'bait'. You probably need to add one of the SQLitePCLRaw.bundle_* nuget packages to your platform project.
As I guessed from the error, I then also installed the SQLitePCLRaw.bundle_green
package, but the error remained the same.