string TheName = "David";
string UserTable = "INSERT INTO USER (name) values (TheName)";
SQLiteCommand command2 = new SQLiteCommand(UserTable, m_dbConnection);
command2.ExecuteNonQuery();
我想知道是否可以在我的 SQLite 表中插入一个变量(如示例代码中的 TheName),如果它是你怎么能做到的?
string TheName = "David";
string UserTable = "INSERT INTO USER (name) values (TheName)";
SQLiteCommand command2 = new SQLiteCommand(UserTable, m_dbConnection);
command2.ExecuteNonQuery();
我想知道是否可以在我的 SQLite 表中插入一个变量(如示例代码中的 TheName),如果它是你怎么能做到的?
您需要参数化查询:
command2.CommandText = "INSERT INTO User (name) VALUES(@param1)";
command2.CommandType = CommandType.Text;
command2.Parameters.Add(new SQLiteParameter("@param1", TheName));
使用参数化查询:
string UserTable = "INSERT INTO USER (name) values ($TheName)";
command2.Parameters.AddWithValue("$TheName", TheName);
http://johnhforrest.com/2010/10/parameterized-sql-queries-in-c/