我有一段代码,但并不总是对我有用。有时它返回有关数据库锁定的错误,有时返回有关连接的错误。
这是代码:
string sql = String.Format("insert into {0}({1}) values({2});", tableName, columns, values);
SQLiteConnection myConn = null;
try
{
myConn = new SQLiteConnection(dbConnection);
myConn.Open();
using(SQLiteCommand sqCommand = new SQLiteCommand(sql))
{
sqCommand.CommandText = sql;
int rows = sqCommand.ExecuteNonQuery();
return !(rows == 0);
}
}
finally
{
myConn.Close();
}
我做错了什么?
错误是:
No connection associated with this command
в System.Data.SQLite.SQLiteCommand.InitializeForReader() в System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior
behavior) в System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior) в System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
行内:
int rows = sqCommand.ExecuteNonQuery();
最佳答案
你忘了assign 连接
到命令
:
No connection associated with this command
using(SQLiteCommand sqCommand = new SQLiteCommand(sql))
因此应该是:
using(SQLiteCommand sqCommand = new SQLiteCommand(sql, myConn))
关于c# - 如何使用SQLiteCommand进行插入查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47231521/