- Home /
Sql Server Compact 4.0 error
Hello.... I'm trying to establishing a connection with a local database,The database item added to as following: and I think this means that the database is connected successfully:
This, is my code:
using System;
using UnityEngine;
using System.Collections;
using System.Data.SqlClient;
public class NewBehaviourScript : MonoBehaviour {
int pointer_number = 0;
string sectionID;
string sectionName;
string search;
SqlConnection myConnection = new SqlConnection();
// Use this for initialization
void Start () {
try
{
myConnection.Open();
}
catch (Exception e)
{
print("Exception");
}
}
void Update () {
if(pointer_number == 0)
{
if(Input.GetKey(KeyCode.A))
{
Mapping();
}
}
}
void Mapping()
{
try
{
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select * from Object WHERE Name="+TextField.textFieldString.ToString(),
myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
sectionID = myReader["Section_ID"].ToString();
}
myReader.Close();
}
catch (Exception e)
{
print("Exception");
}
print(sectionID);
try
{
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select Name from Section WHERE Section_ID=" + sectionID,
myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
sectionName = myReader["Name"].ToString();
}
}
catch (Exception e)
{
print("Exception");
}
}
my problem is that the exception of the connection statement in start function is always arises at run time, and also,I think my problem is My connection string,I know it's empty,but I tried a lot of things and options but isn't working? The error that arise in the unity console is:
ExecuteReader requires an open connection to continue.This connection is closed. System.Data.sqlClient.sqlCommand.ValidateCommand(System.string method, boolean asynch) Help please.....
What is the exception that get's thrown when you connect?
I write this: SqlConnection myConnection = new SqlConnection("Data Source=(local);");
and also I removed the try-catch block the exception is the following:
(local) is used for default database instance installed on your machine, not sdf file.
Answer by ITShrewd · Oct 17, 2013 at 11:12 AM
Finally I found a way to connect unity with a database, not too hard, not too simple. The database I used is MySQL database,I install this version:
And following this tutorial as is:
In this tutorial take care to the following:
1-The default username is 'root'.
2-The tutorial has nothing about how to install MySQL database on your machine, but I found it easy and straight forward, anyway, you can refer to here as installation guide, also,During the installation I Didn't specify any users like db-administrator,I leave it empty,I set the database password only.
3-Modify The DDL and DML methods as your needs, the modification most-likely will be on the number of lists (which represent the columns) and names of columns inside the while loop that exists nearly in every method.
4-These are the namespaces you must have:
Using system;
Using System.collections.generic;
Using UnityEngine;
Using System.collections;
Using MySql.Data.MySqlClient;
and I'm thankful for everyone tried to help me......
Important: for player standalone(.exe) to work correctly you must go to Unity/editor/data/mono/lib/mono/unity and copy l18N.dll and l18N.West.dll to the folder asset in your project.
Answer by ArkaneX · Oct 16, 2013 at 09:11 AM
The main issue is empty connection string - you have to specify it. For examples you can check http://www.connectionstrings.com/sql-server-compact/
Another problem is managing your connection. Opening it inside Start
and keeping it opened is a bad practice. It should only be opened when necessary, which in your case means at the beginning of Mapping
method. And it should be closed at the end of this method, not in the middle like you did - what connection will be used for your second command?
And most importantly - you're using SqlConnection, but for SQL CE you need SqlCeConnection (System.Data.SqlServerCe namespace).
I also strongly suggest using statement to control your connection:
using(var connection = new SqlCeConnection(connectionString))
{
connection.Open();
// actual code to retrieve data from db
}
I used System.data.dll file of .NET framework V2, in this version there's no SqlCeConnection? Is that mean I can't creating a database connection in unity? or there's another way?
You need System.Data.SqlServerCe.dll for this to work. But I did a bit of research, and I found out SQL CE is not supported in $$anonymous$$ono (native libraries are required for it to work, so it's not possible on non Windows platforms).
Could you write a bit more info about your application? Do you need to access a specific database file, or do you need general way to persist data? And additionally - what is your target platform?
Alright here's my story: I want to retrieve some information from a database for the purpose of instantiating a specific game object in unity depending on the information retrieved from database. Let's say after some query I have the following:
object_ID--------Object_Name
0001-------------any-name
I want then to store Object_Name value in a variable to use it to instantiate a game-object (Let's say any-name prefab that exist in the resources folder) just as simple as that. While I was doing my search, I read that the best way to integrating database with unity application is to use Visual Studio with any built-in database. Actually,I don't know a lot about how can I use unity with database? And my target platform is PC. Thanks in advance for any help....
Do you plan to modify the data stored in this database? And do you plan to connect to a single database from each client, or do you want each application to have its own, separate db?
EDIT: by 'do you plan to modify', I mean if your application will modify the data.
No, Application will not modify the data, just retrieving. No,I want each application to has its own database.
Your answer
Follow this Question
Related Questions
SqlConnection Error con.Open() 0 Answers
How to integrate SQL database in game? 1 Answer
Can't connect from Unity 3.1 to SQL Server Express 8 Answers
cannot connect unity to sql server 2012 0 Answers
Unity/C# - Is it possible to connect an Azure (cloud) database, to a Unity game, via EnityFramework? 1 Answer