3
string TheName = "David";
string UserTable = "INSERT INTO USER (name) values (TheName)";
SQLiteCommand command2 = new SQLiteCommand(UserTable, m_dbConnection);
command2.ExecuteNonQuery();

我想知道是否可以在我的 SQLite 表中插入一个变量(如示例代码中的 TheName),如果它是你怎么能做到的?

4

2 回答 2

5

您需要参数化查询:

command2.CommandText = "INSERT INTO User (name) VALUES(@param1)";
command2.CommandType = CommandType.Text;
command2.Parameters.Add(new SQLiteParameter("@param1", TheName));
于 2013-04-17T09:28:46.803 回答
2

使用参数化查询:

string UserTable = "INSERT INTO USER (name) values ($TheName)";
command2.Parameters.AddWithValue("$TheName", TheName);

http://johnhforrest.com/2010/10/parameterized-sql-queries-in-c/

于 2013-04-17T09:27:16.327 回答