简体   繁体   中英

SQLite, insert variable in my table (c# console app)

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

I was wondering if it is possible to insert a variable in my SQLite table (like the TheName in the example code) , and if it is how are you able to do it ?

You need parameterized queries:

command2.CommandText = "INSERT INTO User (name) VALUES(@param1)";
command2.CommandType = CommandType.Text;
command2.Parameters.Add(new SQLiteParameter("@param1", TheName));

Use a parameterized query:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM