How to fix the error "No connection associated with this command"?

SQLiteConnection connect = new SQLiteConnection("Data Source=base.db;");
connect.Open();
SQLiteCommand command = new SQLiteCommand(@"UPDATE voices SET pass = '" + pass + 
 "', hash = '" + hash + 
 "' WHERE id = '" + id + "'");
command.ExecuteNonQuery();
connect.Close();

Actually not updating records in a table fails with the error "No connection associated with this command" in the 6th line.
In what may be the problem?
June 27th 19 at 14:54
1 answer
June 27th 19 at 14:56
Solution
Well, logical. The connection in itself, command itself.

If you just look at the description SqlCommand (architecture for SQLite - the same) https://msdn.microsoft.com/ru-ru/library/system.da...

there you can see the following:

using (SqlConnection conn = new SqlConnection(connectionString)) {
 using (SqlCommand cmd = new SqlCommand(commandText, conn)) {
 // There're three command types: StoredProcedure, Text, TableDirect. The TableDirect 
 // type is only for OLE DB. 
 cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);

conn.Open();
 return cmd.ExecuteNonQuery();
}
 }


The team is constructed with reference to the connection that will be used.

Exactly the same example given in the description of the SQLiteCommand
https://www.devart.com/dotconnect/SQLite/docs/Deva...

public void ReadMyData(string myConnString)
{
 string mySelectQuery = "SELECT DeptNo, DName FROM Dept";
 SQLiteConnection sqConnection = new SQLiteConnection(myConnString);
 SqCommand SQLiteCommand = new SQLiteCommand(mySelectQuery,sqConnection); // command is constructed by specifying connection!!!
sqConnection.Open();
 SQLiteDataReader sqReader = sqCommand.ExecuteReader();
try
{
 while (sqReader.Read())
{
 Console.WriteLine(sqReader.GetInt32(0).ToString() + ", " + sqReader.GetString(1));
}
}
finally
{
 // always call Close when done reading.
sqReader.Close();
 // always call Close when done reading.
sqConnection.Close();
}
}


In General, the answer to the question "how to fix" one is to understand the essence.
Interesting point is that similar queries SELECT and INSERT work fine.
In their image and likeness I created this miracle - karine.Konopelski commented on June 27th 19 at 14:59
: well, miracles do happen -))) - delta89 commented on June 27th 19 at 15:02

Find more questions by tags C#SQLSQLite