Search Unity

MySql server connecting/working in the editor but not in the build

Discussion in 'Editor & General Support' started by Solid_Edgy, Jun 25, 2019.

  1. Solid_Edgy

    Solid_Edgy

    Joined:
    May 12, 2019
    Posts:
    7
    Below there would be an example of one of the scripts accesing the database (it's hosted on GearHost using SSMS)
    It's the first time I'm useing a database so I'm not really sure what I am missing. Couldn't find any helpful solution online. The only thing I found suggested to include the Il18N.West.dll which just gives me another assembly error.

    Help is greatly appreciated.


    The script (The InsertPlayerName is the importnant part):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using System.Data.Sql;
    6. using System.Data.SqlClient;
    7. using TMPro;
    8.  
    9. public class End : MonoBehaviour
    10. {
    11.     [SerializeField] private TextAsset dbPath;
    12.     public RaycastHit hit;
    13.     private string playerName;
    14.     private int currentID;
    15.     [SerializeField] private Canvas m_PromptCanvas;
    16.     [SerializeField] private Canvas m_UsernameCanvas;
    17.     [SerializeField] private TMP_InputField m_UsernameInput;
    18.     [SerializeField] private Canvas m_NoInternetCanvas;
    19.  
    20.     void Start()
    21.     {
    22.         m_PromptCanvas.enabled = false;
    23.         m_UsernameCanvas.enabled = false;
    24.         m_NoInternetCanvas.enabled = false;
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.  
    30.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    31.         if (m_NoInternetCanvas.enabled)
    32.         {
    33.             if (Input.GetButton("Cancel"))
    34.             {
    35.                 m_NoInternetCanvas.enabled = false;
    36.             }
    37.         }
    38.         else if (m_UsernameCanvas.enabled)
    39.         {
    40.             if (Input.GetButton("Cancel"))
    41.             {
    42.                 m_UsernameCanvas.enabled = false;
    43.             }
    44.  
    45.             if (Input.GetButton("Enter") && !string.IsNullOrWhiteSpace(m_UsernameInput.text))
    46.             {
    47.                 PlayerPrefs.SetString("username", m_UsernameInput.text);
    48.                 PlayerPrefs.Save();
    49.                 m_UsernameCanvas.enabled = false;
    50.                 m_PromptCanvas.enabled = false;
    51.             }
    52.         }
    53.         else if (m_PromptCanvas.enabled)
    54.         {
    55.  
    56.         }
    57.         else if (Physics.Raycast(ray, out hit, Mathf.Infinity, Physics.DefaultRaycastLayers, QueryTriggerInteraction.UseGlobal) && hit.collider.gameObject.tag == "Button")
    58.         {
    59.             hit.collider.gameObject.GetComponent<ButtonSwing>().changeSize = true;
    60.             hit.collider.gameObject.GetComponent<ButtonSwing>().timeSinceHit = 0;
    61.             if (Input.GetKeyDown(KeyCode.Mouse0))
    62.             {
    63.  
    64.                 if (hit.collider.gameObject.name == "Main Button")
    65.                 {
    66.                     SceneManager.LoadScene("Main Menu", LoadSceneMode.Single);
    67.                 }
    68.                 else if (hit.collider.gameObject.name == "Restart Button")
    69.                 {
    70.                     SceneManager.LoadScene("Test Main", LoadSceneMode.Single);
    71.                 }
    72.                 else if (hit.collider.gameObject.name == "Leaderboard Button")
    73.                 {
    74.                     if (!string.IsNullOrWhiteSpace(PlayerPrefs.GetString("username")))
    75.                     {
    76.                         playerName = PlayerPrefs.GetString("username");
    77.                         m_PromptCanvas.enabled = false;
    78.                         InsertPlayerName();
    79.                         InsertScore();
    80.                         SceneManager.LoadScene("Leaderboard", LoadSceneMode.Single);
    81.                     }
    82.                     else
    83.                         m_PromptCanvas.enabled = true;
    84.                 }
    85.             }
    86.         }
    87.     }
    88.  
    89.  
    90.     public void InsertPlayerName()
    91.     {
    92.         string content = dbPath.text;
    93.  
    94.         using (SqlConnection con = new SqlConnection(content))
    95.         {
    96.             string sqlcmd = "IF NOT EXISTS(SELECT player FROM dbo.Players WHERE player = '" + playerName + "') BEGIN INSERT INTO dbo.Players(player) VALUES('" + playerName + "') END";
    97.             using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
    98.             {
    99.                 cmd.CommandType = System.Data.CommandType.Text;
    100.  
    101.                 try
    102.                 {
    103.                     con.Open();
    104.                     cmd.ExecuteNonQuery();
    105.  
    106.                 }
    107.                 catch (SqlException)
    108.                 {
    109.                     m_NoInternetCanvas.enabled = true;
    110.                 }
    111.             }
    112.             con.Close();
    113.         }
    114.     }
    The screenshot of errors in the devolpment build:

    upload_2019-6-25_15-58-45.png
     
    Last edited: Jun 25, 2019
  2. Solid_Edgy

    Solid_Edgy

    Joined:
    May 12, 2019
    Posts:
    7
    By the way - I recently also tried copying the System.Data.dll into my project folder. Same error in the build