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…