【问题标题】:SQLite, insert variable in my table (c# console app)SQLite,在我的表中插入变量(c#控制台应用程序)
【发布时间】:2013-04-09 23:51:47
【问题描述】:
string TheName = "David";
string UserTable = "INSERT INTO USER (name) values (TheName)";
SQLiteCommand command2 = new SQLiteCommand(UserTable, m_dbConnection);
command2.ExecuteNonQuery();

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

【问题讨论】:

    标签: c# sql .net sql-server sqlite


    【解决方案1】:

    您需要参数化查询:

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

    【讨论】:

      【解决方案2】:

      使用参数化查询:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-18
        相关资源
        最近更新 更多