Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by ITShrewd · Oct 15, 2013 at 06:02 PM · databaseconnectionsqlsqldatabase

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: alt text and I think this means that the database is connected successfully:

alt text

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

1.png (16.5 kB)
1.png (14.6 kB)
Comment
Add comment · Show 4
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 Benproductions1 · Oct 16, 2013 at 01:08 AM 0
Share

What is the exception that get's thrown when you connect?

avatar image ITShrewd · Oct 16, 2013 at 08:40 AM 0
Share

alt text

untitled.png (4.8 kB)
avatar image ITShrewd · Oct 16, 2013 at 09:12 AM 0
Share

I write this: SqlConnection myConnection = new SqlConnection("Data Source=(local);");

and also I removed the try-catch block the exception is the following: alt text

untitled.png (3.2 kB)
avatar image ArkaneX · Oct 16, 2013 at 09:35 AM 0
Share

(local) is used for default database instance installed on your machine, not sdf file.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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:

MySQL V5.7.2

And following this tutorial as is:

Connect MySQL to C# Tutorial

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.

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 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
 }


Comment
Add comment · Show 8 · 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 ITShrewd · Oct 16, 2013 at 10:26 AM 0
Share

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?

avatar image ArkaneX · Oct 16, 2013 at 10:50 AM 0
Share

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?

avatar image ITShrewd · Oct 16, 2013 at 11:43 AM 0
Share

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

avatar image ArkaneX · Oct 16, 2013 at 11:49 AM 0
Share

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.

avatar image ITShrewd · Oct 16, 2013 at 11:58 AM 0
Share

No, Application will not modify the data, just retrieving. No,I want each application to has its own database.

Show more comments

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.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges