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.

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:

...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=

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

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

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.

I have a problem width this code, I need your help, this read the data from SQL Server 2005 only the first time when I click the button but the next time I click Unity shows "SqlException: SQL Server does not exist or access denied" can some one help me please. import System;

import System.Data;

import System.Data.Sql;

import System.Data.SqlClient;

function Update () {}

function OnGUI() { GUI.Box (Rect (10,100,400,30), Msg1);

if (GUI.Button (new Rect (0,80,100,20), "Escribe algoSQL", "button")){Msg1 = "algoSQL";}

if (GUI.Button (new Rect (120,80,100,20), "BorrarSQL", "button")){Msg1 = "";}

//if (GUI.Button (new Rect (220,20,140,20), "Enviar a Browser", "button")){SendBrowser();}

if (GUI.Button (new Rect (250,80,100,20), "LeeDatosSQL", "button")){LeeDatos ();}

}

function LeeDatos ()

{ var dataSet : System.Data.DataSet = new System.Data.DataSet();

var ConStr : String = "Server=x.x.x.x; Uid=xxxxx; Pwd=xxxxxx; Database=dbname;";

var dbCon : System.Data.IDbConnection= new System.Data.SqlClient.SqlConnection(ConStr);

var query : String = "SELECT * FROM [Table_1] WHERE ([brand] ='xxx') ORDER BY [id_num]";

var dbCommand : IDbCommand = new System.Data.SqlClient.SqlCommand();

var dataAdapter : System.Data.IDbDataAdapter = new System.Data.SqlClient.SqlDataAdapter();

    dbCommand.CommandText = query;

    dbCommand.Connection = dbCon;

    dataAdapter.SelectCommand = dbCommand;

            dataAdapter.Fill(dataSet);

}

A really dedicated solution as local data storage for your games is Siaqodb see more info here:http://siaqodb.com/?p=482

  • easy to use, very simple API
  • works on all platforms: PC, Mac, iOS, Android
  • works for all Unity3D editions(not needed Pro edition)
  • no SQL knowledge needed
  • 100% managed code
  • small footprint

Just put a siaqodb.dll in your scripts folder and GO!, no extra configuration, no nightmares with unmanaged references

the www class can be used to connect with asp.net and eventually with sql server. here is how: Write data from Unity 3D to SQL Server using ASP.NET | Roderick Vella Galea