1

ISSUE

While trying to connect to maria db using mysql.data i get this error:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
  at System.Diagnostics.TraceInternal.get_AppName () [0x0000e] in <e804fc8285a9419784599893ed028695>:0
  at System.Diagnostics.TraceInternal.TraceEvent (System.Diagnostics.TraceEventType eventType, System.Int32 id, System.String format, System.Object[] args) [0x0003d] in <e804fc8285a9419784599893ed028695>:0
  at System.Diagnostics.Trace.TraceError (System.String message) [0x00000] in <e804fc8285a9419784599893ed028695>:0
  at MySql.Data.MySqlClient.MySqlTrace.LogError (System.Int32 id, System.String msg) [0x00026] in <89b4d3936e0b4e49b8d299ddbfe7933d>:0
  at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () [0x0002e] in <89b4d3936e0b4e49b8d299ddbfe7933d>:0
  at MySql.Data.MySqlClient.MySqlPool.GetConnection () [0x0001c] in <89b4d3936e0b4e49b8d299ddbfe7933d>:0
  at MySql.Data.MySqlClient.MySqlConnection.Open () [0x00245] in <89b4d3936e0b4e49b8d299ddbfe7933d>:0
  at MyResource.Server.ServerMain..ctor () [0x00026] in C:\Users\Maurice\CFX\MyResource\Server\ServerMain.cs:23
Done.

The .dll file was compiled with dotnet build under debian 10 without warnings or errors

TRIED:

I want to connect to sql Database (MariaDB) via C# and MySql.Data from Oracle.

First I created a folder with the C# script and the .dll's of the MySql.Data package.

After that i added the reference to the .proj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <DebugType>portable</DebugType>
    <TargetName>ServerMain.net</TargetName>
    <DefineConstants>SERVER</DefineConstants>
    <AssemblyName>TEST.Server.net</AssemblyName>
  </PropertyGroup>
    <ItemGroup>
      <Reference Include="MySql.Data">
        <HintPath>MySql.Data.DLL</HintPath>
      </Reference>
    </ItemGroup>
</Project>
  

To build the project i used the Visual Studio Code SSH Connection to run in vsc Terminal: dotnet build.

RESULT:

Succesfully started Script with test output but after SQL Connection Open() the Error as seen above was thrown.

MY CODE:

using System;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace TEST.Server
{
    public class ServerMain : BaseScript
    {
        public static void DBTEST()
        {
            var connString = "server=localhost;user=NAME;database=NAME;port=3306;password=*****";
            MySqlConnection conn = new MySqlConnection(connString);
            try
            {
                Console.WriteLine("Connecting to MySQL...");
                conn.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public ServerMain()
        {
            DBTEST();
        }
    }
}
4
  • Is the MariaDB database on the same machine as the one you are running the code from? Commented Feb 7, 2023 at 19:58
  • @MichaelBuckman Yes its hosted on the local machine Commented Feb 7, 2023 at 22:31
  • 1
    This is probably a framework mismatch -- you can't run assemblies built for .NET Framework on .NET Core and vice versa (except in special circumstances). Normally you resolve this by not building against an assembly directly, but against a NuGet package which will get the framework-appropriate assemblies automatically. Commented Feb 8, 2023 at 11:08
  • @JeroenMostert Thank you i tried this also, now getting the new error i updated in the post Commented Feb 8, 2023 at 19:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.