В настоящее время я выполняю задание на лифте, а в финальной части есть база данных, которая записывает, когда лифт был открыт и на каком этаже.Попытка вставить в базу данных SQLite в C# по единству
в единстве я получаю это исключение:
«InvalidOperationException: Нет соединение, связанное с этой командой»
копией полного проекта, если вы хотите посмотреть.
это код, я использовал, чтобы попытаться создать вставку, ничего о
(извините, если комментарии делают это неаккуратно я сделать это так, я могу отслеживать, что все делает)
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 parrameter data from current date variable
dbcmd.Parameters.Add(new SqliteParameter("@currenttime", currenttime)); //gives @currenttime sqlite parrameter data from current time variable
dbcmd.Parameters.Add(new SqliteParameter("@currentfloor", currentfloor)); //gives @currentfloor sqlite parrameter 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 im 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;
}
0 его не выдавать ошибок больше, но я должен также избавиться от 'SqliteCommand command = new SqliteCommand();' потому что doesnt, который создает команду в начале или мне все еще нужно, чтобы –
Чтобы очистить ваш код, да я бы. – gmiley