Search code examples
c#mysqlmonosqlconnection

System.Data.SqlClient.SqlConnection “Exception: Unknown packet type 74”


I’m attempting to connect to a MySQL database using System.Data.SqlClient.SqlConnection and I’m getting an “Exception: Unknown packet type 74”.

This is technically through Unity3D 4, although I’m not sure that’s relevant to the problem.* It seems to be a C# issue, and not a mono issue - even though the errors are generated within the mono modules. I could be wrong.

I have seen … Can't connect to remote SQL server in C# …but it doesn’t seem to be relevant either.

  • no, the database login info won’t be a part of the game code, so it won’t get hacked. But for reasons beyond the scope of this question, I’d prefer to connect directly and not through a socket proxy.

---SQLConnect.cs---

using UnityEngine;
using System.Collections;
using System.Net;
using System.Data.SqlClient;

namespace dataVis{

    public class SQLConnect : MonoBehaviour {

        public SqlConnection sqlConnection1;

        private string _server          = "ec2-54-165-xxx-xxx.compute-1.amazonaws.com";
        private string _port            = "3306";
        private string _db              = "xxxxdb";      
        private string _u               = "xxxuser";
        private string _p               = "xxxpass";
        private string _persistSecurity = "true";

        // Constructor ------------------------------
        public SQLConnect()
        {
            SetConnection();
        }//constructor

        private void SetConnection()
        {
            string connectionString = (
                                        "server=tcp:" + _server + ", " + _port + ";" +
                                        //"server=" + _server + ", " + _port + ";" +
                                        "Database=" + _db + ";" +  
                                        "UID=" + _u + ";" +
                                        "Password=" + _p + ";" +
                                        "PersistSecurityInfo=" + _persistSecurity
                                        );

            //Connecting to remote Microsoft SQL Database Server
            sqlConnection1 = new SqlConnection(connectionString);

            Debug.Log("(from SQLConnect)sqlConnection1 =" + sqlConnection1 + "; opening...");

            sqlConnection1.Open(); // error ocurrs here. 

            Debug.Log("Open.");

        }// SetConnection

    }//class

}//namespace

--- main.cs ---

using UnityEngine;
using System.Collections;
using System.Net;
using System.Data.SqlClient;
using dataVis;

public class Main : MonoBehaviour {

    public SQLConnect CONN;

    // Use this for initialization

    void Start () 
    {
        Debug.Log("Starting main...creating new SQL connection");
        CONN = this.gameObject.AddComponent<SQLConnect>();
        //CONN.test();
    }//start

    // Update is called once per frame
    void Update () 
    {
    }//Update

}//class

--- FULL ERROR ---

Exception: Unknown packet type 74
Mono.Data.Tds.Protocol.TdsComm.GetPhysicalPacketHeader ()
Mono.Data.Tds.Protocol.TdsComm.GetPhysicalPacket ()
Mono.Data.Tds.Protocol.TdsComm.GetByte ()
Mono.Data.Tds.Protocol.Tds.ProcessSubPacket ()
Mono.Data.Tds.Protocol.Tds.NextResult ()
Mono.Data.Tds.Protocol.Tds.SkipToEnd ()
Mono.Data.Tds.Protocol.Tds70.Connect(Mono.Data.Tds.Protocol.TdsConnectionParameters connectionParameters)
Mono.Data.Tds.Protocol.Tds80.Connect (Mono.Data.Tds.Protocol.TdsConnectionParameters connectionParameters)
System.Data.SqlClient.SqlConnection.Open ()
UnityEngine.GameObject:AddComponent()
Main:Start() (at Assets/Scripts/Main.cs:14)

Solution

  • SqlClient is for connecting to Microsoft SQL Server, not MySQL. I think MySQL Connector/NET is what you area looking for.