Ian H.
Ian H.

Reputation: 3929

Should I keep the SqlConnection open?

In my Unity3D game the Player plays himself through short levels, which at the beginning take about 4 seconds. The goal is to get the best possible clear time in each level. I am currently saving these clear times locally, but I would like to upload them to an SQL Server, to be able to create a leaderboard for each level.

Since performing the SqlConnection.Open() command takes about 1-2 seconds, sometimes also 3, I was wondering whether I should keep a static connection always open, ready to execute any queries that I want to run.

Are there any unwanted and dangerous side-effects when doing it?

Edit: This is the code that I use for opening the SqlConnection.

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder()
{
    DataSource = dataServer,
    UserID = userId,
    Password = password
};

SqlConnection connection = new SqlConnection(builder.ToString());
connection.Open();

Upvotes: 0

Views: 443

Answers (1)

S.Fragkos
S.Fragkos

Reputation: 301

First I'll answer this question:

Are there any unwanted and dangerous side-effects when doing it?

Assuming you keep this code in your Game (client) and the SQL Server is not client-side but in a server of yours located somewhere, a simple Client Reverse Engineer will be able to grab your credentials from your connection string and use them to gain Unauthorized access to your database. (NEVER trust the client)

With that being said, I would suggest you use a simple server-side technology (php,Socket App, Java servlet, etc..) along with the SQL that the client will send the information there and then to the database.

Example:

1) Client-> Posts Data -> PHP

2) PHP -> Connects -> SQL Database

3) PHP -> Inserts data -> SQL Database

Like this you can also retrieve the results of your ladder from the database by asking php (or whatever technology you are using) to send your data to the client.

Best wishes in your progress, and feel free to contact me for any additional information!

Upvotes: 2

Related Questions