Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using SQL server

Discussion in 'Multiplayer' started by tomandjerry-tas, Apr 8, 2014.

  1. tomandjerry-tas

    tomandjerry-tas

    Joined:
    Feb 7, 2013
    Posts:
    22
    Hi there, I am trying to use SQL server for online high score for my game. However, I am having trouble with connecting the SQL server to my game. Any idea how to do it? Actually i don't know how to script the coding to link with the server. Any help or refer will be appreciated :) Thank you.
     
  2. TournyMasterBot

    TournyMasterBot

    Joined:
    Sep 8, 2013
    Posts:
    13
    Can you be more specific on exactly where you're having an issue?

    Necessary information:
    - Sql Server version (2005, 2008, 2008R2, 2012?) (Express? Full edition?)
    - What are you trying to connect from? (Unity client directly? What version?)
    - What language are you trying to use?

    As a high level, general response to your question, a C# example would be similar to the following. A connection string must be correct for your database, and for correct connection string syntax, please refer to http://www.connectionstrings.com/sql-server/

    Code (csharp):
    1.  
    2. using System.Data.SqlClient
    3. var connectionstring = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
    4.  
    5. using( var conn = new SqlConnection(connectionstring))
    6. {
    7.     using (var command = new SqlCommand("select * from table where some_field = some_value", conn)
    8.     {
    9.          using (SqlDataReader reader = command.ExecuteReader())
    10.          {
    11.               //Handle the data reader
    12.          }
    13.     }
    14. }
    15.  
    Note: Depending on where/how you are using this, it may not be appropriate to wrap the connection in a using statement, and instead set up a connection on your awake function and use it throughout your programs life, and manually clean up the connection (CLEANUP IS VERY IMPORTANT! Close, and dispose your connection when you're fully done with it)
     
  3. tomandjerry-tas

    tomandjerry-tas

    Joined:
    Feb 7, 2013
    Posts:
    22
    Actually I am thinking about starting from the basic as I have no idea where to start. I have used SQL server version 2008 and I tried to connect from Unity client directly version 4.3.4 and I use C#. But as I am new to this, I would like to start from the basic. Could u please recommend me from where to start and which sql server version it works the best? Thanks for your answer. :D
     
  4. TournyMasterBot

    TournyMasterBot

    Joined:
    Sep 8, 2013
    Posts:
    13
    Sql Server 2008 is a good version, if you have it available, Sql Server 2008 R2 is a very solid baseline.

    If you have shared hosting, such as with godaddy, they generally provide the latest and greatest, which is Sql Server 2012 which for most purposes is largely the same as 2008, with a very nice amount of improvements to handling time manipulations (though this mostly impacts reporting services, rather than operational)

    Be aware there is a difference between SQL Server express, and SQL Server standard (and above) - including limits on database size and number of cores your server can utilize. Refer to the SQL Server comparison chart

    2008 R2 Basic Comparison: http://clinthuijbers.files.wordpress.com/2010/07/ssrs-2008r2.png
    Basic Comparisons for newer versions: http://www.microsoft.com/en-us/server-cloud/products/sql-server-editions/
    Very technical comparisons: http://msdn.microsoft.com/en-us/library/cc645993.aspx

    Your game also very highly determines what type of database you'll need, something that runs only locally and stores data locally has vastly different needs than, say, an MMO database.

    Estimating space requirements is also extremely important, as storage space is the minimum knowledge you need to size your server. Operations against the stored data will determine your computer spec requirements and server version requirements.

    Storage spec reference: http://technet.microsoft.com/en-us/library/ms187445.aspx

    Choosing the proper database setup is larger than a breadbox, and can't be properly represented by a forum post, as it depends too highly on your individual implementation requirements. Hopefully the links provided will at least point you in a general direction though.
     
  5. tomandjerry-tas

    tomandjerry-tas

    Joined:
    Feb 7, 2013
    Posts:
    22
    Thank you for your help. I appreciate it. :D