2

I'm currently doing an assignment on an elevator and the final part is to have a database that records when the lift has opened and on what floor.

In unity I am getting this exception:

InvalidOperationException: No connection associated with this command

Copy of full project if you want to have a look.

https://onedrive.live.com/redir?resid=3F68EB193D2E3399!432&authkey=!AOGf1Et4si0a_k4&ithint=file%2czip

This is the code I have used to try and create an insert statement (sorry if the comments make it messy, I do it so I can keep track of what everything does):

void DataBase()
{
    string currenttime = DateTime.Now.ToShortTimeString();                      //sets currenttime variable to the current time
    string currentdate = DateTime.Now.ToShortDateString();                      //sets currentdate variable to the current date
    int currentfloor = 0;                                                       //creates currentfloor variable

    if (FG)                                                                     //if FG is true
    {
        currentfloor = 1;                                                       //sets current floor to 0
    }
    else if (F1)                                                                //if F1 is true
    {
        currentfloor = 2;                                                       //sets current floor to 1
    }
    else if (F2)                                                                //if F2 is true
    {
        currentfloor = 3;                                                       //sets current floor to 2
    }

    IDbConnection dbconn;

    string conn = "URI=file:" + Application.dataPath + "/lift_DB.s3db";         //Path to database.

    dbconn = (IDbConnection)new SqliteConnection(conn);                         //creates database connection

    dbconn.Open();                                                              //Open connection to the database.

    IDbCommand dbcmd = dbconn.CreateCommand();                                  //creates command on connection
    
    string sqlInsert = "INSERT INTO lift_TB (Date, Time, Floor) VALUES (@currentdate, @currenttime, @currentfloor);";   // creates insert statement on sql insert string

    SqliteCommand command = new SqliteCommand();                                //creates new sqlite command
    dbcmd.Parameters.Add(new SqliteParameter("@currentdate", currentdate));     //gives @currentdate sqlite parameter data from current date variable
    dbcmd.Parameters.Add(new SqliteParameter("@currenttime", currenttime));     //gives @currenttime sqlite parameter data from current time variable
    dbcmd.Parameters.Add(new SqliteParameter("@currentfloor", currentfloor));   //gives @currentfloor sqlite parameter data from current floor variable

    dbcmd.CommandText = sqlInsert;                                              // sets dbcmd.CommandText to be equal to the insert statement created above
    command.ExecuteNonQuery();                              
    // not sure what this is or if its needed
    IDataReader reader = dbcmd.ExecuteReader();

    while (reader.Read())
    {
    }
    
    reader.Close();                                                             //closes reader ----- not sure if this is need since I'm not reading from a database only writing to
    reader = null;

    // I assume below here just closes the connections to the data base
    dbcmd.Dispose();                                                            //disposes of command
    dbcmd = null;
    dbconn.Close();                                                             //closes connection to database
    dbconn = null;
}

2 Answers 2

1

You are calling the ExecuteNonQuery() on the command variable which does not contain any command.

Get rid of the line:

command.ExecuteNonQuery();

The following line is what actually executes the SqliteCommand that you populated:

 IDataReader reader = dbcmd.ExecuteReader();
Sign up to request clarification or add additional context in comments.

1 Comment

okay its not giving the errors any more but should i also get rid of SqliteCommand command = new SqliteCommand(); because doesnt that create the command in the start or do i still need that
1

You are using command instead of dbcmd.

You are executing a command with basically no parameters or commands attached to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.