• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by yq li · Nov 23, 2009 at 01:22 PM · database

connect to the sql database

I need to connect to the sql database in unity3d,but can not find the fully information. i hope the company can provide the package,so we can use it.thang you.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jashan · Nov 26, 2009 at 09:04 PM 1
Share

Which database specifically do you want to connect to? And: In what environment do you want to access the database? Unless you're in a "safe environment" (e.g. local network, authoritative server that sits next to the database etc.) you probably don't want a direct (Internet) network connection to a database server.

8 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by jashan · Dec 08, 2009 at 10:30 AM

Well, the question is still pretty unclear - but here's how to connect to an MS SQL Database:

In the editor and standalones, this approach should be working fine ... I'm not sure if it also works with Web players - but even if it should work, I'd definitely not recommend letting Web players connect to your database (unless you got everything in a local network). I also wouldn't let any clients connect directly to a database unless you have a secure environment.

You need System.Data.dll from the Unity.app folder (I think under Frameworks). Do not use System.Data.dll from any Mono installation (I had very ugly crashes with that, and it took me almost forever to find what the problem was because the crashes occured when closing my game server / closing Unity after I had started the game server inside Unity - so there was no hint that it was the database connection which was causing the crashes).

You need to make sure that the SQL Server listens to TCP connections (not a default setting).

I got this up and running easily with code from the Mono project (modified somewhat, but not toooo significantly):

using System.Data; using System.Data.Sql; using System.Data.SqlClient;

...

... and then, in some method:

using (IDbConnection dbcon = new SqlConnection(connectionString)) { using (IDbCommand dbcmd = dbcon.CreateCommand()) { dbcmd.CommandType = CommandType.StoredProcedure; dbcmd.CommandText = procedureName; foreach (SqlParameter parameter in parameters) { dbcmd.Parameters.Add(parameter); } dbcon.Open(); result = dbcmd.ExecuteScalar(); dbcon.Close(); } }

procedureName also could be an SQL statement.

Ah, and I think you can't use the IP-address in the connection string (you need to use a name that can be resolved via DNS). The only parameters I could use in the connection string (I think that's a Mono limitation) are: Database, Server, uid and pwd.

Hope that helps ...

I had a similar posting about this in the forum in MS SQL connection?; other postings regarding this issue on the forums:

  • Unity and Database Access
  • Unity and Sqlite
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shame · Sep 13, 2014 at 09:00 PM 0
Share

connection string "Server=12.34.56.78;Database=YourDatabase;User ID=sa;Password=YourPassword;" works just fine

avatar image
2

Answer by darrelcusey · Nov 24, 2009 at 05:14 AM

...and (2) since I am a new user and can only post 1 link at a time:

2) Use SQLLite directly, as described here:

http://forum.unity3d.com/viewtopic.php?t=30249&start=0&postdays=0&postorder=asc&highlight=

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Daniel 15 · Feb 10, 2011 at 11:30 PM

've made a step by step tutorial in javascript here: http://forum.unity3d.com/threads/77447-Starting-my-MMO-with-SQL-SERVER!-in-Javascript

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by darrelcusey · Nov 24, 2009 at 05:09 AM

There are at least 2 ways that I know of that you can accomplish this:

1) As Duck suggested, go through the WWW class. The full example for this is here:

http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by duck · Nov 23, 2009 at 01:42 PM

You need to provide more information about this - as it stands, this is a vague question. Is this in a webplayer or standalone application? Is it a local or remote SQL server? What are you trying to achieve more broadly?

For example, to talk to a remote SQL server from a webplayer (for example, to implement a high-score board), you would typically achieve this by having your webplayer use the WWW class to make GET or POST requests to server-side scripts on your webserver. The server-side scripts would then connect locally and perform functions using the values provided by the WWW request.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

can i use database with unity 0 Answers

best way to break the string number 2 Answers

Good Setup for Predefined Array Data? 0 Answers

when i build my application i get ,some dll error 2 Answers

how to save and retreive high score.... 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges